gpt4 book ai didi

c# - 在 C# 中实现选择类型

转载 作者:行者123 更新时间:2023-11-30 20:24:05 25 4
gpt4 key购买 nike

出于教育原因,我正在尝试在 C# 中实现 F# 的 Choice 和 Option Type。这是受“真实世界函数式编程”一书和一些博客文章的启发,例如: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>();
}
}

选项类型如下所示:

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)));
}
}

这里是选择类型的实现:

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);
}
}

编辑:

它实际上适用于下面的扩展。我不太喜欢的是此实现向 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)));
}

最佳答案

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

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);
}

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);
}

对于 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);
}

您可能还需要一个 BiSelect 函数来映射两种类型参数:

public abstract BiSelect<T3, T4>(Func<T1, T3> ff, Func<T2, T4> fs);

如果你想将 SelectMany 与 linq 查询语法一起使用,你需要实现另一个重载,如下所示:

public abstract Choice<T1, T4> SelectMany<T3, T4>(Func<T2, Choice<T1, T3>> f, Func<T2, T3, T4> selector);

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);
}

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));
}

你可以这样做:

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

关于c# - 在 C# 中实现选择类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27469077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com