【问题标题】:Maybe monad in TypeScript可能是 TypeScript 中的 monad
【发布时间】:2014-07-14 16:46:22
【问题描述】:

我正在使用 TypeScript 中的 Maybe monad。我对这些概念的理解不是很好,所以我欢迎任何反馈或建议。有很多 JS 示例,但我正在寻找更强大的类型。

用法如下:

var maybe = new Example.Maybe({ name: "Peter", age: 21 })
    .ifF(p => p.age === 100)            // is the person 100?
    .returnF(p => p.name, "Unknown")    // extract Maybe string of name
    .getValue();                        // unwrap the string from the Maybe

因为 if/with/return 是保留字,所以我刚刚在末尾添加了 F..

到目前为止我有:

module Example
{
    export class Maybe<T>
    {
        private val: T;
        private hasv: boolean;

        private static none = new Maybe(null);

        constructor(value: T)
        {
            this.val = value;
            this.hasv = (value != null);
        }

        /** True if Maybe contains a non-empty value
        */
        hasValue(): boolean
        {
            return this.hasv;
        }

        /** Returns non-empty value, or null if empty
        */
        getValue(): T
        {
            if (this.hasv) return this.val;
            return null;
        }

        /** Turns this maybe into Maybe R or Maybe NONE if there is no value
        */
        withF<R>(func: (v: T) => R) : Maybe<R>
        {
            if (!this.hasv) return Maybe.none;
            return new Maybe<R>(func(this.val));
        }

        /** Turns this maybe into Maybe R or Maybe DefaultVal if there is no value
        */
        returnF<R>(func: (v: T) => R, defaultval: R): Maybe<R>
        {
            if (!this.hasv) return new Maybe(defaultval);
            return new Maybe<R>(func(this.val));
        }

        /** If func is true, return this else return Maybe NONE
        */
        ifF(func: (v: T) => boolean): Maybe<T>
        {
            if (!this.hasv) return Maybe.none;
            if (func(this.val))
                return this;
            else
                return Maybe.none;
        }
    }
}

【问题讨论】:

    标签: typescript monads maybe


    【解决方案1】:

    我建议在构建这些东西时参考幻想世界规范。 WithF一般称为filter,returnF/withF可以用map/chain代替。 orJust 是另一种方法,如果您的流程没有返回任何内容,它允许您提供默认值。

    var maybe = new Example.Maybe({ name: "Peter", age: 21 })
        .filter(p => p.age === 100)
        .map(p => p.name)    
        .orJust('unknown');  
    

    https://github.com/fantasyland/fantasy-land

    当您根据规范实现功能时,需要考虑的事情要少得多。您还可以相当容易地切换到不同的数据类型(例如数组/列表),因为这些方法也将存在于新类型上。

    【讨论】:

    • 查看原始函数名称时,不太确定它们在做什么,因此我不得不阅读它们的实现以了解它们的工作。使用您的代码,即使不查看它们的实现,也很清楚它们在做什么。干得好!
    【解决方案2】:

    你的代码很好。只有一些小建议。

    JS 文档

    喜欢

    /** True if Maybe contains a non-empty value */
    

    对于单行 JSDoc cmets 和

      /** 
        * True if Maybe contains a non-empty value
        */
    

    对于多行的

    其他实现

    还有一个 codeplex 问题:https://typescript.codeplex.com/workitem/2585 与实现:https://gist.github.com/khronnuz/1ccec8bea924fe98220e

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-04
      • 2022-01-14
      • 1970-01-01
      相关资源
      最近更新 更多