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

→‎{{header|Ruby}}: Sync with First-class functions#Ruby. Both examples now use Proc#[]; this one had used Proc#call.
(→‎{{header|Ruby}}: Sync with First-class functions#Ruby. Both examples now use Proc#[]; this one had used Proc#call.)
Line 737:
<lang ruby>multiplier = proc {|n1, n2| proc {|m| n1 * n2 * m}}
numlist = [x=2, y=4, x+y]
numlistiinvlist = [0.5, 0.25, 1.0/(x+y)]
p numlist.zip(numlistiinvlist).map {|n,ni invn| multiplier.call([invn, n,ni).call(][0.5)]}
# => [0.5, 0.5, 0.5]</lang>
 
This structure is identical to the treatment of Ruby's [[First-class_functionsclass functions#Ruby|first -class functions]] -- create a Proc object that returns a Proc object (a closure). WeThese examples show that a0.5 times number ''n'' (or function) multiplied by its inverse (appliedpassed to its inverse function ''f'') multipliedtimes byinverse someof number''n'' (or passed someto numberinverse asof an argument''f'') resultsreturns inthe thatoriginal number, 0.5.
 
=={{header|Scala}}==
Anonymous user