Folktale / fantasyland可能没有按预期工作

时间:2018-05-24 14:52:39

标签: javascript functional-programming monads fantasyland folktale

阅读Frisbys guide to functional programming,目前正在讨论Maybe一章。在appendix中,本书建议使用folktalefantasyland

然而,在这两个库中,Maybe似乎没有像书中所描述的那样起作用。

const Maybe = require('folktale/maybe')
// const Maybe = require('fantasy-options')
const {
    flip, concat, toUpper, path, pathOr, match, prop
} = require('ramda')

console.log(
    Maybe.of('Malkovich Malkovich').map(match(/a/ig))
)
// Just(['a', 'a'])

Maybe.of(null).map(match(/a/ig))
//******************************
// TypeError: Cannot read property 'match' of null
//******************************
// Nothing

Maybe.of(
    { name: 'Boris' }
).map(prop('age')).map(add(10))
// Nothing

Maybe.of(
    { name: 'Dinah', age: 14 }
).map(prop('age')).map(add(10))
// Just(24)

在从本书复制的这个示例中,第一个语句正常工作,但第二个语句得到TypeError。这似乎完全违背了Maybe的目的。或者我误解了什么?

Example repl.it

1 个答案:

答案 0 :(得分:1)

更新:2019年8月

高兴地问了这个问题,最初我也对行为的差异感到惊讶。正如其他人的回答一样,它取决于Frisby Mostly Adequate Guide implementation的编码方式。 “不规则”的实现细节与isNothing的函数实现屏蔽使用value传递的空值或未定义的Maybe.of的方式有关:

get isNothing() {
    return this.$value === null || this.$value === undefined;
 }

如果您引用其他实现,则使用Maybe.of()创建Maybe确实可以让您为null传递undefinedJust案例的值并实际打印例如Maybe.Just({ value: null })

相反,在使用Folktale时,请使用Maybe创建Maybe.fromNullable(),这将根据输入的值分配JustNothing

这是所提供代码的有效版本:

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实现)。我从我强烈推荐的书Functional Programming In JavaScript by Luis Atencio中引用了此参考实现。他在第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';
    }
};