在 JavaScript 中管道和 monad 如何协同工作?
How do pipes and monads work together in JavaScript?
我查看了类似的问题和答案,但没有找到直接解决我的问题的答案。我正在努力了解如何将 Maybe
或 Either
或 Monads
与管道功能结合使用。我想将函数通过管道连接在一起,但我希望管道停止,并且 return 如果在任何步骤发生错误,都会出现错误。我正在尝试在 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)
钩、线和坠子
我无法强调您不会被所有感觉必须学习的新术语所困扰是多么重要 – 函数式编程是关于 函数 –也许您唯一需要了解的功能是它允许您使用参数抽象部分程序;或多个参数,如果需要(不是)并且受您的语言支持(通常是)
我为什么要告诉你这个?好吧 JavaScript 已经有了一个非常好的 API 用于使用内置 Promise.prototype.then
对异步函数进行排序
//永远不要重新发明轮子
<s>const _pipe = (f, g) => async (...args) => await g( await f(...args))</s>
myPromise .then (f) .then (g) .then (h) ...
但是你想写函数式程序,对吧?这对函数式程序员来说不是问题。隔离你想要抽象(隐藏)的行为,并简单地将它包装在一个参数化的函数中——既然你有一个函数,继续以函数式风格编写你的程序...
这样做一段时间后,您会开始注意到抽象的 模式 – 这些模式将作为所有其他事物(函子、应用程序、单子)的用例, 等等)你以后会知道——但是把那些留到 later——现在,functions .. .
下面,我们通过comp
演示从左到右 异步函数的组合。出于本程序的目的,delay
作为 Promises 创建者包含在内,sq
和 add1
是示例异步函数 -
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
——还要注意这如何允许你在同一个组合中混合同步和异步函数——a直接插入 .then
的好处,它会自动将非 Promise return 值提升为 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
更聪明地工作,而不是更努力地工作
comp
和 compose
是易于理解的函数,几乎不花力气编写。因为我们使用了内置的 .then
,所以所有错误处理的东西都会自动为我们连接起来。您不必担心手动 await
'ing 或 try/catch
或 .catch
'ing – 然而 另一个 以这种方式编写函数的好处-
抽象无耻
现在,这并不是说每次写抽象都是为了隐藏某些东西 不好的东西,但它对各种任务都非常有用 – 采取例如“隐藏”命令式 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
但是你想写函数式程序,对吧?这对函数式程序员来说不是问题。我们可以创建自己的循环机制,但这次它将使用函数和表达式而不是语句和副作用——所有这些都不会牺牲速度、可读性或 .
在这里,loop
使用我们的 recur
值容器连续应用一个函数。当函数 return 为非 recur
值时,计算完成,最终值为 returned。 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
一样,它会自动将 then
应用于非 then
return 值;我们添加这个并不是因为这是一个特别好的主意,而是为了表明如果我们愿意,我们可以做出这种行为。
const then = 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。请记住专注于机制并获得直觉它是如何工作的;少担心条款。
发货
我们只是在我们的本地代码片段中使用了名称 comp
和 compose
,但是当您打包您的程序时,您应该选择对您的特定上下文有意义的名称——请参阅 Bergi 的评论推荐。
naomik 的回答很有趣,但她似乎并没有抽出时间来回答你的问题。
简短的回答是您的 _pipe
函数可以很好地传播错误。并在抛出错误时立即停止 运行 函数。
问题出在你的 pipeAsync
函数上,你的想法是正确的,但你不必要地让它返回一个 函数 的承诺而不是函数。
这就是你不能这样做的原因,因为它每次都会抛出一个错误:
const result = await pipeAsync(func1, func2)(a, b);
为了在当前状态下使用 pipeAsync
,您需要两个 await
:一个用于获取 pipeAsync
的结果,一个用于获取调用的结果结果:
const result = await (await pipeAsync(func1, func2))(a, b);
解决方法
从pipeAsync
的定义中删除不必要的async
和await
。组合一系列函数的行为,即使是异步函数,也不是异步操作:
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))
我查看了类似的问题和答案,但没有找到直接解决我的问题的答案。我正在努力了解如何将 Maybe
或 Either
或 Monads
与管道功能结合使用。我想将函数通过管道连接在一起,但我希望管道停止,并且 return 如果在任何步骤发生错误,都会出现错误。我正在尝试在 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)
钩、线和坠子
我无法强调您不会被所有感觉必须学习的新术语所困扰是多么重要 – 函数式编程是关于 函数 –也许您唯一需要了解的功能是它允许您使用参数抽象部分程序;或多个参数,如果需要(不是)并且受您的语言支持(通常是)
我为什么要告诉你这个?好吧 JavaScript 已经有了一个非常好的 API 用于使用内置 Promise.prototype.then
//永远不要重新发明轮子
<s>const _pipe = (f, g) => async (...args) => await g( await f(...args))</s>
myPromise .then (f) .then (g) .then (h) ...
但是你想写函数式程序,对吧?这对函数式程序员来说不是问题。隔离你想要抽象(隐藏)的行为,并简单地将它包装在一个参数化的函数中——既然你有一个函数,继续以函数式风格编写你的程序...
这样做一段时间后,您会开始注意到抽象的 模式 – 这些模式将作为所有其他事物(函子、应用程序、单子)的用例, 等等)你以后会知道——但是把那些留到 later——现在,functions .. .
下面,我们通过comp
演示从左到右 异步函数的组合。出于本程序的目的,delay
作为 Promises 创建者包含在内,sq
和 add1
是示例异步函数 -
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
——还要注意这如何允许你在同一个组合中混合同步和异步函数——a直接插入 .then
的好处,它会自动将非 Promise return 值提升为 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
更聪明地工作,而不是更努力地工作
comp
和 compose
是易于理解的函数,几乎不花力气编写。因为我们使用了内置的 .then
,所以所有错误处理的东西都会自动为我们连接起来。您不必担心手动 await
'ing 或 try/catch
或 .catch
'ing – 然而 另一个 以这种方式编写函数的好处-
抽象无耻
现在,这并不是说每次写抽象都是为了隐藏某些东西 不好的东西,但它对各种任务都非常有用 – 采取例如“隐藏”命令式 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
但是你想写函数式程序,对吧?这对函数式程序员来说不是问题。我们可以创建自己的循环机制,但这次它将使用函数和表达式而不是语句和副作用——所有这些都不会牺牲速度、可读性或
在这里,loop
使用我们的 recur
值容器连续应用一个函数。当函数 return 为非 recur
值时,计算完成,最终值为 returned。 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
一样,它会自动将 then
应用于非 then
return 值;我们添加这个并不是因为这是一个特别好的主意,而是为了表明如果我们愿意,我们可以做出这种行为。
const then = 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。请记住专注于机制并获得直觉它是如何工作的;少担心条款。
发货
我们只是在我们的本地代码片段中使用了名称 comp
和 compose
,但是当您打包您的程序时,您应该选择对您的特定上下文有意义的名称——请参阅 Bergi 的评论推荐。
naomik 的回答很有趣,但她似乎并没有抽出时间来回答你的问题。
简短的回答是您的 _pipe
函数可以很好地传播错误。并在抛出错误时立即停止 运行 函数。
问题出在你的 pipeAsync
函数上,你的想法是正确的,但你不必要地让它返回一个 函数 的承诺而不是函数。
这就是你不能这样做的原因,因为它每次都会抛出一个错误:
const result = await pipeAsync(func1, func2)(a, b);
为了在当前状态下使用 pipeAsync
,您需要两个 await
:一个用于获取 pipeAsync
的结果,一个用于获取调用的结果结果:
const result = await (await pipeAsync(func1, func2))(a, b);
解决方法
从pipeAsync
的定义中删除不必要的async
和await
。组合一系列函数的行为,即使是异步函数,也不是异步操作:
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))