2

我正在使用 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;
        }
    }
}
4

2 回答 2

3

我建议在构建这些东西时参考幻想世界规范。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

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

于 2019-07-11T19:44:57.160 回答
1

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

JS文档

更喜欢

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

用于单行 JSDoc 注释和

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

对于多行的

其他实现

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

于 2014-07-14T23:39:55.787 回答