Ruby Proc curry化_Ruby_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > Ruby > Ruby Proc curry化

Ruby Proc curry化

 2011/1/2 10:31:01  fuliang  http://fuliang.javaeye.com  我要评论(0)
  • 摘要:Ruby1.9Proc增加了curry方法,可以将Proc进行curry化:greeting_func=lambda{|name,greeting|puts"#{greeting}#{name}"}.curryhappy_greeting=greeting_func["Happy"]happy_greeting["Newyear"]hello_greeting=greeting_func["Hello"]hello_greeting["world"]结合1
  • 标签:Ruby
Ruby 1.9 Proc增加了curry方法,可以将Proc进行curry化:
greeting_func = lambda { |name,greeting| puts "#{greeting} #{name}" }.curry
happy_greeting = greeting_func["Happy"]
happy_greeting["New year"]

hello_greeting = greeting_func["Hello"]
hello_greeting["world"]

结合1.9的Proc#===作为call的alias:
weekday = lambda {|day_of_week, time| day_of_week == time.wday}.curry
sunday,monday,tuesday,wednesday,thursday,friday,saturday = (0..6).collect{|i| weekday[i]}

case Time.now
when sunday,saturday
    puts "Day of rest"
when monday,tuesday,wednesday,thursday,friday,saturday
    puts "Day of work"
end
上一篇: One line Ruby code 下一篇: Lazy evaluation in Ruby
发表评论
用户名: 匿名