Currying: Difference between revisions

1,427 bytes added ,  3 months ago
m
(Dialects of BASIC moved to the BASIC section.)
 
(10 intermediate revisions by 8 users not shown)
Line 464:
bc
bcde</pre>
 
=={{header|Binary Lambda Calculus}}==
 
In BLC, all multi argument functions are necessarily achieved by currying, since lambda calculus functions (lambdas) are single argument. A good example is the Church numeral 2, which given a function f and an argument x, applies f twice on x: C2 = \f. (\x. f (f x)). This is written in BLC as
 
<pre>00 00 01 110 01 110 01</pre>
 
where 00 denotes lambda, 01 denotes application, and 1^n0 denotes the variable bound by the n'th enclosing lambda. Which is all there is to BCL!
 
=={{header|BQN}}==
Line 637 ⟶ 645:
 
=={{header|EchoLisp}}==
[[EchoLisp]] has native support for curry, which is implemented thru closures, as shown in [[CommonLisp#Common_Lisp|Common Lisp]] .
<syntaxhighlight lang="text">
;;
Line 662 ⟶ 670:
=={{header|Ecstasy}}==
<syntaxhighlight lang="java">
module CurryPower {
{
@Inject Console console;
void run() {
function Int(Int, Int) divide = (x,y) -> x / y;
{
function Int(Int, Int) divide = (x,y) -> x /half y = divide(_, 2);
function Int(Int) partsOf120 = divide(120, _);
 
function Int(Int) half = divide(_, 2);
function Int(Int) partsOf120 = divide(120, _);
 
console.print($|half of a dozen is {half(12)}
Line 677 ⟶ 682:
|and a quarter is {partsOf120(4)}
);
}
}
}
</syntaxhighlight>
 
Line 732 ⟶ 737:
 
where FUNCTION [ANY, TUPLE [Y], Z] denotes the type ''Y'' → ''Z'' (agents taking as argument a tuple with a single argument of type Y and returning a result of type Z), which is indeed the type of the agent expression used on the next-to-last line to define the "Result" of g.
 
=={{header|Elixir}}==
 
<pre>
iex(1)> plus = fn x, y -> x + y end
#Function<41.125776118/2 in :erl_eval.expr/6>
iex(2)> plus.(3, 5)
8
iex(3)> plus5 = &plus.(5, &1)
#Function<42.125776118/1 in :erl_eval.expr/6>
iex(4)> plus5.(3)
8
</pre>
 
=={{header|EMal}}==
Line 922 ⟶ 940:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Currying}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. 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 storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
In Fōrmulæ, a function is just a named lambda expression, and a function call is just a lambda application.
In '''[https://formulae.org/?example=Currying this]''' page you can see the program(s) related to this task and their results.
 
The following is a simple definition of a lambda expression:
 
[[File:Fōrmulæ - Currying 01.png]]
 
When a lambda application is called with the same number of arguments, the result is the habitual:
 
[[File:Fōrmulæ - Currying 02.png]]
 
[[File:Fōrmulæ - Currying 03.png]]
 
However, if a less number of parameters is applied, currying is performed. Notice that the result is another lambda expression.
 
[[File:Fōrmulæ - Currying 04.png]]
 
[[File:Fōrmulæ - Currying 05.png]]
 
Because the result is a lambda expression, it can be used in a lambda application, so we must get the same result:
 
[[File:Fōrmulæ - Currying 06.png]]
 
[[File:Fōrmulæ - Currying 03.png]]
 
Using functions:
 
[[File:Fōrmulæ - Currying 07.png]]
 
[[File:Fōrmulæ - Currying 08.png]]
 
=={{header|GDScript}}==
{{trans|Python}}
 
Uses Godot 4's lambdas. This runs as a script attached to a node.
<syntaxhighlight lang="gdscript">
extends Node
 
func addN(n: int) -> Callable:
return func(x):
return n + x
 
func _ready():
# Test currying
var add2 := addN(2)
print(add2.call(7))
 
get_tree().quit() # Exit
</syntaxhighlight>
 
=={{header|Go}}==
Line 1,410 ⟶ 1,475:
 
</pre>
 
A shorter form of the above function, also without type specification:
<syntaxhighlight lang="julia">
addN(n) = x -> n + x
</syntaxhighlight>
 
=={{header|Kotlin}}==
Line 1,651 ⟶ 1,721:
Out[5]:= 5
</pre>
 
=={{header|MiniScript}}==
{{trans|Rust}}
<syntaxhighlight lang="miniscript">addN = function(n)
f = function(x)
return n + x
end function
return @f
end function
 
adder = addN(40)
print "The answer to life is " + adder(2) + "."</syntaxhighlight>
 
{{out}}
<pre>The answer to life is 42.</pre>
 
=={{header|Nemerle}}==
Line 2,394 ⟶ 2,479:
=={{header|Wren}}==
{{trans|Rust}}
<syntaxhighlight lang="ecmascriptwren">var addN = Fn.new { |n| Fn.new { |x| n + x } }
 
var adder = addN.call(40)
56

edits