今日も findanglesSub2() を書いた。
今日も findanglesSub2() を書いた。
前回
ライブラリにしてみた。名前はてきとうです。
$ crystal init app testtest2
dependencies:
esty:
github: righ1113/esty
$ cd testtest2
$ shards install
# $ crystal src/testtest2.cr require "esty" module Testtest2 VERSION = "0.1.0" end # example struct Int include Esty # factorial def fact self.<=(0).if 1, D.i(self.pred.fact.*(self)) end # Fibonacci def fib self.<=(1).if (self.==(0).if 0, 1), D.i(self.pred.fib.+(self.pred.pred.fib)) end # collatz (0 is inf loop) def collatz self.==(1).if [1], D.ai(self.odd?.if D.ai(self.*(3).succ.collatz.unshift(self)), D.ai(self.tdiv(2).collatz.unshift(self))) end # FizzBuzz def fizzbuzz (s = (self.%(3).==(0).if "Fizz", "").+(self.%(5).==(0).if "Buzz", "")).==("").if self.to_s, s end # zundoko def zdk_base(str_arr) rand.>(0.5).if D.ast(self.succ.zdk_base str_arr << "Zun"), D.ast((self.>=(4)).if D.ast(str_arr << "Doko" << "Ki-Yo-Shi!"), D.ast(0.zdk_base str_arr << "Doko")) end end # run puts 10.fact # => 3628800 puts (0..10).to_a.map &.fib # => [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55] puts 3.collatz # => [3, 10, 5, 16, 8, 4, 2, 1] puts (1..30).to_a.map &.fizzbuzz # => ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz", "16", "17", "Fizz", "19", "Buzz", "Fizz", "22", "23", "Fizz", "Buzz", "26", "Fizz", "28", "29", "FizzBuzz"] def zdk 0.zdk_base [] of String end puts zdk # => ["Doko", "Zun", "Zun", "Zun", "Zun", "Doko", "Ki-Yo-Shi!"]
$ crystal src/testtest2.cr
今日も findanglesSub2() を書いた。
今日は findanglesSub2() を書いた。
今日も findangles() を書いた。
前回
前回の続きで、ズンドコキヨシをやってみた。
# $ crystal zdk.cr # 遅延評価 class Delay(T) def initialize(init_v : T, &block : -> T) @func = block @flag = false @value = init_v end def force unless @flag ret = @func.call @value = ret.nil? ? raise("@func.call is nil. #{@func}") : ret @flag = true end @value end end # Bool 構造体にメソッドを追加 struct Bool def if(t, e) return t.responds_to?(:force) ? t.force : t if self e.responds_to?(:force) ? e.force : e end end # 短縮マクロ macro d_i(x) Delay(Int32).new(0){{{x}}} end macro d_s(x) Delay(String).new(""){{{x}}} end macro d_ai(x) Delay(Array(Int32)).new([0]){{{x}}} end macro d_as(x) Delay(Array(String)).new([""]){{{x}}} end struct Int # zundoko def zdk_base(str_arr) rand.>(0.5).if d_as(self.succ.zdk_base str_arr << "Zun"), d_as((self.>=(4)).if d_as(str_arr << "Doko" << "Ki-Yo-Shi!"), d_as(0.zdk_base str_arr << "Doko")) end end def zdk 0.zdk_base [] of String end # 実行 puts zdk # => ["Doko", "Zun", "Zun", "Zun", "Zun", "Doko", "Ki-Yo-Shi!"]
ズンドコキヨシもほぼワンライナーで書けた。