예제 #1
0
        public Value In()
        {
            var needle   = Arguments[0];
            var iterator = new NSIterator(GetGenerator());

            return(iterator.Any(value => needle.Compare(value) == 0));
        }
예제 #2
0
 public NSIteratorEnumerator(NSIterator iterator)
 {
     this.iterator = iterator;
     this.iterator.Reset();
     index      = -1;
     continuing = (v, i) => i < MAX_LOOP;
 }
예제 #3
0
        public static Value Group(INSGenerator generator, Arguments arguments)
        {
            var count = arguments[0].Int;

            if (count > 0)
            {
                var iterator = new NSIterator(generator);
                var array    = new Array();
                iterator.Reset();
                var value = iterator.Next();
                for (var i = 0; i < MAX_ARRAY && !value.IsNil; i += count)
                {
                    var subArray = new Array();
                    for (var j = 0; j < count && !value.IsNil; j++)
                    {
                        subArray.Add(value);
                        value = iterator.Next();
                    }

                    array.Add(value);
                    value = iterator.Next();
                }

                return(array);
            }

            using (var assistant = new ParameterAssistant(arguments))
            {
                var block = assistant.Block();
                if (block == null)
                {
                    return(ToArray(generator));
                }

                var hash = new AutoHash <string, List <Value> >
                {
                    Default        = DefaultType.Lambda,
                    DefaultLambda  = k => new List <Value>(),
                    AutoAddDefault = true
                };

                assistant.IteratorParameter();
                var iterator = new NSIterator(generator);
                iterator.Reset();
                foreach (var item in iterator)
                {
                    assistant.SetIteratorParameter(item);
                    var key = block.Evaluate().Text;
                    hash[key].Add(item);
                }

                var array = new Array();
                foreach (var item in hash)
                {
                    array[item.Key] = new Array(item.Value);
                }

                return(array);
            }
        }
예제 #4
0
        public static Value First(INSGenerator generator, Arguments arguments)
        {
            var iterator = new NSIterator(generator);

            iterator.Reset();
            var next = iterator.Next();

            return(next.IsNil ? new None() : new Some(next));
        }
예제 #5
0
 public NSInnerComprehension(Parameters parameters, Block generatorSource, IMaybe <Block> ifBlock)
 {
     this.parameters      = parameters;
     this.generatorSource = generatorSource;
     this.ifBlock         = ifBlock;
     iterator             = null;
     index  = -1;
     region = new Region();
 }
예제 #6
0
        public override void Reset()
        {
            var sourceIterator = new NSIterator(sourceGenerator);

            foreach (var item in sourceIterator)
            {
                Add(item);
            }
        }
예제 #7
0
        public void Reset()
        {
            index = 0;
            var value      = generatorSource.Evaluate().PossibleGenerator();
            var innerValue = value.Must().HaveValue().Force("Inner comprehension", () => "Source must be a generator");

            iterator = new NSIterator(innerValue);
            iterator.Reset();
            more = true;
        }
예제 #8
0
        public void Reset()
        {
            index = 0;
            var value = generatorSource.Evaluate().PossibleGenerator();

            Assert(value.IsSome, "Inner comprehension", "Source must be a generator");
            iterator = new NSIterator(value.Value);
            iterator.Reset();
            more = true;
        }
예제 #9
0
 public NSIteratorEnumerator(NSIterator iterator, Func <Value, int, bool> continuing)
     : this(iterator) => this.continuing = continuing;
예제 #10
0
        public static Value FoldL(INSGenerator generator, Arguments arguments)
        {
            var iterator = new NSIterator(generator);

            using var assistant = new ParameterAssistant(arguments);
            var block = assistant.Block();

            if (block == null)
            {
                return(NilValue);
            }

            iterator.Reset();

            assistant.TwoValueParameters();
            var initialFromArguments = arguments[0];
            var initialValue         = initialFromArguments.IsEmpty ? iterator.Next() : initialFromArguments;

            if (initialValue.IsNil)
            {
                return(initialValue);
            }

            var secondValue = iterator.Next();

            if (secondValue.IsNil)
            {
                return(initialValue);
            }

            assistant.SetParameterValues(initialValue, secondValue);
            var value  = block.Evaluate();
            var signal = Signal();

            if (signal == Breaking)
            {
                return(value);
            }

            switch (signal)
            {
            case ReturningNull:
                return(null);

            case Continuing:
                return(value);
            }

            var next = iterator.Next();

            if (next.IsNil)
            {
                return(value);
            }

            for (var i = 0; i < MAX_LOOP; i++)
            {
                assistant.SetParameterValues(value, next);
                value  = block.Evaluate();
                signal = Signal();
                if (signal == Breaking)
                {
                    break;
                }

                switch (signal)
                {
                case ReturningNull:
                    return(null);

                case Continuing:
                    continue;
                }

                next = iterator.Next();
                if (next.IsNil)
                {
                    return(value);
                }
            }

            return(value);
        }
예제 #11
0
        public void Add(Value value)
        {
            var iterator = new NSIterator(value.PossibleGenerator().FlatMap(g => g, () => new NSOneItemGenerator(value)));

            iterators.Enqueue(iterator);
        }
예제 #12
0
        public void Add(Value value)
        {
            var iterator = new NSIterator(value.PossibleGenerator().DefaultTo(() => new NSOneItemGenerator(value)));

            iterators.Enqueue(iterator);
        }