# Programming-Idioms

This language bar is your friend. Select your favorite languages!

# Idiom #37 Currying

Transform a function that takes multiple arguments into a function for which some of the arguments are preset.

``````fn add(a: u32, b: u32) -> u32 {
a + b
}

let add5 = move |x| add(5, x);
``````
``````(def rev-key #(update %2 %1 reverse))

(def rev-a (partial rev-key :a))
``````
``(def add5 (partial + 5))``
``````//function
auto add(int a, int b) -> int {
return a + b;
}

//curry with std::bind
using namespace std::placeholders;

//curry with lambda
auto add5 = [](int x) { return add(x, 5); };

//use
auto result = add5(1);
assert(result == 6);``````
``#include <functional>``
``````// function taking many parameters
int add(int a, int b)
{
return a + b;
}

// define a new function preseting the first parameter
std::function<int (int)> add_def(int a)
{
return [a](int b){return add(a, b);};
}

int result = add_def(4)(6);``````
``using System;``
``Func<A, C> curry<A, B, C>(Func<A, B, C> f, B b) => (A a) => f(a, b);``
``import std.functional;``
``````int add(int n1, int n2)
{
return n1 + n2;
}

``	``
``curry(f(a, b), a) => (b) => f(a, b);``
``````defmodule Curry do

def curry(fun) do
{_, arity} = :erlang.fun_info(fun, :arity)
curry(fun, arity, [])
end

def curry(fun, 0, arguments) do
apply(fun, Enum.reverse arguments)
end

def curry(fun, arity, arguments) do
fn arg -> curry(fun, arity - 1, [arg | arguments]) end
end

end``````
``````type PayFactory func(Company, *Employee, *Employee) Payroll

type CustomPayFactory func(*Employee) Payroll

func CurryPayFactory(pf PayFactory,company Company, boss *Employee) CustomPayFactory {
return func(e *Employee) Payroll {
return pf(company, boss, e)
}
}``````
``import Data.Ix``
``````curry range
``````
``````addThem :: Num a => a -> a -> a

add5 :: Num a => a -> a
``````function curry (fn, scope) {

scope = scope || window;

// omit curry function first arguments fn and scope
var args = Array.prototype.slice.call(arguments, 2);

return function() {
var trueArgs = args.concat(Array.prototype.slice.call(arguments, 0));
fn.apply(scope, trueArgs);
};
}``````
``````const curry = (fn, ...initialArgs) => (...args) => fn(...initialArgs, ...args);

const add = (a, b) => a + b;

const result = add5(1) // 6``````
``import java.util.function.*;``
``````IntBinaryOperator simpleAdd = (a, b) -> a + b;
IntFunction<IntUnaryOperator> curriedAdd = a -> b -> a + b;
``````(defun curry (fn &rest args)
(lambda (&rest remaining-args)
(apply fn (append args remaining-args))))

(defun add (a b)
(+ a b))

(funcall (curry add 2) 1)
``````
``````local unpack = unpack or table.unpack
local function aux_ncurry(n, m, fn, args)
if m>n then return fn(unpack(args,1,n))end
local new_args, new_m = {}, m
for i=1,m-1 do new_args[i]=args[i]end
return function(a)
new_args[new_m]=a
return aux_ncurry(n, new_m + 1, fn, new_args)
end
end
local function ncurry(n, fn) return aux_ncurry(n,1,fn,{}) end``````
``````function curry2(f)
return function(a)
return function(b)
return f(a,b)
end
end
end

return a + b
end

``````function curry(\$f, ...\$argsCurried)
{
return function (...\$args) use (\$f, \$argsCurried) {
\$finalArgs = array_merge(\$argsCurried, \$args);
return call_user_func_array(\$f, \$finalArgs);
};
}

{
return \$n1 + \$n2;
}

``````function curry(\$f, ...\$argsCurried)
{
return function (...\$args) use (\$f, \$argsCurried) {
return \$f(...\$argsCurried, ...\$args);
};
}

{
return \$n1 + \$n2;
}

``````sub curry {
my (\$func, \$fixed_arg) = @_;
return sub {
\$func->(\$fixed_arg, @_);
}
}
``````
``from functools import partial``
``````def f(a):
return lambda b: a+b

print (f(2)(1))

#add_to_two = partial(f, 2)``````
``````adder = -> a, b { a + b }
``````def add(x: Int, y: Int) = x + y
``````def add(x: Int)(y: Int) = x + y
``(define add5 (curry + 5))``