![](/img/trans.png)
[英]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.