我想发明的 FP 轮子的名称是什么? (见代码)
What is the name of the wheel from FP, that I am trying to invent? (see the code)
所需的最终打字稿代码是:
transform(input)(transformation1)(transformation2)(...
// input is any data, e.g. string or object
// transformationX should be a transforming function
我已经写了下面的代码了,我觉得我在发明轮子,即像这样的东西必须已经在 FP 中实现,但我不知道它是如何调用的。谁能告诉我可以改用 https://gcanti.github.io/fp-ts/ 中的哪个工具?
type Transformer = (transformation: Transformation) => Transformer
type Transformation = (input: object) => Transformer
const TranformerCreator = (input: object): Transformer
=> (transformation: Transformation): Transformer
=> transformation(input)
const transform: Transformation = (input: object) => {
return TranformerCreator(input)
}
const transformation1: Transformation = (input: object) => {
// do sometging with input
return TranformerCreator(input)
}
是continuation monad,其中函数应用作为绑定操作
const cont = x =>
k => k (x)
const add1 = x =>
cont (x + 1)
const double = x =>
cont (x * 2)
const square = x =>
cont (x * x)
cont (2) (add1) (double) (square) (console.log)
// 36
// 2 -> add1 -> 3 -> double -> 6 -> square -> 36
这是另一种编码方式,可以让您更轻松地了解其工作原理。不使用函数应用程序来绑定,而是使用显式 bind
和 unit
函数。注意:这里的"bind"与Function.prototype.bind
无关,指的是monad二元运算
class Cont
{ constructor (run)
{ this.run = run
}
bind (f)
{ return new Cont (k => this.run (x => f (x) .run (k)))
}
static unit (x)
{ return new Cont (k => k (x))
}
}
const add1 = x =>
Cont.unit (x + 1)
const double = x =>
Cont.unit (x * 2)
const square = x =>
Cont.unit (x * x)
Cont.unit (2)
.bind (add1)
.bind (double)
.bind (square)
.run (console.log) // 36
所需的最终打字稿代码是:
transform(input)(transformation1)(transformation2)(...
// input is any data, e.g. string or object
// transformationX should be a transforming function
我已经写了下面的代码了,我觉得我在发明轮子,即像这样的东西必须已经在 FP 中实现,但我不知道它是如何调用的。谁能告诉我可以改用 https://gcanti.github.io/fp-ts/ 中的哪个工具?
type Transformer = (transformation: Transformation) => Transformer
type Transformation = (input: object) => Transformer
const TranformerCreator = (input: object): Transformer
=> (transformation: Transformation): Transformer
=> transformation(input)
const transform: Transformation = (input: object) => {
return TranformerCreator(input)
}
const transformation1: Transformation = (input: object) => {
// do sometging with input
return TranformerCreator(input)
}
是continuation monad,其中函数应用作为绑定操作
const cont = x =>
k => k (x)
const add1 = x =>
cont (x + 1)
const double = x =>
cont (x * 2)
const square = x =>
cont (x * x)
cont (2) (add1) (double) (square) (console.log)
// 36
// 2 -> add1 -> 3 -> double -> 6 -> square -> 36
这是另一种编码方式,可以让您更轻松地了解其工作原理。不使用函数应用程序来绑定,而是使用显式 bind
和 unit
函数。注意:这里的"bind"与Function.prototype.bind
无关,指的是monad二元运算
class Cont
{ constructor (run)
{ this.run = run
}
bind (f)
{ return new Cont (k => this.run (x => f (x) .run (k)))
}
static unit (x)
{ return new Cont (k => k (x))
}
}
const add1 = x =>
Cont.unit (x + 1)
const double = x =>
Cont.unit (x * 2)
const square = x =>
Cont.unit (x * x)
Cont.unit (2)
.bind (add1)
.bind (double)
.bind (square)
.run (console.log) // 36