Example #1
0
        public override Value Evaluate(NSIterator iterator, Lambda lambda)
        {
            var found = false;

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

            for (var i = 0; !value.IsNil && i < MAX_LOOP; i++)
            {
                var result = lambda.Invoke(new Arguments(value));
                if (result.IsTrue)
                {
                    if (found)
                    {
                        return(false);
                    }

                    found = true;
                }

                value = iterator.Next();
            }

            return(found);
        }
Example #2
0
        public static string Iterate(INSGenerator generator, Parameters parameters, Block block)
        {
            var iterator = new NSIterator(generator);
            var index    = 0;

            using var popper = new RegionPopper(new Region(), "for");
            popper.Push();
            iterator.Reset();
            for (var i = 0; i < MAX_LOOP; i++)
            {
                index = i;
                var next = iterator.Next();
                if (next.IsNil)
                {
                    break;
                }

                parameters.SetValues(next, i);
                block.Evaluate();
                var signal = Signal();
                if (signal == Breaking || signal == ReturningNull)
                {
                    break;
                }
            }

            return(index == 1 ? "1 iteration" : $"{index} iterations");
        }
Example #3
0
        public override Value Evaluate()
        {
            var stack = State.Stack;
            var right = stack.Pop(true, LOCATION);
            var left  = stack.Pop(true, LOCATION);

            var leftGenerator  = left.PossibleGenerator().Must().HaveValue().Force(LOCATION, () => $"{left} is not a generator");
            var rightGenerator = right.PossibleGenerator().Must().HaveValue().Force(LOCATION, () => $"{right} is not a generator");

            var leftIterator  = new NSIterator(leftGenerator);
            var rightIterator = new NSIterator(rightGenerator);
            var outerArray    = new Array();

            var outerValue = leftIterator.Next();

            for (var i = 0; !outerValue.IsNil && i < MAX_ARRAY; i++)
            {
                rightIterator.Reset();
                var innerValue = rightIterator.Next();
                for (var j = 0; !innerValue.IsNil && j < MAX_ARRAY; j++)
                {
                    var innerArray = new Array {
                        outerValue, innerValue
                    };
                    outerArray.Add(modifyInnerArray(innerArray));
                    innerValue = rightIterator.Next();
                }

                outerValue = leftIterator.Next();
            }

            return(outerArray);
        }
Example #4
0
        public virtual Value Evaluate()
        {
            var value    = arguments[0];
            var iterator = new NSIterator(generator);

            iterator.Reset();

            var block = arguments.Executable;

            if (block != null && block.Count > 0)
            {
                return(EvaluateAsLambda(iterator, arguments.Parameters, block));
            }

            for (var i = 0; i < MAX_LOOP; i++)
            {
                var current = iterator.Next();
                if (current.IsNil)
                {
                    return(IfNil());
                }
                if (Compare(current, value))
                {
                    var success = Success();
                    if (success.IsSome)
                    {
                        return(success.Value);
                    }
                }
            }
            return(Final());
        }
Example #5
0
        public static Array ToArray(INSGenerator generator)
        {
            var array    = new Array();
            var iterator = new NSIterator(generator);

            iterator.Reset();
            for (var i = 0; i < MAX_ARRAY; i++)
            {
                var next = iterator.Next();
                if (next.IsNil)
                {
                    break;
                }

                array.Add(next);
            }

            return(array);
        }