First-class functions/Use numbers analogously: Difference between revisions
First-class functions/Use numbers analogously (view source)
Revision as of 12:05, 4 March 2024
, 2 months agoAdded FreeBASIC
(New post.) |
(Added FreeBASIC) |
||
(3 intermediate revisions by 3 users not shown) | |||
Line 437:
=={{header|Elena}}==
{{trans|C#}}
ELENA
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Line 455:
var multiplied := numlist.zipBy(numlisti, (n1,n2 => (m => n1 * n2 * m) )).toArray();
multiplied.forEach::(multiplier){ console.printLine(multiplier(0.5r)) }
}</syntaxhighlight>
{{out}}
Line 565:
}
</syntaxhighlight>
=={{header|FreeBASIC}}==
FreeBASIC does not support first-class functions or function closures, which means that you cannot create a function that returns another function or that has a function defined inside it.
However, similar behavior can be achieved with subroutines and global variables.
<syntaxhighlight lang="vbnet">Dim As Double x = 2.0, xi = 0.5
Dim As Double y = 4.0, yi = 0.25
Dim As Double z = x + y, zi = 1.0 / (x + y)
Dim As Double values(2) = {x, y, z}
Dim As Double inverses(2) = {xi, yi, zi}
Dim Shared As Double m = 0.5
Function multiplier(a As Double, d As Double) As Double
Return a * d * m
End Function
For i As Byte = 0 To Ubound(values)
Dim As Double new_function = multiplier(values(i), inverses(i))
Print values(i); " *"; inverses(i); " *"; m; " ="; new_function
Next i
Sleep</syntaxhighlight>
{{out}}
<pre> 2 * 0.5 * 0.5 = 0.5
4 * 0.25 * 0.5 = 0.5
6 * 0.1666666666666667 * 0.5 = 0.5</pre>
=={{header|Go}}==
Line 837 ⟶ 864:
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.List;
import java.util.function.BiFunction;
Line 849 ⟶ 875:
z = x + y, zi = 1.0 / ( x + y );
List<Double> list =
List<Double> inverseList =
BiFunction<Double, Double, Function<Double, Double>> multiplier = (a, b) -> product -> a * b * product;
Line 1,862 ⟶ 1,888:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="
var multiplier = Fn.new { |n1, n2| Fn.new { |m| n1 * n2 * m } }
|