Jump to content

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

m (Fixed a tag omission)
(→‎{{header|Groovy}}: new solution)
Line 559:
return 0 // never reached
}</lang>
 
=={{header|Groovy}}==
 
<lang groovy>def multiplier = { n1, n2 -> { m -> n1 * n2 * m } }
 
def ε = 0.00000001 // tolerance(epsilon): acceptable level of "wrongness" to account for rounding error
[(2.0):0.5, (4.0):0.25, (6.0):(1/6.0)].each { num, inv ->
def new_function = multiplier(num, inv)
(1.0..5.0).each { trial ->
assert (new_function(trial) - trial).abs() < ε
printf('%5.3f * %5.3f * %5.3f == %5.3f\n', num, inv, trial, trial)
}
println()
}</lang>
{{out}}
<pre>2.000 * 0.500 * 1.000 == 1.000
2.000 * 0.500 * 2.000 == 2.000
2.000 * 0.500 * 3.000 == 3.000
2.000 * 0.500 * 4.000 == 4.000
2.000 * 0.500 * 5.000 == 5.000
 
4.000 * 0.250 * 1.000 == 1.000
4.000 * 0.250 * 2.000 == 2.000
4.000 * 0.250 * 3.000 == 3.000
4.000 * 0.250 * 4.000 == 4.000
4.000 * 0.250 * 5.000 == 5.000
 
6.000 * 0.167 * 1.000 == 1.000
6.000 * 0.167 * 2.000 == 2.000
6.000 * 0.167 * 3.000 == 3.000
6.000 * 0.167 * 4.000 == 4.000
6.000 * 0.167 * 5.000 == 5.000</pre>
 
=={{header|Haskell}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.