钩、线和坠子
我不能强调不要被所有你必须学习的新术语所困扰——函数式编程是关于函数的——这可能是唯一的事情您需要了解的功能是它允许您使用参数抽象部分程序;或多个参数(如果需要(不是)并且您的语言支持(通常是))
我为什么要告诉你这个?好吧,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) ...
但是您想编写函数式程序,对吗?这对函数式程序员来说没有问题。隔离您想要抽象(隐藏)的行为,并将其简单地包装在一个参数化的 function 中——现在您有了一个函数,继续以函数式风格编写您的程序...
在您这样做一段时间后,您开始注意到抽象的模式——这些模式将用作您学习的所有其他事物(函子、应用程序、monad 等)的用例关于稍后 - 但保存那些稍后 - 现在,函数 ...
下面,我们通过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 来接受任意数量的函数 - 还要注意这如何让您在同一组合中混合同步 和 异步函数 - 插入 @987654332 的好处@,它会自动将非 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
工作更聪明,而不是更努力
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
但是您想编写函数式程序,对吗?这对函数式程序员来说没有问题。我们可以创建自己的循环机制,但这次它将使用函数和表达式而不是语句和副作用——所有这些都不会牺牲速度、可读性或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一样,它会自动将then应用于非then的返回值;我们添加这个不是因为它是一个特别好的主意,而是为了表明我们可以根据需要做出这种行为。
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。记住要专注于机制并了解它是如何工作的;不用担心条款。
发货
我们只是在本地 sn-ps 中使用了名称 comp 和 compose,但是当您打包程序时,您应该根据您的特定上下文选择有意义的名称 - 请参阅 Bergi 的评论以获取建议。