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.
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;
Prevent memory allocation when converting to a list using LINQ's .ToList()
.
Usage:
List<int> myList = myEnumerable.EnsureList();
Prevent memory allocation when converting to an array using LINQ's .ToArray()
.
Usage:
T[] myArray = myEnumerable.EnsureArray();
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();
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();
Filter out non-null items from the input IEnumerable.
Usage:
var myFilteredList = myEnumerable.CollectNonNulls();
Prevent the enumerable to be null.
Usage:
var myNotNullEnumerable = myEnumerable.EnsureEnumerable();
Splits the list according to the predicate.
Usage:
var (desiredItems, remainingItems) = enumerable.SplitBy(customer => customer.LoyaltyTimeInYears > 20);
Checks if the list is null or empty
Usage:
var result = enumerable.IsNullOrEmpty();
Introduces optional filtering, applying a predicate only if a specified condition holds true.
Usage:
var result = enumerable.WhereIf(YourBooleanCondition(), w => w > 0);
Converts a collection of non-null objects to a string by concatenating their properties recursively, separated by commas.
Usage:
var result = enumerable.Stringify();
Enumerate an IEnumerable source and getting the Index and the Item returned in a ValueTuple.
Usage
var result = enumerable.EnumerateWithIndex();
Chainable extensions that joins the separator string with the elements of your Enumerable.
Usage
var result = list.Select(s => s.Name).JoinString();
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);
});
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.
This project is licensed under the MIT License. See LICENSE for more information.