public Persist(IEnumerable <Expression> initialValue, NewValueFunction newValue) { State = initialValue.Select((v, i) => new State(i, 0, v)).ToList().AsReadOnly(); NewValue = new ReadOnlyCollection <Expression>(newValue(State).ToArray()); if (NewValue.Any(e => e == null)) { throw new ArgumentException("An operand was null"); } if (State.Count != NewValue.Count) { throw new ArgumentException("Collection counts were different"); } // Increment the scope number if there's nested loops var scope = NewValue.SelectMany(n => n.AllDependent).OfType <State>().OrderBy(s => s.Scope).FirstOrDefault(); if (scope != null) { State = initialValue.Select((v, i) => new State(i, scope.Id + 1, v)).ToList().AsReadOnly(); NewValue = new ReadOnlyCollection <Expression>(newValue(State).ToArray()); } }
public Loop(IEnumerable <Expression> initialValue, ConditionFunction condition, NewValueFunction body) { if (condition == null) { throw new ArgumentNullException(nameof(condition)); } State = initialValue.Select((v, i) => new State(i, 0, v)).ToList().AsReadOnly(); Body = new ReadOnlyCollection <Expression>(body(State).ToArray()); Condition = condition(State); if (Body.Any(e => e == null)) { throw new ArgumentException("An operand was null"); } if (State.Count != Body.Count) { throw new ArgumentException("Collection counts were different"); } // Increment the scope number if there's nested loops var scope = Body.SelectMany(n => n.AllDependent) .Concat(Condition.AllDependent) .OfType <State>() .OrderBy(s => s.Scope) .FirstOrDefault(); if (scope != null) { State = initialValue.Select((v, i) => new State(i, scope.Id + 1, v)).ToList().AsReadOnly(); Body = new ReadOnlyCollection <Expression>(body(State).ToArray()); Condition = condition(State); } }