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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With