Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lazily Transforming IReadOnlyList in C#

Tags:

c#

list

I would like to be able to do something like this for setting up a mesh data structure.

IReadOnlyList<Point> points;
IReadOnlyList<IReadOnlyList<int>> triangles;

where the triangles are indices into the points list. Given an index of a triangle ''ti'' we can find the points easily

IEnumerable<Point> points = triangles[ti].Select(pi=>points[pi])

However I would like to be able to define a convienience structure

IReadOnlyList<IReadOnlyList<Point>> trianglesAsPoints;

so I can do

IEnumerable<Point> points = triangles[ti]

The obvious way to do this would be to create a linq like selector

IReadOnlyList<T> Select( this IReadOnlyList<U> This
                             , Func<U,T> selector)

which returns an instance whose class overrides the following method and invokes the selector

public interface IReadOnlyList<out T> : IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable
{
    // Summary:
    //     Gets the element at the specified index in the read-only list.
    //
    // Parameters:
    //   index:
    //     The zero-based index of the element to get.
    //
    // Returns:
    //     The element at the specified index in the read-only list.
    T this[int index] { get; }
}

Does such a factory exist anywhere in the standard libs or nuget for this pattern? Note I do not want IEnumerable as a result because I would lose the indexing ability and the Count property, I just want to lazily transform the value which means not copying all the values to a new list instance up front.

like image 604
bradgonesurfing Avatar asked Sep 15 '25 09:09

bradgonesurfing


1 Answers

I don't believe there's anything which does this in the framework, no. It's obviously reasonably easy to implement yourself, but I believe you'll have to do it. It's entirely possible that there are 3rd party libraries which do it, but as IReadOnlyCollection was only in .NET 4.5 it's less likely than if the interface had existed for a while.

I'd suggest calling it something other than Select though - I'd use ProjectView or something similar. Of course that means it wouldn't work with LINQ query expressions, but it will be clearer to anyone reading the code that it's not just Enumerable.Select.

like image 65
Jon Skeet Avatar answered Sep 17 '25 22:09

Jon Skeet