Runtime: Enumerable.Repeat should allow for infinite repetition

Created on 2 Dec 2018  路  2Comments  路  Source: dotnet/runtime

Enumerable.Repeat should have an overload with the following signature:

public static System.Collections.Generic.IEnumerable Repeat (TResult element);

The behavior would be repeating the value infinitely to allow composing lazy sequences without having to provide int.MaxValue or similar workarounds (which have the obvious limitation of having a bound).

~cs
static class Example
{
public static IEnumerable ToBinary(uint i) => Enumerable.Repeat(i)
.Select((num, shift) => num >> shift)
.TakeWhile(x => x != 0)
.Select(x => (x & 1) == 1);
}
~

api-needs-work api-suggestion area-System.Runtime

Most helpful comment

As rather uncommon usecase I think this shouldn't get into corefx.

But you can write such a method quite simple:
c# public static IEnumerable<T> RepeatEndless<T>(T element) { while (true) { yield return element; } }

BTW: see API Review Process

All 2 comments

As rather uncommon usecase I think this shouldn't get into corefx.

But you can write such a method quite simple:
c# public static IEnumerable<T> RepeatEndless<T>(T element) { while (true) { yield return element; } }

BTW: see API Review Process

I think the less common use case is actually the Repeat(x, y), because it's a special case of Repeat(x) as the former could be implemented with idiomatic LINQ:

~cs
var _ = Enumerable.Repeat(x).Take(y);
~

Of course it is trivial to implement it with a generator, but that breaks the flow of a functional style C# program.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yahorsi picture yahorsi  路  3Comments

v0l picture v0l  路  3Comments

jamesqo picture jamesqo  路  3Comments

Timovzl picture Timovzl  路  3Comments

jzabroski picture jzabroski  路  3Comments