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

Added FreeBASIC
(→‎{{header|TXR}}: New section.)
(Added FreeBASIC)
 
(7 intermediate revisions by 6 users not shown)
Line 108:
The First Class Functions example uses C. H. Lindsey's partial parameterization extension to Algol 68 which implemented in Algol 68G but not in algol68toc.
This example uses an alternative (technically, invalid Algol 68 as the author notes) accepted by algol68toc but not Algol 68G.
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">x: 2.0
xi: 0.5
y: 4.0
yi: 0.25
z: x + y
zi: 1 / z
 
multiplier: function [m n][
function [a] with [m n][
a*m*n
]
]
 
couple @[x y z] @[xi yi zi]
| map 'p -> multiplier p\0 p\1
| map => [call & -> 0.5]
| print</syntaxhighlight>
 
{{out}}
 
<pre>0.5 0.5 0.5</pre>
 
=={{header|Axiom}}==
Line 413 ⟶ 437:
=={{header|Elena}}==
{{trans|C#}}
ELENA 56.0x :
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Line 431 ⟶ 455:
var multiplied := numlist.zipBy(numlisti, (n1,n2 => (m => n1 * n2 * m) )).toArray();
multiplied.forEach::(multiplier){ console.printLine(multiplier(0.5r)) }
}</syntaxhighlight>
{{out}}
Line 541 ⟶ 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 810 ⟶ 861:
 
Please refer to [http://rosettacode.org/wiki/First-class_functions#Tacit_.28unorthodox.29_version First-class functions tacit (unorthodox) version] for the definitions of the functions train, an and of.
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
 
public class FirstClassFunctionsUseNumbersAnalogously {
 
public static void main(String[] args) {
final double x = 2.0, xi = 0.5,
y = 4.0, yi = 0.25,
z = x + y, zi = 1.0 / ( x + y );
 
List<Double> list = List.of( x, y, z );
List<Double> inverseList = List.of( xi, yi, zi );
BiFunction<Double, Double, Function<Double, Double>> multiplier = (a, b) -> product -> a * b * product;
for ( int i = 0; i < list.size(); i++ ) {
Function<Double, Double> multiply = multiplier.apply(list.get(i), inverseList.get(i));
final double argument = (double) ( i + 1 );
System.out.println(multiply.apply(argument));
}
}
 
}
</syntaxhighlight>
{{ out }}
<pre>
1.0
2.0
3.0
</pre>
 
=={{header|jq}}==
Line 815 ⟶ 900:
<syntaxhighlight lang="jq"># Infrastructure:
# zip this and that
def zip(that): [., that] | transpose;
. as $this | reduce range(0;length) as $i ([]; . + [ [$this[$i], that[$i]] ]);
 
# The task:
Line 833 ⟶ 917:
def multiplier(j): .[0] * .[1] * j;
 
numlist | zip(invlist) | map( multiplier(0.5) )</syntaxhighlight>
{{out}}
$ jq -n -c -f First_class_functions_Use_numbers_analogously.jq
[0.5,0.5,0.5]
 
As of this writing, there is no entry for jq at<!-- [[First-class functions#jq]]. -->
 
=={{header|JavaScript}}==
Line 1,217 ⟶ 1,301:
 
type
TMultiplerTMultiplier = reference to function(n: Double): Double;
 
function GetMultiplerGetMultiplier(a, b: Double): TMultiplerTMultiplier;
var
prod: Double;
Line 1,228 ⟶ 1,312:
 
var
Multiplier: TMultiplier;
Multipler: TMultipler;
I: Integer;
x, xi, y, yi: Double;
Line 1,240 ⟶ 1,324:
Inverses := [xi, yi, 1.0 / (x + y)];
for I := 0 to High(Numbers) do begin
MultiplerMultiplier := GetMultiplerGetMultiplier(Numbers[I], Inverses[I]);
WriteLn(MultiplerMultiplier(0.5));
end;
end.
Line 1,804 ⟶ 1,888:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var multiplier = Fn.new { |n1, n2| Fn.new { |m| n1 * n2 * m } }
2,122

edits