Last active
November 9, 2025 12:48
-
-
Save wullemsb/67c7ba2ca4633478644d50ba1183b4f0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [CollectionBuilder(typeof(ImmutableStackBuilder), nameof(ImmutableStackBuilder.Create))] | |
| public class ImmutableStack<T> | |
| { | |
| private readonly ImmutableList<T> _items; | |
| internal ImmutableStack(){} | |
| internal ImmutableStack(ImmutableList<T> items) | |
| { | |
| _items = items; | |
| } | |
| public T Peek() => _items[^1]; | |
| public ImmutableStack<T> Push(T item) => new(_items.Add(item)); | |
| public ImmutableStack<T> Pop() => new(_items.RemoveAt(_items.Count - 1)); | |
| public IEnumerator<T> GetEnumerator() => _items.GetEnumerator(); | |
| } | |
| public static class ImmutableStackBuilder | |
| { | |
| public static ImmutableStack<T> Create<T>(ReadOnlySpan<T> items) | |
| { | |
| return new ImmutableStack<T>(ImmutableList.Create<T>(items)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment