ʕ•ᴥ•ʔ RUNNINGJ

函数式编程术语

arity

函数的参数个数,unary: 参数个数为1,binary: 参数个数为2.

Higher-Order Functions

一个把函数作为参数或返回结果是函数的函数被称为高阶函数。

const filter = (predicate, items) => items.filter(item => predicate(item))
const isEven = x => x % 2 === 0
filter(isEven, [1, 2, 3, 4, 5, 6]) // [2, 4, 6]

Closure

Closure是一个作用域(scope),在此保留了作用域创建时的变量,在函数返回后仍然能够被访问到。对偏函数和高阶函数非常有用。

const addTo = x => y => x + y

const addToFive = addTo(5)
cosnt addToFive(3) // => 8

Partial Application

偏函数。

const foo = (a, b, c, d, mul, off) => {
    return (a + b*b + Math.pow(c, 3) + Math.pow(d, 4)) * mul - off
}

const bar = (a, b) => foo(a, b, 10, 42, 10, 10)

bar(15, 15) === foo(15, 15, 10, 42, 10, 10) // => true

Currying

将多参数的函数转化成一系列单参数函数的过程。

Auto Currying

如果调用一个函数时,参数不够定义时的参数,则自动返回一个等待剩余参数偏函数,知道所给参数足够,则返回结果。

Referential Transparency

一个表达式在任何时候都能被替换成其对应的value。

const greet = () => 'Hello World!'

任何调用greet()都能替换成Hello World!

Lambda

可被当做value的匿名函数。

Lamda Calculus

数学分支,通过函数来建立计算模型。

Purity

如果函数的返回值只取决于函数的输入,则称其为pure。

Side effects

函数除了返回之外,读取或写入了外部的可变状态。

Idempotent

重复调用,返回的结果不会改变。

Function Composition

将两个或更多的函数作为参数传入,组合成一个新的函数。

Point-Free style

函数的定义没有显示声明使用的参数,通常需要使用科里化和高阶函数。

Predicate

一个返回true或false的函数。

Contracts

定义函数行为的责任和保证。如果contract被违反,通常会抛出错误。

Category

集合论中概念,元素和映射,单位元,结合律。

Value

能够被赋值给变量的东西。

Constant

不能被重新赋值的变量。

Functor

(函子) 实现了map的对象,遍历每个元素,生成同类型的functror。满足两个规则:

保留同一性

`object.map(x => x) is object

可组合

object.map(compose(f, g)) === object.map(f).map(g)

Pointed Functor

An object with an of function that puts an single value into it.

Lifting

lift a function into a context. for example, give a function a => b, and lift it into List, then signature would be List[a] => List[b].

Equational Reasoning

如果程序由表达式组成,且没有副作用,系统的结果变可以通过部分推算而来。

Monoid

(幺半群) An object with a function that can “combines” that object with another of the same type.

简单的例子是:

i + 1 // i is a monoid

需要满足一下规律: 同一律 有一个单位元,与单位元操作不改变原值。

i + 0 // => i

结合律

a + (b + c)  == (a + b) + c

Monad

(定义在Functor范畴中的Monoid幺半群)

Comonad

An object that has extract and extend functions.

Morphism

A transformation function.(映射)

Endomorphism

A function where the input type is same as the output.

Isomorphism

同构

Homomorphism

同态

Catamorphism

折叠,例如 reduce

Anamorphism

reduce的反向,通过单一value生成list。

Hylomorphism

The combination of anamorphism and catamorphism.

Setoid

An object that has an equals function which can be used to compare other objects of the same type.

需满足:

反身性: a.eq(a) => true 交换性: a.eq(b) == b.eq(a) 传递性: a.eq(b) && b.eq(c) => a.eq(c)

Semigroup

(半群): 满足结合律的代数结构。

Foldable

折叠,生成其他类型的。

Lens

A lens is a type that pairs a getter and a non-mutating setter for some other data structure.

Type Signature

Every function in Rust will indicate the types of their arguments and return values.

ref: https://functional.works-hub.com/learn/functional-programming-jargon-in-rust-1b555