更新:2019 年 8 月
很高兴你问这个问题,我最初也对行为上的差异感到惊讶。正如其他人所回应的那样,这归结为Frisby Mostly Adequate Guide 实施的编码方式。“不规则”实现细节与isNothing
s 函数实现屏蔽value
传入 using的 null 或 undefined 的方式有关Maybe.of
:
get isNothing() {
return this.$value === null || this.$value === undefined;
}
如果您参考其他实现 - 然后使用Maybe.of()
创建您的Maybe
确实允许您传递 a null
orundefined
为Just
案例的值并实际打印例如Maybe.Just({ value: null })
相反,当使用 Folktale 时,创建将根据输入的值分配 a或的Maybe
using 。Maybe.fromNullable()
Just
Nothing
这是提供的代码的工作版本:
const Maybe = require("folktale/maybe");
const {
flip,
concat,
toUpper,
path,
pathOr,
match,
prop,
add
} = require("ramda");
console.log(Maybe.of("Malkovich Malkovich").map(match(/a/gi)));
//-> folktale:Maybe.Just({ value: ["a", "a"] })
console.log(Maybe.fromNullable(null).map(match(/a/gi)));
//-> folktale:Maybe.Nothing({ })
最后,这里有一个 Maybe 的演示实现,经过编码使用fromNullable
(类似于 Folktale 实现)。我从我认为强烈推荐的书 - Luis Atencio 的 JavaScript 函数式编程中获取了这个参考实现。他在第 5 章的大部分时间里都清楚地解释了这一点。
/**
* Custom Maybe Monad used in FP in JS book written in ES6
* Author: Luis Atencio
*/
exports.Maybe = class Maybe {
static just(a) {
return new exports.Just(a);
}
static nothing() {
return new exports.Nothing();
}
static fromNullable(a) {
return a !== null ? Maybe.just(a) : Maybe.nothing();
}
static of(a) {
return Maybe.just(a);
}
get isNothing() {
return false;
}
get isJust() {
return false;
}
};
// Derived class Just -> Presence of a value
exports.Just = class Just extends exports.Maybe {
constructor(value) {
super();
this._value = value;
}
get value() {
return this._value;
}
map(f) {
return exports.Maybe.fromNullable(f(this._value));
}
chain(f) {
return f(this._value);
}
getOrElse() {
return this._value;
}
filter(f) {
exports.Maybe.fromNullable(f(this._value) ? this._value : null);
}
get isJust() {
return true;
}
toString () {
return `Maybe.Just(${this._value})`;
}
};
// Derived class Empty -> Abscense of a value
exports.Nothing = class Nothing extends exports.Maybe {
map(f) {
return this;
}
chain(f) {
return this;
}
get value() {
throw new TypeError("Can't extract the value of a Nothing.");
}
getOrElse(other) {
return other;
}
filter() {
return this._value;
}
get isNothing() {
return true;
}
toString() {
return 'Maybe.Nothing';
}
};