First-class functions: Difference between revisions
Content deleted Content added
m Sort F# < Factor < Fantom < Forth. |
→{{header|Ruby}}: Skip Method#to_proc, because Method#[] works as well as Proc#[]. Shorten input lines, never exceed 80 characters for input lines (but an output line is 81 characters). |
||
Line 1,131: | Line 1,131: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<lang ruby>irb(main):001:0> cube = proc |
<lang ruby>irb(main):001:0> cube = proc{|x| x ** 3} |
||
=> #<Proc: |
=> #<Proc:0x0000020c66b878@(irb):1> |
||
irb(main):002:0> croot = proc |
irb(main):002:0> croot = proc{|x| x ** (1.quo 3)} |
||
=> #<Proc: |
=> #<Proc:0x00000201f6a6c8@(irb):2> |
||
irb(main):003:0> compose = proc {|f,g| proc {|x| f[g[x]]}} |
irb(main):003:0> compose = proc {|f,g| proc {|x| f[g[x]]}} |
||
=> #<Proc: |
=> #<Proc:0x00000205e4e768@(irb):3> |
||
irb(main):004:0> funclist = [Math.method(:sin) |
irb(main):004:0> funclist = [Math.method(:sin), Math.method(:cos), cube] |
||
=> [#< |
=> [#<Method: Math.sin>, #<Method: Math.cos>, #<Proc:0x0000020c66b878@(irb):1>] |
||
irb(main):005:0> |
irb(main):005:0> invlist = [Math.method(:asin), Math.method(:acos), croot] |
||
=> [#< |
=> [#<Method: Math.asin>, #<Method: Math.acos>, #<Proc:0x00000201f6a6c8@(irb):2>] |
||
irb(main): |
irb(main):009:0> funclist.zip(invlist).map {|f, invf| compose[invf, f][0.5]} |
||
=> [0.5, 0. |
=> [0.5, 0.4999999999999999, 0.5]</lang> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |