First-class functions/Use numbers analogously: Difference between revisions

Content added Content deleted
(→‎{{header|Ruby}}: Sync with First-class functions#Ruby. Both examples now use Proc#[]; this one had used Proc#call.)
Line 737: Line 737:
<lang ruby>multiplier = proc {|n1, n2| proc {|m| n1 * n2 * m}}
<lang ruby>multiplier = proc {|n1, n2| proc {|m| n1 * n2 * m}}
numlist = [x=2, y=4, x+y]
numlist = [x=2, y=4, x+y]
numlisti = [0.5, 0.25, 1.0/(x+y)]
invlist = [0.5, 0.25, 1.0/(x+y)]
p numlist.zip(numlisti).map {|n,ni| multiplier.call(n,ni).call(0.5)}
p numlist.zip(invlist).map {|n, invn| multiplier[invn, n][0.5]}
# => [0.5, 0.5, 0.5]</lang>
# => [0.5, 0.5, 0.5]</lang>


This structure is identical to the treatment of Ruby's [[First-class_functions#Ruby|first class functions]] -- create a Proc object that returns a Proc object (a closure). We show that a number (or function) multiplied by its inverse (applied to its inverse function) multiplied by some number (passed some number as an argument) results in that number.
This structure is identical to the treatment of Ruby's [[First-class functions#Ruby|first-class functions]] -- create a Proc object that returns a Proc object (a closure). These examples show that 0.5 times number ''n'' (or passed to function ''f'') times inverse of ''n'' (or passed to inverse of ''f'') returns the original number, 0.5.


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