Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add slicing of sequences via System.Range #748

Open
atifaziz opened this issue May 15, 2020 · 1 comment
Open

Add slicing of sequences via System.Range #748

atifaziz opened this issue May 15, 2020 · 1 comment

Comments

@atifaziz
Copy link
Member

atifaziz commented May 15, 2020

I propose to add a new Elements extension that accepts System.Range and which will allow usage of the range operator .. in C# to succinctly express slicing with start and end offsets.

We already have Slice, which is why I propose to call this extension Elements. An overload of Slice would be confusing since it takes a count as the second argument as opposed to an index.

Prototype

public static IEnumerable<T> Elements<T>(this IEnumerable<T> source, Range range) =>
    ((range.Start.Value, range.Start.IsFromEnd), (range.End.Value, range.End.IsFromEnd)) switch
    {
        ((0, false), (0, true)) => source,
        ((var rs, false), (0, true)) => source.Skip(rs),
        ((0, false), (var re, true)) => source.SkipLast(re),
        ((var rs, true), (0, true)) => source.TakeLast(rs),
        ((var rs, false), (var re, false)) =>
            source.Index()
                  .SkipWhile(e => e.Key < rs)
                  .TakeWhile(e => e.Key < re)
                  .Select(e => e.Value),
        ((var rs, true), (var re, true)) =>
            source.TakeLast(rs)
                  .Take(rs - re),
        ((var rs, true), (var re, false)) =>
            source.Index()
                  .TakeLast(rs)
                  .TakeWhile(e => e.Key < re)
                  .Select(e => e.Value),
        ((var rs, false), (var re, true)) =>
            source.Index()
                  .CountDown(re, (e, cd) => cd is null ? (true, e) : (false, e))
                  .TakeWhile(e => e is (true, _))
                  .Choose(e => e)
                  .Skip(rs)
                  .Select(e => e.Value)
    };

Examples

var seq = Enumerable.Range(0, 10);

var q =
    from r in new[]
    {
        ..,
        5..,
        ..5,
        3..7,
        ..^3,
        ^3..,
        ^8..^2,
        ^8..6,
        2..^2,
        ..^20,
        20..,
    }
    select $"{r,6} = [{seq.Elements(r).ToDelimitedString(", ")}]";

foreach (var e in q)
    Console.WriteLine(e);

Output:

 0..^0 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
 5..^0 = [5, 6, 7, 8, 9]
  0..5 = [0, 1, 2, 3, 4]
  3..7 = [3, 4, 5, 6]
 0..^3 = [0, 1, 2, 3, 4, 5, 6]
^3..^0 = [7, 8, 9]
^8..^2 = [2, 3, 4, 5, 6, 7]
 ^8..6 = [2, 3, 4, 5]
 2..^2 = [2, 3, 4, 5, 6, 7]
0..^20 = []
20..^0 = []
@atifaziz
Copy link
Member Author

There is also a proposal from @Dixin to add something like this to .NET eventually. See dotnet/runtime#28776.

@atifaziz atifaziz changed the title Add slicing sequences via System.Range Add slicing of sequences via System.Range May 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant