From: Yusuke ENDOH Date: 2008-02-13T21:57:18+09:00 Subject: [ruby-dev:33762] Re: Suggestion: Proc#curry 遠藤です。 08/02/12 に Yukihiro Matsumoto さんは書きました: > |> f = proc{|x, y, z| x + y + z } > |> f2 = f.curry(2) > |> f3 = f2.curry(3) > |> f3[4] # => 9 > |> > |> ってAPIなんじゃないんでしょうか。それともたいした違いじゃな > |> い? > | > |これはカリー化ではなくて部分適用ですね。これはこれで意味がある > |かもしれません。 > > 世の中にはこういうcurry関数があふれてますね。 よくある誤用みたいです。誤用も広まれば正用? http://lambda-the-ultimate.org/node/2266 > で、納得したのでコミットしてください。 コミットしました。 可変長引数どうするとか、lambda と proc の違いどうするとか、 引数 0 個のときどうするとかは、適当に決めてしまいました。 まずかったら直します。 * call-seq: * prc.curry => a_proc * prc.curry(arity) => a_proc * * Returns a curried proc. If the optional arity argument is given, * it determines the number of arguments. * A curried proc receives some arguments. If a sufficient number of * arguments are supplied, it passes the supplied arguments to the original * proc and returns the result. Otherwise, returns another curried proc that * takes the rest of arguments. * * b = proc {|x, y, z| (x||0) + (y||0) + (z||0) } * p b.curry[1][2][3] #=> 6 * p b.curry[1, 2][3, 4] #=> 6 * p b.curry(5)[1][2][3][4][5] #=> 6 * p b.curry(5)[1, 2][3, 4][5] #=> 6 * p b.curry(1)[1] #=> 1 * * b = proc {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) } * p b.curry[1][2][3] #=> 6 * p b.curry[1, 2][3, 4] #=> 10 * p b.curry(5)[1][2][3][4][5] #=> 15 * p b.curry(5)[1, 2][3, 4][5] #=> 15 * p b.curry(1)[1] #=> 1 * * b = lambda {|x, y, z| (x||0) + (y||0) + (z||0) } * p b.curry[1][2][3] #=> 6 * p b.curry[1, 2][3, 4] #=> wrong number of arguments (4 or 3) * p b.curry(5) #=> wrong number of arguments (5 or 3) * p b.curry(1) #=> wrong number of arguments (1 or 3) * * b = lambda {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) } * p b.curry[1][2][3] #=> 6 * p b.curry[1, 2][3, 4] #=> 10 * p b.curry(5)[1][2][3][4][5] #=> 15 * p b.curry(5)[1, 2][3, 4][5] #=> 15 * p b.curry(1) #=> wrong number of arguments (1 or 3) * * b = proc { :foo } * p b.curry[] #=> :foo -- Yusuke ENDOH