Здравствуйте, Serginio1, Вы писали:
S>На yield построен Linq
S>Непонятно?
Как тебе такой linq без yield и даже без IEnumerable?
static void Main()
{
var n =
from c in 1.ToMaybe()
from s in "2".ToMaybe()
from x in 2.ToMaybe()
select s + c + x;
Console.WriteLine(n);
}
abstract class Maybe<T>
{
public class Nothing : Maybe<T> {}
public class Just : Maybe<T>
{
public T Value;
}
public override string ToString() => this switch
{
Just j => j.Value.ToString(),
_ => "Nothing"
};
}
static class Extensions
{
public static Maybe<T> ToMaybe<T>(this T value)
{
return value == null ? (Maybe<T>)new Maybe<T>.Nothing() : new Maybe<T>.Just { Value = value };
}
public static Maybe<T2> SelectMany<T1,T2>(
this Maybe<T1> source, Func<T1,Maybe<T2>> selector)
{
if (source is Maybe<T1>.Just j) return selector(j.Value);
return new Maybe<T2>.Nothing();
}
public static Maybe<T3> SelectMany<T1,T2,T3>(
this Maybe<T1> source, Func<T1,Maybe<T2>> collectionSelector, Func<T1,T2,T3> resultSelector)
{
return source.SelectMany(x => collectionSelector(x).SelectMany(y => resultSelector(x, y).ToMaybe()));
}
}