コラッツ予想がとけたらいいな2

自分の考察を書いていきます。

Crystal をやってみた その8(ズンドコキヨシ)

前回


前回の続きで、ズンドコキヨシをやってみた。


# $ 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!"]


ズンドコキヨシもほぼワンライナーで書けた。




Crystal をやってみた その7(FizzBuzz)

前回


Crystal のドキュメントはこの pdf が良い。
https://crystal-jp.github.io/introducing-crystal/assets/pdfs/introducing-crystal.pdf

前回の続きで、FizzBuzz をやってみた。


# $ crystal fizzbuzz.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_ai(x)
  Delay(Array(Int32)).new([0]){{{x}}}
end
macro d_s(x)
  Delay(String).new(""){{{x}}}
end

struct Int
  # FizzBuzz
  def fizzbuzz
    (s = (self.%(3).==(0).if "Fizz", "").+(self.%(5).==(0).if "Buzz", "")).==("").if self.to_s, s
  end
end

# 実行
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"]


代入式は、その入れた値が返ってくる事を利用した。
これはけっこう使えると思う。
FizzBuzzワンライナーで書けた。




GitHub_4CT_Ruby 21/04/05の変更 Angles の RBS

Angles の RBS を忘れていた。
Ruby の型チェックは、RBS を書いて、steep check という流れだ。

エラーの始め 5つ は仕方ないとして、最後 1つ が解決できなかった。

/4CT_Ruby$ steep check
program/app/reduce.rb:31:10: NoMethodError: type=(::Array[::Array[::Array[::Integer]]] | nil), method=each_with_index (g_confs.data[0..4].each_with_index)
program/app/reduce.rb:31:10: NoMethodError: type=(::Array[::Array[::Array[::Integer]]] | nil), method=each_with_index (g_confs.data[0..4].each_with_index)
program/app/reduce.rb:49:12: NoMethodError: type=singleton(::Strip), method=new (Strip.new g_conf)
program/app/reduce.rb:59:13: NoMethodError: type=singleton(::Angles), method=new (Angles.new g_conf, strip.edgeno)
program/lib/c_read_file.rb:50:6: NoMethodError: type=singleton(::File), method=foreach (File.foreach(format('../4ct_data/d_tactics%02<num>d.txt', num: deg)) do |line|)
program/lib/r_angles.rb:112:10: UnresolvedOverloading: receiver=::Array[(true | false)], method_name=[]=, method_types=(::int, (true | false)) -> (true | false) | (::int, ::int, (true | false)) -> (true | false) | (::int, ::int, ::Array[(true | false)]) -> ::Array[(true | false)] | (::int, ::int, nil) -> nil | (::Range[::Integer], (true | false)) -> (true | false) | (::Range[::Integer], ::Array[(true | false)]) -> ::Array[(true | false)] | (::Range[::Integer], nil) -> nil (neighbour[u] = false)