在 C# 中实现选择类型

lei*_*ann 3 c# linq monads f# functional-programming

出于教育原因,我尝试在 C# 中实现 F# 中的选择和选项类型。这是受到《真实世界函数式编程》一书和一些博客文章的启发,例如:http: //bugsquash.blogspot.de/2011/08/refactoring-to-monadic-c-applicative.htmlhttp://tomasp。 net/blog/idioms-in-linq.aspx/

我想让它工作,但我不知道如何实现选择类型的扩展(Bind、Map、SelectMany,...):

public static void Division()
{
    Console.WriteLine("Enter two (floating point) numbers:");

    (
        from f1 in ReadDouble().ToChoice("Could not parse input to a double.")
        from f2 in ReadDouble().ToChoice("Could not parse input to a double.")
        from result in Divide(f1, f2).ToChoice("Cannot divide by zero.")
        select result
    )
        .Match(
            x => Console.WriteLine("Result = {0}", x),
            x => Console.WriteLine("Error: {0}", x));
}

public static Option<double> Divide(double a, double b)
{
    return b == 0 ? Option.None<double>() : Option.Some(a / b);
}

public static Option<Double> ReadDouble()
{
    double i;
    if (Double.TryParse(Console.ReadLine(), out i))
        return Option.Some(i);
    else
        return Option.None<double>();
}

    public static Option<int> ReadInt()
    {
        int i;
        if (Int32.TryParse(Console.ReadLine(), out i))
            return Option.Some(i);
        else
            return Option.None<int>();
    }
}
Run Code Online (Sandbox Code Playgroud)

选项类型如下所示:

public enum OptionType
{
    Some, None
}

public abstract class Option<T>
{
    private readonly OptionType _tag;

    protected Option(OptionType tag)
    {
        _tag = tag;
    }

    public OptionType Tag { get { return _tag; } }

    internal bool MatchNone()
    {
        return Tag == OptionType.None;
    }

    internal bool MatchSome(out T value)
    {
        value = Tag == OptionType.Some ? ((Some<T>)this).Value : default(T);
        return Tag == OptionType.Some;
    }

    public void Match(Action<T> onSome, Action onNone)
    {
        if (Tag == OptionType.Some)
            onSome(((Some<T>)this).Value);
        else
            onNone();
    }

    public Choice<T, T2> ToChoice<T2>(T2 value)
    {
        if (Tag == OptionType.Some)
        {
            T some;
            MatchSome(out some);
            return Choice.NewChoice1Of2<T, T2>(some);
        }
        else
            return Choice.NewChoice2Of2<T, T2>(value);
    }
}

internal class None<T> : Option<T>
{
    public None() : base(OptionType.None) { }
}

internal class Some<T> : Option<T>
{
    public Some(T value)
        : base(OptionType.Some)
    {
        _value = value;
    }
    private readonly T _value;
    public T Value { get { return _value; } }
}

public static class Option
{
    public static Option<T> None<T>()
    {
        return new None<T>();
    }
    public static Option<T> Some<T>(T value)
    {
        return new Some<T>(value);
    }
}

public static class OptionExtensions
{
    public static Option<TResult> Map<T, TResult>(this Option<T> source, Func<T, TResult> selector)
    {
        T value;
        return source.MatchSome(out value) ? Option.Some(selector(value)) : Option.None<TResult>();
    }

    public static Option<TResult> Bind<T, TResult>(this Option<T> source, Func<T, Option<TResult>> selector)
    {
        T value;
        return source.MatchSome(out value) ? selector(value) : Option.None<TResult>();
    }

    public static Option<TResult> Select<T, TResult>(this Option<T> source, Func<T, TResult> selector)
    {
        return source.Map(selector);
    }

    public static Option<TResult> SelectMany<TSource, TValue, TResult>(this Option<TSource> source, Func<TSource, Option<TValue>> valueSelector, Func<TSource, TValue, TResult> resultSelector)
    {
        return source.Bind(s => valueSelector(s).Map(v => resultSelector(s, v)));
    }
}
Run Code Online (Sandbox Code Playgroud)

这是选择类型的实现:

public enum ChoiceType { Choice1Of2, Choice2Of2 };

public abstract class Choice<T1, T2>
{
    private readonly ChoiceType _tag;

    protected Choice(ChoiceType tag)
    {
        _tag = tag;
    }

    public ChoiceType Tag { get { return _tag; } }

    internal bool MatchChoice1Of2(out T1 value)
    {
        value = Tag == ChoiceType.Choice1Of2 ? ((Choice1Of2<T1, T2>)this).Value : default(T1);
        return Tag == ChoiceType.Choice1Of2;
    }

    internal bool MatchChoice2Of2(out T2 value)
    {
        value = Tag == ChoiceType.Choice2Of2 ? ((Choice2Of2<T1, T2>)this).Value : default(T2);
        return Tag == ChoiceType.Choice2Of2;
    }

    public void Match(Action<T1> onChoice1Of2, Action<T2> onChoice2Of2)
    {
        if (Tag == ChoiceType.Choice1Of2)
            onChoice1Of2(((Choice1Of2<T1, T2>)this).Value);
        else
            onChoice2Of2(((Choice2Of2<T1, T2>)this).Value);
    }
}

internal class Choice1Of2<T1, T2> : Choice<T1, T2>
{
    public Choice1Of2(T1 value)
        : base(ChoiceType.Choice1Of2)
    {
        _value = value;
    }
    private readonly T1 _value;

