函数编码类型(即嵌套的咖喱函数)在Javascript中有一些缺点:
[Some(5), None]
显示为[f, f]
)eitherMap(f) (Some(5))
)这些缺点使它们在实际应用中毫无用处。
我想知道是否有办法克服这些缺点,并提出以下草图:
const tag = (name, x, k) => {
const Cons =
Function(`return function ${name}() {}`) ();
Cons.prototype[Symbol.toStringTag] = name;
Cons.prototype["run" + name] = k;
Cons.prototype.tag = name;
const o = new Cons();
Object.defineProperty(o, "value", {get: () => x});
return o;
};
const Some = x =>
tag("Option", x, def =>
tag("Option", x, k => k(x)));
const None = tag("Option", null, def =>
tag("Option", def, k => def));
const option = def => k => fx =>
fx.runOption(def).runOption(k);
const safeInc = option(0) (n => n + 1);
safeInc(Some(5)); // 6
safeInc(None); // 0
const xs = [Some("foo"), None]; // [Option, Option]
/*
expanded dev console display:
1: Option {value: ...} --> expands to "foo"
2: Otpion {value: ...} --> expands to null
*/
请注意,我对原型继承完全不感兴趣。
这种方法既繁琐又可能很慢,因为我应用了Function
构造函数,这使得代码的可预测性降低了。是否有更好的方法为咖喱函数提供类型(或在JS中为标签),从而消除列出的缺点?
答案 0 :(得分:1)
我略微改进了方法,摆脱了Function
调用和重复创建构造函数的过程。它适合于我的特定用例(函数编码类型),但是我无法以咖喱形式解决任意函数的更一般情况,因为它仍然很繁琐。无论如何,这里是:
const tag = Cons => (k, ...args) => {
const o = new Cons();
Object.defineProperties(o, {
"value": {get: () => args},
"runOpt": {value: k}});
return o;
};
const Opt = tag(
function Option() {
Option.prototype[Symbol.toStringTag] = "Option";
Option.prototype.tag = "Option";
});
const Some = x =>
Opt(def => Opt(k => k(x), x), x);
const None = Opt(def => Opt(k => def, def));
const option = def => k => fx =>
fx.runOpt(def).runOpt(k);
const safeInc = option(0) (n => n + 1);
safeInc(Some(5)); // 6
safeInc(None); // 0
Some(5); // Option {runOption}
[Some("foo"), None]; // [Option, Option]
/*
expanded dev console display:
1: Option {runOpt: f, value: ...} --> expands to ["foo"]
2: Otpion {runOpt: f, value: ...} --> expands to []
*/