currying
curry让一个多参函数变为了一个每次都只有一个参数的函数嵌套结构:
https://stackoverflow.com/questions/36314/what-is-currying
多参数函数 curry 化后变为函数嵌套,嵌套每层都是单参数。
Currying is when you break down a function that takes multiple arguments into a series of functions that each take only one argument
其实我们平常经常使用的是一个被 curry 成一个需要环境中完成参数注入,最后返回一个 单参数 函数的高阶函数。
compose
多个函数如果它们的输入跟输出都是同一样”东西”,那这一系列函数就满足结合律,就可以被自由 compose。
当然这是最理想的情况。
redux 的 compose 是:
/**
* Composes single-argument functions from right to left. The rightmost
* function can take multiple arguments as it provides the signature for
* the resulting composite function.
*
* @param {...Function} funcs The functions to compose.
* @returns {Function} A function obtained by composing the argument functions
* from right to left. For example, compose(f, g, h) is identical to doing
* (...args) => f(g(h(...args))).
*/
export default function compose(...funcs) {
if (funcs.length === 0) {
return arg => arg
}
if (funcs.length === 1) {
return funcs[0]
}
return funcs.reduce((a, b) => (...args) => a(b(...args)))
}
Composes single-argument functions from right to left. The rightmost function can take multiple arguments as it provides the signature for the resulting composite function.
将多个 单参数函数,从右往左结合。
最右端的函数可接受多参数做初始化,并且它也为结合后的函数提供函数签名。它的产物将用于后续处理的输入并输出。
params 右1=> someThing 右2=>… 左1=> someThing
也就是最右端的函数可以接受一个多参数,生成后续需要处理及输出的同一样”东西”。
常见的高阶嵌套都拥有的一个特点就是,需要在环境中传入配置参数,之后返回一个接受组件,并返回组件的函数。所以它们都相当于一个坷里化了一层的高阶函数,因为它们接受了环境参数后的函数输入输出一致,所以它们可以被 compose。
Function Signature
https://stackoverflow.com/questions/2322736/what-is-the-difference-between-function-declaration-and-signature
A function declaration is the prototype for a function (or it can come from the function definition if no prototype has been seen by the compiler at that point) - it includes the return type, the name of the function and the types of the parameters (optionally in C).
A function signature is the parts of the function declaration that the compiler uses to perform overload resolution. Since multiple functions might have the same name (ie., they’re overloaded), the compiler needs a way to determine which of several possible functions with a particular name a function call should resolve to. The signature is what the compiler considers in that overload resolution. Specifically, the standard defines ‘signature’ as:
the information about a function that participates in overload resolution: the types of its parameters and, if the function is a class member, the cv-qualifiers (if any) on the function itself and the class in which the member function is declared.
Note that the return type is not part of the function signature. As the standard says in a footnote, “Function signatures do not include return type, because that does not participate in overload resolution”.
函数声明是函数的 prototype 也就是函数的 原始类型。
declaration 相当于类型声明,definition 则是实际实现(所以只有ts中有declaration)。
函数签名是用于识别具体需要执行的函数体的解决方案,它一般是函数声明的一部分。用 ts 来解释就是一个函数所接受参数的类型,因为接受参数类型可静态解析,所以它也就可以用来具体匹配到函数重载中的某一个具体函数。