[英]What is wrong in this code? I am trying to move JS code in TS in Angular
[英]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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.