First-class functions: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 870:
0.5000000000000001
</pre>
 
=={{header|Déjà Vu}}==
<lang dejavu>negate:
- 0
 
set :A [ @++ $ @negate @-- ]
 
set :B [ @-- $ @++ @negate ]
 
test n:
for i range 0 -- len A:
if /= n call compose @B! i @A! i n:
return false
true
 
test to-num !prompt "Enter a number: "
if:
!print "f^-1(f(x)) = x"
else:
!print "Something went wrong."
</lang>
 
{{out}}
<pre>Enter a number: 23
f^-1(f(x)) = x</pre>
 
=={{header|Dyalect}}==
Line 933 ⟶ 908:
(y, x) => fun(x, y)
}</lang>
 
=={{header|Déjà Vu}}==
<lang dejavu>negate:
- 0
 
set :A [ @++ $ @negate @-- ]
 
set :B [ @-- $ @++ @negate ]
 
test n:
for i range 0 -- len A:
if /= n call compose @B! i @A! i n:
return false
true
 
test to-num !prompt "Enter a number: "
if:
!print "f^-1(f(x)) = x"
else:
!print "Something went wrong."
</lang>
 
{{out}}
<pre>Enter a number: 23
f^-1(f(x)) = x</pre>
 
=={{header|E}}==
Line 987:
0.5
</lang>
 
=={{header|Ela}}==
Translation of Haskell:
Line 1,008 ⟶ 1,009:
 
<lang ela>[0.5,0.5,0.499999989671302]</lang>
 
 
=={{header|Elena}}==
Line 1,179:
 
main \ 0.5 0.5 0.5</lang>
 
=={{header|FreeBASIC}}==
Like C, FreeBASIC doesn't have first class functions so I've contented myself by translating their code:
Line 1,635 ⟶ 1,636:
0.5000000000000001
</pre>
 
 
=={{header|Kotlin}}==
Line 2,142:
0.5
0.5</pre>
 
=={{header|Perl 6}}==
Here we use the <tt>Z</tt> ("zipwith") metaoperator to zip the 𝐴 and 𝐵 lists with a user-defined compose function, expressed as an infix operator, <tt>∘</tt>. The <tt>.()</tt> construct invokes the function contained in the <tt>$_</tt> (current topic) variable.
<lang perl6>sub infix:<∘> (&𝑔, &𝑓) { -> \x { 𝑔 𝑓 x } }
 
my \𝐴 = &sin, &cos, { $_ ** <3/1> }
my \𝐵 = &asin, &acos, { $_ ** <1/3> }
 
say .(.5) for 𝐴 Z∘ 𝐵</lang>
Output:
<pre>0.5
0.5
0.5</pre>
 
Operators, both buildin and user-defined, are first class too.
 
<lang perl6>my @a = 1,2,3;
my @op = &infix:<+>, &infix:<->, &infix:<*>;
for flat @a Z @op -> $v, &op { say 42.&op($v) }</lang>
 
{{output}}
<pre>43
40
126</pre>
 
=={{header|Phix}}==
Line 2,435 ⟶ 2,411:
0.5
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
Here we use the <tt>Z</tt> ("zipwith") metaoperator to zip the 𝐴 and 𝐵 lists with a user-defined compose function, expressed as an infix operator, <tt>∘</tt>. The <tt>.()</tt> construct invokes the function contained in the <tt>$_</tt> (current topic) variable.
<lang perl6>sub infix:<∘> (&𝑔, &𝑓) { -> \x { 𝑔 𝑓 x } }
 
my \𝐴 = &sin, &cos, { $_ ** <3/1> }
my \𝐵 = &asin, &acos, { $_ ** <1/3> }
 
say .(.5) for 𝐴 Z∘ 𝐵</lang>
Output:
<pre>0.5
0.5
0.5</pre>
 
Operators, both buildin and user-defined, are first class too.
 
<lang perl6>my @a = 1,2,3;
my @op = &infix:<+>, &infix:<->, &infix:<*>;
for flat @a Z @op -> $v, &op { say 42.&op($v) }</lang>
 
{{output}}
<pre>43
40
126</pre>
 
=={{header|REBOL}}==
10,327

edits