using System; using System.Collections; using System.Collections.Generic; using System.Runtime.CompilerServices; namespace Immutable { public class PureImmutableEnumerator : IImmutableEnumerator { private T state; private IEqF>>> generator; private int hashCode; public PureImmutableEnumerator(T state, IEqF>>> generator) { this.state = state; this.generator = generator; this.hashCode = Equality.HashCode("PureImmutableEnumerator", state, generator); } public static bool operator ==(PureImmutableEnumerator a, PureImmutableEnumerator b) => Equality.Operator(a, b); public static bool operator !=(PureImmutableEnumerator a, PureImmutableEnumerator b) => !(a == b); public override bool Equals(object other) => Equality.Untyped( this, other, x => x as PureImmutableEnumerator, x => x.hashCode, // Two immutable enumerators are equal if and only if // they have the same (immutable) state and use the same // generator lambda. x => x.state, x => x.generator); public bool Equals(IImmutableEnumerator other) => Equality.Equatable>(this, other); public override int GetHashCode() => hashCode; public override string ToString() => "ImmutableEnumerator"; public IEnumerator GetEnumerator() => this.ToIEnumerable().GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => this.ToIEnumerable().GetEnumerator(); void IDisposable.Dispose() { /* Nothing to do */ } public Option> MoveNext() => generator.F(state).IfSome((first, rest) => new PureImmutableEnumeratorElement(first, rest)); } }