NetFabric.ForEachEx 2.0.0

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
dotnet add package NetFabric.ForEachEx --version 2.0.0
NuGet\Install-Package NetFabric.ForEachEx -Version 2.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NetFabric.ForEachEx" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NetFabric.ForEachEx --version 2.0.0
#r "nuget: NetFabric.ForEachEx, 2.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install NetFabric.ForEachEx as a Cake Addin
#addin nuget:?package=NetFabric.ForEachEx&version=2.0.0

// Install NetFabric.ForEachEx as a Cake Tool
#tool nuget:?package=NetFabric.ForEachEx&version=2.0.0

NetFabric.ForEachEx

Overview

The NetFabric.ForEachEx package is a valuable addition to your .NET projects that enhances the functionality of the ForEach method, specifically designed for enumerables. With this package, you can seamlessly apply the ForEachEx method to various enumerable types, making your code cleaner, more concise, and more efficient.

Features

  • Universal ForEachEx: Apply the ForEachEx method to a wide range of enumerable types, including lists, arrays, spans, and more, simplifying your iteration code.

  • Improved Readability: Simplify your code by using a consistent ForEachEx syntax across different enumerable types, making it more readable and maintainable.

  • Custom Value Actions: Easily create custom value actions to perform operations on enumerable elements efficiently, all while avoiding heap allocations.

  • Vectorization (SIMD) support: Take advantage of vectorization (SIMD) support using ForEachVectorEx to improve the performance of your code.

Installation

You can easily add the NetFabric.ForEachEx package to your .NET project using NuGet Package Manager. Simply run the following command in your project directory:

dotnet add package NetFabric.ForEachEx

Usage

Using NetFabric.ForEachEx is straightforward. First, ensure you've added the package to your project as described in the installation section.

Then, you can import the NetFabric namespace and start using the enhanced ForEachEx method with various enumerable types:

using NetFabric;

// ...

var myList = new List<int> { 1, 2, 3, 4, 5 };

myList.ForEachEx(item => Console.WriteLine(item));

The ForEachEx method is now available for enumerable types, allowing you to iterate through and operate on their elements with ease.

Additionally, you can take advantage of custom value actions to perform specific operations on enumerable elements. Here's an example of how to create and use a custom value action:

using NetFabric;

// Define a custom value action
public struct CustomValueAction<T> : IAction<T>
{
    private int count;

    public CustomValueAction(int initialCount)
    {
        count = initialCount;
    }

    public void Invoke(T item)
    {
        // Implement your custom action here
        Console.WriteLine($"Item {count}: {item}");
        count++;
    }
}

// ...

var values = new List<int> { 10, 20, 30, 40, 50 };

var customAction = new CustomValueAction<int>(1);
values.ForEachEx(customAction); // Apply the custom action to the enumerable

In this example, the CustomValueAction<T> struct is implemented to perform a custom action on each element of the values enumerable. You can define your logic inside the Invoke method of your custom value action.

You can also take advantage of vectorization (SIMD) support using the ForEachVectorEx method. Here's an example using the provided SumValueAction to summ all the items of the collection:

using NetFabric;

// ...

var myList = new List<int> { 1, 2, 3, 4, 5 };
var action = new SumValueAction<int>();
myList.ForEachVectorEx(action);
var sum = action.Result;

You can find how to implement a value action that support vectorization by checking the source code of the SumValueAction class.

With the NetFabric.ForEachEx package, you can easily apply the ForEachEx method to various enumerable types while also benefiting from the fact that value actions do not allocate on the heap, unlike lambda expressions. This can lead to improved memory efficiency in your code.

Benchmarks

The latest benchmarks for the NetFabric.ForEachEx package can be found in BENCHMARKS.md.

Contribution

Contributions are welcome! If you encounter issues or have suggestions for improvements, please feel free to open an issue or submit a pull request on our GitHub repository.

License

This package is licensed under the MIT License. See the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 253 10/17/2023
1.0.0 115 10/3/2023

Changed Invoke() in IAction to take a ref T parameter.
     Add support for vectorization (SIMD).