-
Notifications
You must be signed in to change notification settings - Fork 0
ImmutableNonEmptyList
Ahmad Al-freihat edited this page Jan 1, 2026
·
1 revision
ImmutableNonEmptyList<T> is an immutable variant where all operations return new instances, making it thread-safe.
// From values
ImmutableNonEmptyList<int> list = new(1, 2, 3);
// From mutable
NonEmptyList<int> mutable = new(1, 2, 3);
ImmutableNonEmptyList<int> immutable = mutable.ToImmutable();All modifications return new instances - the original is never changed:
ImmutableNonEmptyList<int> list = new(1, 2, 3);
ImmutableNonEmptyList<int> appended = list.Append(4);
// list: [1, 2, 3] (unchanged)
// appended: [1, 2, 3, 4] (new instance)ImmutableNonEmptyList<int> list = new(1, 2, 3);
ImmutableNonEmptyList<int> prepended = list.Prepend(0);
// prepended: [0, 1, 2, 3]Update an element at an index:
ImmutableNonEmptyList<int> list = new(1, 2, 3);
ImmutableNonEmptyList<int> updated = list.SetItem(1, 99);
// updated: [1, 99, 3]Insert at a position:
ImmutableNonEmptyList<int> list = new(1, 2, 3);
ImmutableNonEmptyList<int> inserted = list.Insert(1, 99);
// inserted: [1, 99, 2, 3]Remove operations return null if the result would be empty:
ImmutableNonEmptyList<int> list = new(1, 2, 3);
ImmutableNonEmptyList<int>? removed = list.RemoveAt(0);
// removed: [2, 3]
ImmutableNonEmptyList<int> single = new(1);
ImmutableNonEmptyList<int>? removedSingle = single.RemoveAt(0);
// removedSingle: null (can't remove last element)ImmutableNonEmptyList<int> list = new(1, 2, 3);
ImmutableNonEmptyList<int>? withoutFirst = list.RemoveFirst();
// withoutFirst: [2, 3]
ImmutableNonEmptyList<int>? withoutLast = list.RemoveLast();
// withoutLast: [1, 2]All functional operations from NonEmptyList<T> are available:
ImmutableNonEmptyList<int> list = new(1, 2, 3);
ImmutableNonEmptyList<string> mapped = list.Map(x => x.ToString());
// mapped: ["1", "2", "3"]ImmutableNonEmptyList<int> list = new(1, 2, 3);
ImmutableNonEmptyList<int> reversed = list.Reverse();
// reversed: [3, 2, 1]ImmutableNonEmptyList<int> list = new(1, 2, 2, 3, 3);
ImmutableNonEmptyList<int> distinct = list.Distinct();
// distinct: [1, 2, 3]ImmutableNonEmptyList<int> immutable = new(1, 2, 3);
NonEmptyList<int> mutable = immutable.ToMutable();
// Now you can modify mutableNonEmptyList<int> mutable = new(1, 2, 3);
ImmutableNonEmptyList<int> immutable = mutable.ToImmutable();ImmutableNonEmptyList<T> is completely thread-safe:
ImmutableNonEmptyList<int> shared = new(1, 2, 3);
// Safe to access from multiple threads
var task1 = Task.Run(() => shared.Append(4));
var task2 = Task.Run(() => shared.Append(5));
var task3 = Task.Run(() => shared.Map(x => x * 2));
// shared is never modified
// Each task creates and returns a new instanceWorks with System.Text.Json:
ImmutableNonEmptyList<int> list = new(1, 2, 3);
string json = JsonSerializer.Serialize(list);
// Output: [1,2,3]
ImmutableNonEmptyList<int> deserialized =
JsonSerializer.Deserialize<ImmutableNonEmptyList<int>>(json);| Scenario | Use |
|---|---|
| Multi-threaded access | ImmutableNonEmptyList<T> |
| Sharing across components | ImmutableNonEmptyList<T> |
| Functional programming style | ImmutableNonEmptyList<T> |
| Performance-critical mutations | NonEmptyList<T> |
| Local, single-threaded use | NonEmptyList<T> |
- Learn about JSON Serialization for persistence
- See Thread Safety for more concurrency details