Skip to content

Commit

Permalink
Add Transformlist.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ottermandias committed Jul 28, 2023
1 parent e3d26f1 commit 03b6b17
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Classes/TransformList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace OtterGui.Classes;

/// <summary>
/// A IReadOnlyList based on any other IReadOnlyList that applies a transforming step before fetching.
/// </summary>
public readonly struct TransformList<TIn, TOut> : IReadOnlyList<TOut>
{
private readonly IReadOnlyList<TIn> _list;
private readonly Func<TIn, TOut> _transform;

public TransformList(IReadOnlyList<TIn> items, Func<TIn, TOut> transform)
{
_list = items;
_transform = transform;
}

public IEnumerator<TOut> GetEnumerator()
=> _list.Select(_transform).GetEnumerator();

IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();

public int Count
=> _list.Count;

public TOut this[int index]
=> _transform(_list[index]);
}

0 comments on commit 03b6b17

Please sign in to comment.