public static string ToString <T>(this ISequence <T> @this, int n) { var iterator = @this.Begin(); return(string.Join(", ", Enumerable.Range(0, n - 1) .Select(x => iterator.Next()) .Select(x => x.ToString()).ToArray())); }
public static List <T> Take <T>(this ISequence <IOption <T> > @this, int n) { var iterator = @this.Begin(); return(Enumerable.Range(0, n - 1) .Select(x => iterator.Next()) .Where(option => option.HasValue) .Select(option => option.Value).ToList()); }
//public static List<T> Take<T>(this ISequence<T> @this, int n) //{ // var iterator = @this.Begin(); // return Enumerable.Range(1, n).Select(x => iterator.Next()).ToList(); //} public static T At <T>(this ISequence <T> @this, long n) { var iterator = @this.Begin(); for (long i = 0; i < n; i++) { iterator.Next(); } return(iterator.Next()); }
//public static ISequence<TResult> SelectMany<TSource, TCollection, TResult> // (this ISequence<TSource> source, // Func<TSource, ISequence<TCollection>> collectionSelector, // Func<TSource, TCollection, TResult> resultSelector) //{ // return new FunctionalSequence<TResult>(() => // { // long g = 0; // return () => // { // var pair = NtoN2Mapping.GetPair(g++); // var t = source.At(pair.I); // var cs = collectionSelector(t); // var c = cs.At(pair.J); // return resultSelector(t, c); // }; // }); //} //public static ISequence<Option<ISequenceGrouping<TKey, Option<TSource>>>> GroupBy<TSource, TKey>( // this ISequence<Option<TSource>> source, // Func<TSource, TKey> keySelector) //{ // return source.GroupBy(keySelector, t => t); //} // Helper function to zip two regular sequences together public static ISequence <TResult> Zip <TSource, TOther, TResult>( this ISequence <TSource> @this, ISequence <TOther> other, Func <TSource, TOther, TResult> mapFunc) { return(new FunctionalSequence <TResult>(() => { var tIter = @this.Begin(); var sIter = other.Begin(); return () => mapFunc(tIter.Next(), sIter.Next()); })); }
//public static ISequence<Option<T>> Where<T>(this ISequence<Option<T>> @this, Func<T, bool> filter) //{ // return @this.Select(t => filter(t) ? new Option<T>(t) : new Option<T>()); //} //public static ISequence<Option<T>> Where<T>(this ISequence<T> @this, Func<T, bool> filter) //{ // return @this.Select(t => filter(t) ? new Option<T>(t) : new Option<T>()); //} //public static ISequence<S> Select<S, T>(this ISequence<Option<T>> @this, Func<T, S> mapFunc) //{ // return @this.Where1(t => t.HasValue).Select(o => mapFunc(o.Value)); //} public static ISequence <IOption <T> > Where <T>(this ISequence <IOption <T> > @this, Func <T, bool> filter) { return(new FunctionalSequence <IOption <T> >(() => { var tIter = @this.Begin(); return () => { var t = tIter.Next(); return t.HasValue && filter(t.Value) ? t : Empty <T>(); }; })); }
//public static T At<T>(this ISequence<T> @this, long n) //{ // var iterator = @this.Begin(); // for (long i = 0; i < n - 1; i++) // { // iterator.Next(); // } // return iterator.Next(); //} public static ISequence <IOption <S> > Select <S, T>(this ISequence <IOption <T> > @this, Func <T, S> mapFunc) { return(new FunctionalSequence <IOption <S> >(() => { var tIter = @this.Begin(); return () => { var t = tIter.Next(); return t.HasValue ? FromValue(mapFunc(t.Value)) : Empty <S>(); }; })); }
public ISequenceIterator <TElement> Begin() { return(_sequnce.Begin()); }
public ISequenceIterator <S> Begin() { return(new SelectSequenceIterator <S, T>(_original.Begin(), _mapFunc)); }