本文簡要介紹ruby語言中 Proc.curry
的用法。
用法
curry → a_proc
curry(arity) → a_proc
返回一個咖喱過程。如果給出了可選的arity
參數,則它確定參數的數量。一個 curried proc 接收一些參數。如果提供了足夠數量的參數,它會將提供的參數傳遞給原始 proc 並返回結果。否則,返回另一個接受其餘參數的 curried proc。
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 (given 4, expected 3)
p b.curry(5) #=> wrong number of arguments (given 5, expected 3)
p b.curry(1) #=> wrong number of arguments (given 1, expected 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 (given 1, expected 3)
b = proc { :foo }
p b.curry[] #=> :foo
相關用法
- Ruby Proc.call用法及代碼示例
- Ruby Proc.eql?用法及代碼示例
- Ruby Proc.prc ==用法及代碼示例
- Ruby Proc.ruby2_keywords用法及代碼示例
- Ruby Proc.new用法及代碼示例
- Ruby Proc.lambda?用法及代碼示例
- Ruby Proc.arity用法及代碼示例
- Ruby Proc.(params,...)用法及代碼示例
- Ruby Proc.prc << g用法及代碼示例
- Ruby Proc.parameters用法及代碼示例
- Ruby Proc.binding用法及代碼示例
- Ruby Proc.prc >>用法及代碼示例
- Ruby Proc.prc[params,...]用法及代碼示例
- Ruby Process.groups用法及代碼示例
- Ruby Process.wait2用法及代碼示例
- Ruby Process.getpgrp用法及代碼示例
- Ruby Process.setproctitle用法及代碼示例
- Ruby Process.setrlimit用法及代碼示例
- Ruby Process.uid用法及代碼示例
- Ruby Process.pid用法及代碼示例
- Ruby Process.detach用法及代碼示例
- Ruby Process.maxgroups用法及代碼示例
- Ruby Process.clock_gettime用法及代碼示例
- Ruby Process.exec用法及代碼示例
- Ruby Process.groups=用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Proc.curry。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。