【发布时间】:2021-03-04 22:54:03
【问题描述】:
ContT monad 转换器与 Cont monad 具有相同的实现,但我无法将其应用于所有三个 Either 案例
Right-
Left来自当前的一元动作 -
Left来自之前的一元计算
最后一个失败了,所有修复它的尝试也失败了:
const record = (type, o) => (
o[Symbol.toStringTag] = type.name || type,
o);
const union = type => (tag, o) => (
o[Symbol.toStringTag] = type,
o.tag = tag.name || tag,
o);
const match = (tx, o) => o[tx.tag] (tx);
const Either = union("Either");
const Left = left => Either(Left, {left});
const Right = right => Either(Right, {right});
const eithChain = mx => fm =>
match(mx, {
Left: _ => mx,
Right: ({right: x}) => fm(x)
});
const ContT = contt => record(ContT, {contt});
const contChainT = mmk => fmm =>
ContT(c => mmk.contt(x => fmm(x).contt(c)));
const main = foo =>
contChainT(ContT(k => k(foo))) (mx =>
eithChain(mx) (x =>
x === 0
? ContT(k => k(Left("ouch!")))
: ContT(k => k(Right(x * x)))));
main(Right(5)).contt(console.log); // Right(25)
main(Right(0)).contt(console.log); // Left("ouch!")
main(Left("yikes!")).contt(console.log); // Type Error
这令人沮丧。任何提示都非常感谢!
【问题讨论】:
标签: javascript functional-programming monads monad-transformers continuations