    public T1 Value { get { return _value; } }
}

internal class Choice2Of2<T1, T2> : Choice<T1, T2>
{
    public Choice2Of2(T2 value)
        : base(ChoiceType.Choice2Of2)
    {
        _value = value;
    }
    private readonly T2 _value;

    public T2 Value { get { return _value; } }
}

public static class Choice
{
    public static Choice<T1, T2> NewChoice1Of2<T1, T2>(T1 value)
    {
        return new Choice1Of2<T1, T2>(value);
    }

    public static Choice<T1, T2> NewChoice2Of2<T1, T2>(T2 value)
    {
        return new Choice2Of2<T1, T2>(value);
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

它实际上可以与下面的扩展一起使用。我真正不喜欢的是这个实现向 Choice 类型添加了上下文特定的行为。这是因为 Choice1Of2 是首选选择,因为所有扩展方法主要对它进行操作,而不是对 Choice2Of2 或两者进行操作。(但这就是消费代码实际上意味着什么,所以我想这是让它工作的唯一方法。)

public static Choice<TResult, T2> Map<T1, T2, TResult>(this Choice<T1, T2> source, Func<T1, TResult> selector)
{
    T1 value1;
    if(source.MatchChoice1Of2(out value1))
    {
        return Choice.NewChoice1Of2<TResult, T2>(selector(value1));
    }

    T2 value2;
    if (source.MatchChoice2Of2(out value2))
    {
        return Choice.NewChoice2Of2<TResult, T2>(value2);
    }

    throw new InvalidOperationException("source (:Choice) has no value.");
}

public static Choice<TResult, T2> Bind<T1, T2, TResult>(this Choice<T1, T2> source, Func<T1, Choice<TResult, T2>> selector)
{
    T1 value1;
    if (source.MatchChoice1Of2(out value1))
    {
        return selector(value1);
    }

    T2 value2;
    if (source.MatchChoice2Of2(out value2))
    {
        return Choice.NewChoice2Of2<TResult, T2>(value2);
    }

    throw new InvalidOperationException("source (:Choice) has no value.");
}

public static Choice<TResult, T2> Select<T1, T2, TResult>(this Choice<T1, T2> source, Func<T1, TResult> selector)
{
    return source.Map(selector);
}

public static Choice<TResult, T2> SelectMany<TSource, TValue, T2, TResult>(this Choice<TSource, T2> source, Func<TSource, Choice<TValue, T2>> valueSelector, Func<TSource, TValue, TResult> resultSelector)
{
    return source.Bind(s => valueSelector(s).Map(v => resultSelector(s, v)));
}
Run Code Online (Sandbox Code Playgroud)

Lee*_*Lee 5

由于Choice有两个类型参数,您需要修复第一个参数才能写入Selectand SelectMany(绑定):

public abstract class Choice<T1, T2>
{
    public abstract Choice<T1, T3> Select<T3>(Func<T2, T3> f);
    public abstract Choice<T1, T3> SelectMany<T3>(Func<T2, Choice<T1, T3>> f);
}
Run Code Online (Sandbox Code Playgroud)

他们的实施很简单Choice1Of2

public override Choice<T1, T3> Select<T3>(Func<T2, T3> f)
{
    return new Choice1Of2<T1, T3>(this._value);
}

public override Choice<T1, T3> SelectMany<T3>(Func<T2, Choice<T1, T3>> f)
{
    return new Choice1Of2<T1, T3>(this._value);
}
Run Code Online (Sandbox Code Playgroud)

因为Choice2Of2您只需要为给定函数提供内部值:

public override Choice<T1, T3> Select<T3>(Func<T2, T3> f)
{
    return new Choice2Of2<T1, T3>(f(this.Value));
}
public override Choice<T1, T3> SelectMany<T3>(Func<T2, Choice<T1, T3>> f)
{
    return f(this._value);
}
Run Code Online (Sandbox Code Playgroud)

您可能还需要一个BiSelect用于映射两个类型参数的函数:

public abstract BiSelect<T3, T4>(Func<T1, T3> ff, Func<T2, T4> fs);
Run Code Online (Sandbox Code Playgroud)

如果要使用SelectManylinq 查询语法,则需要实现另一个重载,如下所示:

public abstract Choice<T1, T4> SelectMany<T3, T4>(Func<T2, Choice<T1, T3>> f, Func<T2, T3, T4> selector);
Run Code Online (Sandbox Code Playgroud)

的实现Choice1Of2与之前类似:

public override Choice<T1, T4> SelectMany<T3, T4>(Func<T2, Choice<T1, T3>> f, Func<T2, T3, T4> selector)
{
    return new Choice1Of2<T1, T4>(this._value);
}
Run Code Online (Sandbox Code Playgroud)

那么的实现Choice2Of2是:

public override Choice<T1, T4> SelectMany<T3, T4>(Func<T2, Choice<T1, T3>> f, Func<T2, T3, T4> selector)
{
    T2 val = this._value;
    var e = f(val);
    return e.Select(v => selector(val, v));
}
Run Code Online (Sandbox Code Playgroud)

你可以这样做:

var choice = from x in new Choice2Of2<string, int>(1)
             from y in new Choice2Of2<string, int>(4)
             select x + y;
Run Code Online (Sandbox Code Playgroud)