Jump to content

Currying: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Better PHP example)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 42:
REAL x = read real;
print ((new line, sin (3 * x), 3 * sin (x) - 4 * (sin ** 3) (x)))</lang>
 
 
=={{header|AppleScript}}==
Line 310 ⟶ 309:
return 0
</lang>
 
 
 
=={{header|Eiffel}}==
Line 482 ⟶ 479:
--- Data stack:
{ 4 5 6 7 8 }</lang>
 
=={{header|Fōrmulæ}}==
 
In [http://wiki.formulae.org/Currying this] page you can see the solution of this task.
 
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for transportation effects more than visualization and edition.
 
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
=={{header|Forth}}==
Line 529 ⟶ 518:
2 + 6 = 8
</pre>
 
=={{header|Fōrmulæ}}==
 
In [http://wiki.formulae.org/Currying this] page you can see the solution of this task.
 
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for transportation effects more than visualization and edition.
 
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
=={{header|Go}}==
Line 1,168 ⟶ 1,165:
Puzzle
</lang>
 
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Line 1,289 ⟶ 1,285:
my $plusXOne = curry(\&plusXY, 1);
print &$plusXOne(3), "\n";</lang>
 
=={{header|Perl 6}}==
All callable objects have an "assuming" method that can do partial application of either positional or named arguments. Here we curry the built-in subtraction operator.
<lang perl6>my &negative = &infix:<->.assuming(0);
say negative 1;</lang>
{{out}}
<pre>-1</pre>
 
=={{header|Phix}}==
Line 1,600 ⟶ 1,589:
((curried+ 3) 2) ; => 5
</lang>
 
=={{header|Perl 6Raku}}==
(formerly Perl 6)
All callable objects have an "assuming" method that can do partial application of either positional or named arguments. Here we curry the built-in subtraction operator.
<lang perl6>my &negative = &infix:<->.assuming(0);
say negative 1;</lang>
{{out}}
<pre>-1</pre>
 
=={{header|REXX}}==
Line 1,688 ⟶ 1,685:
var adder = curry(add, 1);
say adder(3); #=>4</lang>
 
=={{header|Standard ML}}==
Standard ML has a built-in natural method of defining functions that are curried:
<lang sml>fun addnums (x:int) y = x+y (* declare a curried function *)
 
val add1 = addnums 1 (* bind the first argument to get another function *)
add1 42 (* apply to actually compute a result, 43 *)</lang>
The type of <code>addnums</code> above will be <tt>int -> int -> int</tt> (the type constraint in the declaration only being necessary because of the polymorphic nature of the <code>+</code> operator).
 
Note that <code>fun addnums x y = ...</code> is really just syntactic sugar for <code>val addnums = fn x => fn y => ...</code>.
 
You can also define a general currying higher-ordered function:
<lang sml>fun curry f x y = f(x,y)
(* Type signature: ('a * 'b -> 'c) -> 'a -> 'b -> 'c *)</lang>
This is a function that takes a function as a parameter and returns a function that takes one of the parameters and returns ''another'' function that takes the other parameter and returns the result of applying the parameter function to the pair of arguments.
 
=={{header|Swift}}==
Line 1,709 ⟶ 1,721:
println(add2) // (Function)
println(add2(7)) // 9</lang>
 
=={{header|Standard ML}}==
Standard ML has a built-in natural method of defining functions that are curried:
<lang sml>fun addnums (x:int) y = x+y (* declare a curried function *)
 
val add1 = addnums 1 (* bind the first argument to get another function *)
add1 42 (* apply to actually compute a result, 43 *)</lang>
The type of <code>addnums</code> above will be <tt>int -> int -> int</tt> (the type constraint in the declaration only being necessary because of the polymorphic nature of the <code>+</code> operator).
 
Note that <code>fun addnums x y = ...</code> is really just syntactic sugar for <code>val addnums = fn x => fn y => ...</code>.
 
You can also define a general currying higher-ordered function:
<lang sml>fun curry f x y = f(x,y)
(* Type signature: ('a * 'b -> 'c) -> 'a -> 'b -> 'c *)</lang>
This is a function that takes a function as a parameter and returns a function that takes one of the parameters and returns ''another'' function that takes the other parameter and returns the result of applying the parameter function to the pair of arguments.
 
=={{header|Tcl}}==
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.