>>> from functools import partial as curry
>>> # Original function taking three parameters:>>> def display_quote(who, subject, quote):print who, 'said regarding', subject + ':'print '"' + quote + '"'
>>> display_quote("hoohoo", "functional languages","I like Erlang, not sure yet about Haskell.")hoohoo said regarding functional languages:"I like Erlang, not sure yet about Haskell."
>>> # Let's curry the function to get another that always quotes Alex...>>> am_quote = curry(display_quote, "Alex Martelli")
>>> am_quote("currying", "As usual, wikipedia has a nice summary...")Alex Martelli said regarding currying:"As usual, wikipedia has a nice summary..."
public static class FuncExtensions {public static Func<T1, Func<T2, TResult>> Curry<T1, T2, TResult>(this Func<T1, T2, TResult> func){return x1 => x2 => func(x1, x2);}}
//Usagevar add = new Func<int, int, int>((x, y) => x + y).Curry();var func = add(1);
//Obtaining the next parameter here, calling later the func with next parameter.//Or you can prepare some base calculations at the previous step and then//use the result of those calculations when calling the func multiple times//with different input parameters.
int result = func(1);
const multiply = (presetConstant) => {return (x) => {return presetConstant * x;};};
const multiplyByTwo = multiply(2);
// now multiplyByTwo is like below function & due to closure property in JavaScript it will always be able to access 'presetConstant' value// const multiplyByTwo = (x) => {// return presetConstant * x;// };
console.log(`multiplyByTwo(8) : ${multiplyByTwo(8)}`);
例如,如果您有一个柯里化函数add,您可以将JSx => k + x(或Python lambda x: k + x或Ruby{ |x| k + x }或Lisp(lambda (x) (+ k x))或…)的等价物编写为仅add(k)。在Haskelll中,您甚至可以使用运算符:(k +)或(+ k)(这两种形式允许您对非交换运算符进行任何一种柯里化:(/ 9)是一个将数字除以9的函数,这可能是更常见的用例,但您也可以将(9 /)用于将9除以其参数的函数。)除了更短之外,柯里化版本不包含像所有其他版本中的x => k + x0那样的虚构参数名称。它不需要。你正在定义一个函数,它向一个数字添加一些常量k,你不需要为该数字命名只是为了谈论这个函数。甚至不需要定义它。这是所谓的“点自由风格”的一个例子。你可以将操作组合在一起,只给出操作本身。你不必声明什么都不做的匿名函数,只是对它们的参数应用一些操作,因为*这就是操作已经是的。