Skip to content

Instantly share code, notes, and snippets.

@einarwh
Created October 4, 2011 23:44
Show Gist options
  • Save einarwh/1263173 to your computer and use it in GitHub Desktop.
Save einarwh/1263173 to your computer and use it in GitHub Desktop.
An infinite sequence repeating another sequence.
public class BrokenRecord<T> : IEnumerable<T>
{
private readonly IEnumerable<T> _seq;
public BrokenRecord(IEnumerable<T> seq)
{
_seq = seq;
}
public IEnumerator<T> GetEnumerator()
{
var e = _seq.GetEnumerator();
return new BrokenRecordEnumerator<T>(e);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public class BrokenRecordEnumerator<T> : IEnumerator<T>
{
private readonly IEnumerator<T> _seq;
public BrokenRecordEnumerator(IEnumerator<T> seq)
{
_seq = seq;
}
public void Dispose() {}
private bool ResetAndMoveNext()
{
Reset();
return _seq.MoveNext();
}
public bool MoveNext()
{
return _seq.MoveNext() || ResetAndMoveNext();
}
public void Reset()
{
_seq.Reset();
}
public T Current
{
get
{
return _seq.Current;
}
}
object IEnumerator.Current
{
get { return Current; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment