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 {|x| x ** 3}
<lang ruby>irb(main):001:0> cube = proc{|x| x ** 3}
=> #<Proc:0xb7cac4b8@(irb):1>
=> #<Proc:0x0000020c66b878@(irb):1>
irb(main):002:0> croot = proc {|x| x ** (1/3.0)}
irb(main):002:0> croot = proc{|x| x ** (1.quo 3)}
=> #<Proc:0xb7ca40d8@(irb):2>
=> #<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:0xb7c9996c@(irb):3>
=> #<Proc:0x00000205e4e768@(irb):3>
irb(main):004:0> funclist = [Math.method(:sin).to_proc, Math.method(:cos).to_proc, cube]
irb(main):004:0> funclist = [Math.method(:sin), Math.method(:cos), cube]
=> [#<Proc:0xb7c84be8@(irb):4>, #<Proc:0xb7c84bac@(irb):4>, #<Proc:0xb7cac4b8@(irb):1>]
=> [#<Method: Math.sin>, #<Method: Math.cos>, #<Proc:0x0000020c66b878@(irb):1>]
irb(main):005:0> funclisti = [Math.method(:asin).to_proc, Math.method(:acos).to_proc, croot]
irb(main):005:0> invlist = [Math.method(:asin), Math.method(:acos), croot]
=> [#<Proc:0xb7c7a88c@(irb):5>, #<Proc:0xb7c7a850@(irb):5>, #<Proc:0xb7ca40d8@(irb):2>]
=> [#<Method: Math.asin>, #<Method: Math.acos>, #<Proc:0x00000201f6a6c8@(irb):2>]
irb(main):006:0> funclist.zip(funclisti).map {|f,inversef| compose[inversef, f][0.5] }
irb(main):009:0> funclist.zip(invlist).map {|f, invf| compose[invf, f][0.5]}
=> [0.5, 0.5, 0.5]</lang>
=> [0.5, 0.4999999999999999, 0.5]</lang>


=={{header|Scala}}==
=={{header|Scala}}==