[表示 : 全て 最新50 1-99 101- 2chのread.cgiへ]
Update time : 04/29 11:45 / Filesize : 32 KB / Number-of Response : 122
[このスレッドの書き込みを削除する]
[+板 最近立ったスレ&熱いスレ一覧 : +板 最近立ったスレ/記者別一覧] [類似スレッド一覧]


↑キャッシュ検索、類似スレ動作を修正しました、ご迷惑をお掛けしました

【激突】関数型言語 VS オブジェクト指向言語



44 名前:42 mailto:sage [2012/03/11(日) 08:35:50.94 ]
メソッドfoldの定義が冗長だったので、>>42を訂正する

def fold(init, xs, &fn)
  if xs.empty?
    init
  else
    result = fn.call(init, xs[0])
    for x in xs[1..(xs.length - 1)]
      result = fn.call(result, x, &fn)
    end
    result
  end
end

def sum(ary); fold(0, ary) { |s, x| s + x }; end
def product(ary); fold(1, ary) { |p, x| p * x }; end
def reverse(ary); fold([], ary) { |xs, x| xs.unshift(x) }; end
def length(ary); fold(0, ary) { |n, _| n + 1 }; end






[ 続きを読む ] / [ 携帯版 ]

全部読む 次100 最新50 [ このスレをブックマーク! 携帯に送る ] 2chのread.cgiへ
[+板 最近立ったスレ&熱いスレ一覧 : +板 最近立ったスレ/記者別一覧](*・∀・)<32KB

read.cgi ver5.27 [feat.BBS2 +1.6] / e.0.2 (02/09/03) / eucaly.net products.
担当:undef