/FunctionalEnumerableExtensions

These extensions are designed to enhance memory efficiency and make common operations more convenient. Whether you're converting to lists, arrays, or working with spans, these extensions aim to improve your code's performance and readability.

Primary LanguageC#MIT LicenseMIT

FunctionalEnumerableExtensions 🌟

Build Quality Gate Coverage NuGet latest version NuGet downloads

FunctionalEnumerableExtensions is a C# class library that provides a set of extension methods for working with enumerable collections. These extensions are designed to enhance memory efficiency and make common operations more convenient. Whether you're converting to lists, arrays, or working with spans, these extensions aim to improve your code's performance and readability.

Installation 🚀

To easily integrate the FunctionalEnumerableExtensions library into your project, you can use NuGet Package Manager. NuGet is a package manager for .NET that simplifies the process of adding, removing, and updating libraries in your applications.

After that import the FunctionalEnumerableExtensions namespace in your code files where you want to use the provided extension methods:

using FunctionalEnumerableExtensions;

Available Extension Methods 🛠️

EnsureList

Prevent memory allocation when converting to a list using LINQ's .ToList().

Usage:

List<int> myList = myEnumerable.EnsureList();

EnsureArray

Prevent memory allocation when converting to an array using LINQ's .ToArray().

Usage:

T[] myArray = myEnumerable.EnsureArray();

AsSpan

Warning: DO NOT use Span if you would change the list while looping into it, it can cause exceptions.

Convert the enumerable collection to a Span, suitable for in-place data processing.

Usage:

Span<T> mySpan = myEnumerable.AsSpan();

EnsureHashSet

Prevent memory allocation by casting an IEnumerable to a HashSet if it's already of that type, otherwise create a new HashSet.

Usage:

HashSet<T> myHashSet = myEnumerable.EnsureHashSet();

CollectNonNulls

Filter out non-null items from the input IEnumerable.

Usage:

var myFilteredList = myEnumerable.CollectNonNulls();

EnsureEnumerable

Prevent the enumerable to be null.

Usage:

var myNotNullEnumerable = myEnumerable.EnsureEnumerable();

SplitBy

Splits the list according to the predicate.

Usage:

var (desiredItems, remainingItems) = enumerable.SplitBy(customer => customer.LoyaltyTimeInYears > 20);

IsNullOrEmpty

Checks if the list is null or empty

Usage:

var result = enumerable.IsNullOrEmpty();

WhereIf

Introduces optional filtering, applying a predicate only if a specified condition holds true.

Usage:

var result = enumerable.WhereIf(YourBooleanCondition(), w => w > 0);

Stringify

Converts a collection of non-null objects to a string by concatenating their properties recursively, separated by commas.

Usage:

var result = enumerable.Stringify();

EnumerateWithIndex

Enumerate an IEnumerable source and getting the Index and the Item returned in a ValueTuple.

Usage

var result = enumerable.EnumerateWithIndex();

JoinString

Chainable extensions that joins the separator string with the elements of your Enumerable.

Usage

var result = list.Select(s => s.Name).JoinString();

OrderBy and OrderByDescending with a delegate comparer

Sorts the elements of a sequence in ascending/descending order by using a specified comparer.

Usage

record Customer(string Name, int Age);

//Order by name then by age
//user OrderByDescending to order descendingly
var result = values.OrderBy(x => (x.Name, x.Age), (a, b) =>
        {
            var nameComparison = string.Compare(a.Name, b.Name, StringComparison.Ordinal);
            return nameComparison != 0 ? nameComparison : a.Age.CompareTo(b.Age);
        });

Contributing 👥

Contributions are welcome! If you find a bug or have a feature request, please open an issue on GitHub. If you would like to contribute code, please fork the repository and submit a pull request.

License 📄

This project is licensed under the MIT License. See LICENSE for more information.

Support ☕

Buy Me A Coffee