管道和monad如何在JavaScript中协同工作?

时间:2017-10-24 18:11:21

标签: javascript functional-programming pipe monads

我查看了类似的问题和答案,但没有找到直接解决我问题的答案。我很难理解如何将MaybeEitherMonads与管道功能结合使用。我想将函数连接在一起,但我希望管道停止并在任何步骤发生错误时返回错误。我正在尝试在node.js应用程序中实现函数式编程概念,这实际上是我对它们的第一次认真探索,所以没有答案会如此简单以至于侮辱我对这个主题的智慧。

我写了这样的管道函数:

const _pipe = (f, g) => async (...args) => await g( await f(...args))

module.exports = {arguments.
    pipeAsync: async (...fns) => {
        return await fns.reduce(_pipe)
    }, 
...

我这样称呼它:

    const token = await utils.pipeAsync(makeACall, parseAuthenticatedUser, syncUserWithCore, managejwt.maketoken)(x, y)  

2 个答案:

答案 0 :(得分:17)

勾,线和坠子

我无法强调你不会因为必须学习的所有新术语而陷入困境 - 函数编程是关于函数 - 并且也许您唯一需要了解的功能是它允许您使用参数抽象部分程序;或多个参数(如果需要)(并非如此)并且您的语言支持(通常是)

为什么我这样告诉你? JavaScript已经有一个非常好的API,用于使用内置的Promise.prototype.then

对异步函数进行排序
// never reinvent the wheel
const _pipe = (f, g) => async (...args) => await g( await f(...args))
myPromise .then (f) .then (g) .then (h) ...

但是你想编写功能程序,对吧?这对功能程序员来说没有问题。隔离你想要抽象(隐藏)的行为,并简单地将它包装在参数化的函数中 - 现在你有了一个函数,继续用函数式编写你的程序......

执行此操作一段时间后,您开始注意到抽象的模式 - 这些模式将作为您学习的所有其他事物(函子,应用程序,monad等)的用例稍后 - 但保存以后的 - 目前, 功能 ......

下面,我们通过comp演示从左到右组成的异步函数。出于本程序的目的,delay被包含为Promises创建者,sqadd1是示例异步函数 -



const delay = (ms, x) =>
  new Promise (r => setTimeout (r, ms, x))

const sq = async x =>
  delay (1000, x * x)
  
const add1 = async x =>
  delay (1000, x + 1)

// just make a function  
const comp = (f, g) =>
  // abstract away the sickness
  x => f (x) .then (g)

// resume functional programming  
const main =
  comp (sq, add1)

// print promise to console for demo
const demo = p =>
  p .then (console.log, console.error)

demo (main (10))
// 2 seconds later...
// 101




发明自己的便利

你可以创建一个接受任意数量函数的可变参数compose - 同时注意这可以让你在同一个组合中混合同步异步函数 - 这样做的好处就是直接插入.then,它会自动将非Promise返回值提升为Promise -



const delay = (ms, x) =>
  new Promise (r => setTimeout (r, ms, x))

const sq = async x =>
  delay (1000, x * x)
  
const add1 = async x =>
  delay (1000, x + 1)

// make all sorts of functions
const effect = f => x =>
  ( f (x), x )

// invent your own convenience
const log =
  effect (console.log)
  
const comp = (f, g) =>
  x => f (x) .then (g)

const compose = (...fs) =>
  fs .reduce (comp, x => Promise .resolve (x))
  
// your ritual is complete
const main =
  compose (log, add1, log, sq, log, add1, log, sq)

// print promise to console for demo
const demo = p =>
  p .then (console.log, console.error)

demo (main (10))
// 10
// 1 second later ...
// 11
// 1 second later ...
// 121
// 1 second later ...
// 122
// 1 second later ...
// 14884




更聪明,更努力

compcompose是易于理解的功能,几乎不费力地编写。因为我们使用了内置的.then,所以所有错误处理的东西都会自动连接到我们这里。您不必担心手动awaittry/catch.catch - 还有其他的好处用这种方式写我们的函数 -

抽象中没有羞耻

现在,并不是说每次你为了隐藏的东西而写一个抽象时,它都会对各种各样的东西非常有用。任务 - 例如"隐藏"命令式while -



const fibseq = n => // a counter, n
{ let seq = []      // the sequence we will generate
  let a = 0         // the first value in the sequence
  let b = 1         // the second value in the sequence
  while (n > 0)     // when the counter is above zero
  { n = n - 1             // decrement the counter
    seq = [ ...seq, a ]   // update the sequence
    a = a + b             // update the first value
    b = a - b             // update the second value
  }
  return seq        // return the final sequence
}

console .time ('while')
console .log (fibseq (500))
console .timeEnd ('while')
// [ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...  ]
// while: 3ms




但是你想编写功能程序,对吧?这对功能程序员来说没有问题。我们可以创建自己的循环机制,但这次它将使用函数和表达式而不是语句和副作用 - 所有这些都不会牺牲速度,可读性或stack safety

此处,loop使用我们的recur值容器连续应用函数。当函数返回非recur值时,计算完成,并返回最终值。 fibseq是一个纯粹的函数式表达式,具有无界递归。两个程序在大约3毫秒内计算结果。不要忘记检查答案匹配:D



const recur = (...values) =>
  ({ recur, values })

// break the rules sometimes; reinvent a better wheel
const loop = f =>
{ let acc = f ()
  while (acc && acc.recur === recur)
    acc = f (...acc.values)
  return acc
}
      
const fibseq = x =>
  loop               // start a loop with vars
    ( ( n = x        // a counter, n, starting at x
      , seq = []     // seq, the sequence we will generate
      , a = 0        // first value of the sequence
      , b = 1        // second value of the sequence
      ) =>
        n === 0      // once our counter reaches zero
          ? seq      // return the sequence
          : recur    // otherwise recur with updated vars
              ( n - 1          // the new counter
              , [ ...seq, a ]  // the new sequence
              , b              // the new first value
              , a + b          // the new second value
              )
    )

console.time ('loop/recur')
console.log (fibseq (500))
console.timeEnd ('loop/recur')
// [ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...  ]
// loop/recur: 3ms




没有什么是神圣的

请记住,你可以做任何你想做的事。 then没有什么神奇之处 - 有人决定去做某事。你可以成为某个地方的某个人而只是制作你自己的then - 这里then是一种前向合成功能 - 就像Promise.prototype.then一样,它会自动应用thenthen返回值;我们补充这一点并不是因为它是一个特别好的主意,而是表明如果我们愿意,我们可以做出这种行为。



const then = x =>
  x && x.then === then
    ? x
    : Object .assign
        ( f => then (f (x))
        , { then }
        )
  
const sq = x =>
  then (x * x)
  
const add1 = x =>
  x + 1
  
const effect = f => x =>
  ( f (x), x )
  
const log =
  effect (console.log)
  
then (10) (log) (sq) (log) (add1) (add1) (add1) (log)
// 10
// 100
// 101

sq (2) (sq) (sq) (sq) (log)
// 65536




这是什么语言?

它甚至看起来都不像JavaScript了,但是谁在乎呢?它你的程序和决定你想要它的样子。一句优秀的语言不会阻碍你,强迫你用任何特定风格编写你的程序;功能性或其他。

它实际上是JavaScript,只是对其能够表达能力的误解不受限制 -



const $ = x => k =>
  $ (k (x))
  
const add = x => y =>
  x + y

const mult = x => y =>
  x * y
  
$ (1)           // 1
  (add (2))     // + 2 = 3
  (mult (6))    // * 6 = 18
  (console.log) // 18
  
$ (7)            // 7
  (add (1))      // + 1 = 8
  (mult (8))     // * 8 = 64
  (mult (2))     // * 2 = 128
  (mult (2))     // * 2 = 256
  (console.log)  // 256




当您理解$时,您将理解the mother of all monads。记住要专注于机制并获得如何运作的直觉;不用担心这些条款。

发货

我们在本地代码段中使用了名称compcompose,但是当您打包程序时,应根据具体情况挑选有意义的名称 - 请参阅Bergi的评论推荐。

答案 1 :(得分:4)

naomik的回答非常有趣,但看起来她似乎并没有回答你的问题。

简短的回答是你的_pipe函数传播错误就好了。一旦发生错误就停止运行功能。

问题在于你的pipeAsync函数,你有正确的想法,但是你不必要地让它返回承诺函数而不是函数。

这就是为什么你不能这样做,因为它每次都会抛出错误:

const result = await pipeAsync(func1, func2)(a, b);

为了在当前状态下使用pipeAsync,您需要两个await s:一个用于获取pipeAsync的结果,另一个用于获取调用结果结果:

const result = await (await pipeAsync(func1, func2))(a, b);

解决方案

async的定义中删除不必要的awaitpipeAsync。组成一系列函数(甚至是异步函数)的行为不是异步操作:

module.exports = {
    pipeAsync: (...fns) => fns.reduce(_pipe),

一旦你做完了,一切都很顺利:



const _pipe = (f, g) => async(...args) => await g(await f(...args))
const pipeAsync = (...fns) => fns.reduce(_pipe);

const makeACall = async(a, b) => a + b;
const parseAuthenticatedUser = async(x) => x * 2;
const syncUserWithCore = async(x) => {
  throw new Error("NOOOOOO!!!!");
};
const makeToken = async(x) => x - 3;

(async() => {
  const x = 9;
  const y = 7;

  try {
    // works up to parseAuthenticatedUser and completes successfully
    const token1 = await pipeAsync(
      makeACall,
      parseAuthenticatedUser
    )(x, y);
    console.log(token1);

    // throws at syncUserWithCore
    const token2 = await pipeAsync(
      makeACall,
      parseAuthenticatedUser,
      syncUserWithCore,
      makeToken
    )(x, y);
    console.log(token2);
  } catch (e) {
    console.error(e);
  }
})();




这也可以在不使用async的情况下编写:



const _pipe = (f, g) => (...args) => Promise.resolve().then(() => f(...args)).then(g);
const pipeAsync = (...fns) => fns.reduce(_pipe);

const makeACall = (a, b) => Promise.resolve(a + b);
const parseAuthenticatedUser = (x) => Promise.resolve(x * 2);
const syncUserWithCore = (x) => {
  throw new Error("NOOOOOO!!!!");
};
const makeToken = (x) => Promise.resolve(x - 3);

const x = 9;
const y = 7;

// works up to parseAuthenticatedUser and completes successfully
pipeAsync(
  makeACall,
  parseAuthenticatedUser
)(x, y).then(r => console.log(r), e => console.error(e));

// throws at syncUserWithCore
pipeAsync(
  makeACall,
  parseAuthenticatedUser,
  syncUserWithCore,
  makeToken
)(x, y).then(r => console.log(r), e => console.error(e))