/// <summary> /// Map the sequence using the function provided /// </summary> /// <typeparam name="B"></typeparam> /// <param name="f">Mapping function</param> /// <returns>Mapped sequence</returns> public Seq <B> Map <B>(Func <A, B> f) { IEnumerable <B> Yield() { foreach (var item in this) { yield return(f(item)); } } return(SeqEnumerable <B> .New(Yield())); }
/// <summary> /// Filter the items in the sequence /// </summary> /// <param name="f">Predicate to apply to the items</param> /// <returns>Filtered sequence</returns> public Seq <A> Filter(Func <A, bool> f) { IEnumerable <A> Yield() { foreach (var item in this) { if (f(item)) { yield return(item); } } } return(SeqEnumerable <A> .New(Yield())); }
/// <summary> /// Monadic bind (flatmap) of the sequence /// </summary> /// <typeparam name="B">Bound return value type</typeparam> /// <param name="bind">Bind function</param> /// <returns>Flatmapped sequence</returns> public Seq <C> SelectMany <B, C>(Func <A, Seq <B> > bind, Func <A, B, C> project) { IEnumerable <C> Yield() { foreach (var a in this) { foreach (var b in bind(a)) { yield return(project(a, b)); } } } return(SeqEnumerable <C> .New(Yield())); }
/// <summary> /// Monadic bind (flatmap) of the sequence /// </summary> /// <typeparam name="B">Bound return value type</typeparam> /// <param name="f">Bind function</param> /// <returns>Flatmapped sequence</returns> public Seq <B> Bind <B>(Func <A, Seq <B> > f) { IEnumerable <B> Yield() { foreach (var a in this) { foreach (var b in f(a)) { yield return(b); } } } return(SeqEnumerable <B> .New(Yield())); }
/// <summary> /// Append operator /// </summary> public static Seq <A> operator +(Seq <A> x, Seq <A> y) { IEnumerable <A> Yield(Seq <A> sx, Seq <A> sy) { foreach (var a in sx) { yield return(a); } foreach (var b in sy) { yield return(b); } } return(SeqEnumerable <A> .New(Yield(x, y))); }
public static Seq <A> Cons <A>(this A head, IEnumerable <A> tail) => SeqEnumerable <A> .New(head, tail);
public static Seq <A> createRange <A>(IEnumerable <A> items) => SeqEnumerable <A> .New(items);