首页 文章

也许是jad解释中的monad示例代码

提问于
浏览
0

我正在开始或尝试学习函数式编程monad .

所以第一个是可能 . 我试图用monad转换代码 .

function(fieldName, vals, fields) {
    var newValue = vals[fieldName];
    if (typeof(newValue) == 'undefined') {
        var elemFrom = fields[fieldName];
        if (elemFrom) {
            newValue = fields[fieldName]
        }
    }
    if (typeof (newValue) != 'undefined') {
        return newValue
    }
}

在这里,我有一堆未定义的检查,我认为很好用monay .

我的问题是我读到你将值传递给了monad和map函数 .

但是在我的情况下,我替换monad中的值 .

如果我传递null,则map方法将不会,因为值未定义 .

我没有使用框架,我想要简单的实现,所以我可以理解它 .

我应该在monad类(函数)中添加“else”方法 .

我有相反的情况“如果 Value 未定义,做一些事情”

你能建议如何解决这个问题吗?

谢谢

1 回答

  • 0

    所以你发布的功能可以改写为

    const f = (a, b, c) => b[a] === undefined ? c[a] : b[a];
    

    我不清楚这需要一个功能,而不是在你想要使用相关对象属性的任何地方内联,但也许你部分应用它或什么,我不是在评判 .

    至于Maybe,一个(非常简单的)实现可能看起来像这样:

    class Maybe {
      static of (value) {
        return new Maybe(value);
      }
    
      // Proper solution here should be recursive to handle
      // nesting properly, but I'm lazy
      static equals (a, b) {
        return a.chain(x => x) === b.chain(x => x);
      }
    
      constructor(value) {
        this._value = value;
      }
    
      map (f) {
        // Does not distinguish null from undefined, but YMMV. Note
        // that if the Maybe value is null or undefined we never touch
        // f, that's the null propagation thing.
        return this._value == null ? this : new Maybe(f(this._value));
      }
    
      chain (f) {
        // We'll assume presence of .chain means it's a Monad
        const value = this._value && typeof this._value.chain === 'function' ?
          this._value.chain(x => x) :
          this._value;
    
        return f(value);
      }
    }
    

    现在我们可以测试它是否符合Monad法则:

    const a = 3;
    const f = x => x * x;
    Maybe.of(a).chain(f) === f(a) // left identity
    Maybe.equals(Maybe.of(5).chain(Maybe.of), Maybe.of(5)); // right identity
    

    而且这是一个有效的Functor

    Maybe.equals(Maybe.of(3).map(x => x), Maybe.of(3)); // identity
    Maybe.equals(                                       // composition
      Maybe.of(3).map(x => x + 2).map(x => x * 3), 
      Maybe.of(3).map(compose(x => x * 3, x => x + 2))
    );
    

    甜 .

    所以,现在,你的功能 . 它将被重写为

    const f = (a, b, c) => {
      return b[a] === undefined ? Maybe.of(c[a]) : Maybe.of(b[a]);
    }
    

    也许你现在看到我混淆的原因,也许这并不是真的在这里拯救你 . 但是,如果我使用Maybe,我会重写整个这样的事情:

    const or = (a, b) => {
      return Maybe.of(a == null ? b : a);
    }
    

    然后我会传入属性访问:

    const obj1 = { a: 2, c: 3 };
    const obj2 = { b: 4 };
    const prop = "a"
    const result = or(obj1["prop"], obj2["prop"]); // Maybe(2)
    

    更新

    感谢@Bergi提醒我有关Alternative的评论 . 你可以在上面的Maybe类中添加一个方法,如下所示:

    alt (x) {
      if (!(x instanceof Maybe)) {
        throw new TypeError("Expected a Maybe");
      }
      return this.chain(x => x) == null ? x : this;
    }
    
    // semantics
    
    Maybe.of(null).alt(Maybe.of(3)); // Maybe(3)
    Maybe.of(2).alt(Maybe.of(4));    // Maybe(2)
    
    // usage
    Maybe.of(obj1[prop]).alt(Maybe.of(obj2[prop]));
    

    请注意,这不需要零/空方法)但您可以阅读herehere以获取更多详细信息 . 这可能是您发布的功能的最佳替代品 .

相关问题