Currying: Difference between revisions

Content added Content deleted
(Better PHP example)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 42: Line 42:
REAL x = read real;
REAL x = read real;
print ((new line, sin (3 * x), 3 * sin (x) - 4 * (sin ** 3) (x)))</lang>
print ((new line, sin (3 * x), 3 * sin (x) - 4 * (sin ** 3) (x)))</lang>



=={{header|AppleScript}}==
=={{header|AppleScript}}==
Line 310: Line 309:
return 0
return 0
</lang>
</lang>




=={{header|Eiffel}}==
=={{header|Eiffel}}==
Line 482: Line 479:
--- Data stack:
--- Data stack:
{ 4 5 6 7 8 }</lang>
{ 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}}==
=={{header|Forth}}==
Line 529: Line 518:
2 + 6 = 8
2 + 6 = 8
</pre>
</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}}==
=={{header|Go}}==
Line 1,168: Line 1,165:
Puzzle
Puzzle
</lang>
</lang>



=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Line 1,289: Line 1,285:
my $plusXOne = curry(\&plusXY, 1);
my $plusXOne = curry(\&plusXY, 1);
print &$plusXOne(3), "\n";</lang>
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}}==
=={{header|Phix}}==
Line 1,600: Line 1,589:
((curried+ 3) 2) ; => 5
((curried+ 3) 2) ; => 5
</lang>
</lang>

=={{header|Raku}}==
(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}}==
=={{header|REXX}}==
Line 1,688: Line 1,685:
var adder = curry(add, 1);
var adder = curry(add, 1);
say adder(3); #=>4</lang>
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}}==
=={{header|Swift}}==
Line 1,709: Line 1,721:
println(add2) // (Function)
println(add2) // (Function)
println(add2(7)) // 9</lang>
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}}==
=={{header|Tcl}}==