【激突】関数型言語 VS オブジェクト指向言語2at TECH【激突】関数型言語 VS オブジェクト指向言語2 - 暇つぶし2ch■コピペモード□スレを通常表示□オプションモード□このスレッドのURL■項目テキスト532:デフォルトの名無しさん 12/04/25 20:45:17.23 >>531をRubyに翻訳してみた版。1.9だが50世代に4分以上かかってしまう。 翻訳の手間を省くのにArray#*,#+を置き換えちゃったのが足を引っ張ったか? require "benchmark" class Array def *(x); zip(x.kind_of?(Array)?x:Array.new(size,x)).map{|e|e.reduce(&:*)} end def +(x); zip(x.kind_of?(Array)?x:Array.new(size,x)).map{|e|e.reduce(&:+)} end end def life(ary2d) sum = ary2d * 0 (-1..1).each{ |dx| (-1..1).each{ |dy| sum += ary2d.map{ |row| row.rotate(dx) }.rotate(dy) } } survivs = [3,4].map{ |alive| sum.map{ |row| row.map{ |cell| (cell == alive)?1:0 } } } survivs.first + (survivs.last * ary2d) end p life([ [0,1,1,1,0], [0,1,0,0,0], [0,0,1,0,0], [0,0,0,0,0], [0,0,0,0,0]]) #=> [[0,1,1,0,0],[0,1,0,1,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,1,0,0]] cells = (1..500).map{ (1..500).map{ [0,1].sample } } p Benchmark.measure{ 50.times{ cells = life(cells) } } 次ページ最新レス表示レスジャンプ類似スレ一覧スレッドの検索話題のニュースおまかせリストオプションしおりを挟むスレッドに書込スレッドの一覧暇つぶし2ch