Ejemplo n.º 1
0
        public static IEnumerable <T> ToEnumerable <T>(this Enumerate <T> source)
        {
            var        e = source();
            IMaybe <T> value;

            while (!((value = e()) is None <T>))
            {
                yield return(value.Value);
            }
        }
Ejemplo n.º 2
0
        public static List <T> ToList <T>(this Enumerate <T> source)
        {
            var        list = new List <T>();
            var        e    = source();
            IMaybe <T> value;

            while (!((value = e()) is None <T>))
            {
                list.Add(value.Value);
            }

            return(list);
        }
Ejemplo n.º 3
0
        public static Enumerate <TResult> SelectMany <T, TResult>(this Enumerate <T> source, Func <T, Enumerate <TResult> > selector)
        {
            return(() =>
            {
                var e = source();
                IMaybe <T> lastOuter = new None <T>();
                IMaybe <TResult> lastInner = new None <TResult>();
                Enumeration <TResult> innerSet = null;
                return () =>
                {
                    do
                    {
                        while (lastInner is None <TResult> )
                        {
                            lastOuter = e();

                            if (!lastOuter.HasValue)
                            {
                                return lastOuter.TransposeEmpty <TResult>();
                            }

                            innerSet = selector(lastOuter.Value)();
                            lastInner = innerSet();
                            if (lastInner is Some <TResult> )
                            {
                                return lastInner;
                            }
                        }

                        lastInner = innerSet();
                    }while (lastInner is None <TResult>);

                    return lastInner;
                };
            });
        }
Ejemplo n.º 4
0
 public static Enumerate <T> Where <T>(this Enumerate <T> source, Func <T, bool> filter)
 {
     return(source.SelectMany(t => filter(t) ? Return(t) : Empty <T>()));
 }
Ejemplo n.º 5
0
 public static Enumerate <TResult> Select <T, TResult>(this Enumerate <T> source, Func <T, TResult> selector)
 {
     return(source.SelectMany(a => Return(selector(a))));
 }