https://github.com/a7mdfre7at/masterly.nonemptylist
A simple and lightweight implementation of a non-empty list in C#, inspired by Scala's List, that ensures a collection always has at least one item. This helps in reducing null-related bugs and ensures safe operations on collections with at least one element.
https://github.com/a7mdfre7at/masterly.nonemptylist
a7mdfre7at csharp data-structures dotnet dotnet-core functional-programming list masterly non-empty non-empty-collections scala-list-like
Last synced: 5 months ago
JSON representation
A simple and lightweight implementation of a non-empty list in C#, inspired by Scala's List, that ensures a collection always has at least one item. This helps in reducing null-related bugs and ensures safe operations on collections with at least one element.
- Host: GitHub
- URL: https://github.com/a7mdfre7at/masterly.nonemptylist
- Owner: a7mdfre7at
- License: mit
- Created: 2024-09-13T17:22:26.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-09-15T18:39:11.000Z (over 1 year ago)
- Last Synced: 2025-01-22T14:07:53.813Z (over 1 year ago)
- Topics: a7mdfre7at, csharp, data-structures, dotnet, dotnet-core, functional-programming, list, masterly, non-empty, non-empty-collections, scala-list-like
- Language: C#
- Homepage: https://github.com/a7mdfre7at/Masterly.NonEmptyList
- Size: 87.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Masterly.NonEmptyList
A simple and lightweight implementation of a non-empty list in C#, inspired by Scala's `List`, that ensures a collection always has at least one item. This helps in reducing null-related bugs and ensures safe operations on collections with at least one element.

[](https://www.nuget.org/packages/Masterly.NonEmptyList)    [](https://github.com/a7mdfre7at/Masterly.NonEmptyList/actions/workflows/build.yml) [](https://github.com/a7mdfre7at/Masterly.NonEmptyList/actions/workflows/codeql.yml) [](https://github.com/a7mdfre7at/Masterly.NonEmptyList/actions/workflows/publish.yml)
## Give a Star! :star:
If you like or are using this project please give it a star. Thanks!
## Features
- **Non-Empty Guarantee**: Always contains at least one element
- **Head/Tail Access**: Convenient `Head`, `Tail`, `Init`, `First`, and `Last` properties
- **Functional Operations**: `Map`, `FlatMap`, `Reduce`, `Fold`, `Zip`, `Partition`, and more
- **Pattern Matching**: `Match` method and deconstruction support
- **Async Support**: Full async extension methods for all operations
- **Immutable Variant**: `ImmutableNonEmptyList` for thread-safe scenarios
- **JSON Serialization**: Built-in System.Text.Json support
- **EF Core Integration**: Entity Framework Core support via separate package
- **Equality Support**: Implements `IEquatable` with `==` and `!=` operators
- **Multi-targeting**: Supports .NET 6.0 and .NET 8.0
## Installation
```bash
dotnet add package Masterly.NonEmptyList
```
Or via Package Manager Console:
```
Install-Package Masterly.NonEmptyList
```
## Quick Start
```csharp
using Masterly.NonEmptyList;
// Create a NonEmptyList - guaranteed to have at least one element
NonEmptyList numbers = new(1, 2, 3, 4, 5);
Console.WriteLine(numbers.Head); // 1
Console.WriteLine(numbers.Last); // 5
Console.WriteLine(numbers.Tail); // [2, 3, 4, 5]
// Functional operations
NonEmptyList strings = numbers.Map(x => x.ToString());
int sum = numbers.Reduce((a, b) => a + b);
// Pattern matching
string result = numbers.Match(
single: x => $"Just {x}",
multiple: (head, tail) => $"Head: {head}, Count: {tail.Count + 1}"
);
// Safe from empty collection errors
int first = numbers.Head; // Always safe - no exceptions!
```
## Documentation
For comprehensive documentation, see the **[Wiki](../../wiki)**.
### Quick Links
| Topic | Description |
|-------|-------------|
| [Getting Started](../../wiki/Getting-Started) | Installation and basic usage |
| [Core Concepts](../../wiki/Core-Concepts) | Head, Tail, and properties |
| [Creating NonEmptyList](../../wiki/Creating-NonEmptyList) | Constructors and factory methods |
| [Functional Operations](../../wiki/Functional-Operations) | Map, FlatMap, Reduce, Fold, Zip |
| [Pattern Matching](../../wiki/Pattern-Matching) | Match and deconstruction |
| [Collection Operations](../../wiki/Collection-Operations) | Concat, Reverse, Sliding, Chunks |
| [Async Operations](../../wiki/Async-Operations) | MapAsync, FilterAsync, FoldAsync |
| [ImmutableNonEmptyList](../../wiki/ImmutableNonEmptyList) | Thread-safe immutable variant |
| [JSON Serialization](../../wiki/JSON-Serialization) | System.Text.Json support |
| [EF Core Integration](../../wiki/EF-Core-Integration) | Entity Framework Core support |
## EF Core Integration
For Entity Framework Core support, install the separate package:
```bash
dotnet add package Masterly.NonEmptyList.EntityFrameworkCore
```
```csharp
// Store NonEmptyList as JSON
modelBuilder.Entity()
.Property(p => p.Tags)
.HasNonEmptyListConversion();
// Configure relationships
modelBuilder.Entity()
.HasNonEmptyManyWithOne(o => o.Items, i => i.Order);
```
See the [EF Core Integration wiki page](../../wiki/EF-Core-Integration) for full documentation.
## License
MIT
**Free Software, Hell Yeah!**