C# Version of Pipe in Angular RxJS
Do you like the Pipe function in angular rsjx? Want a C# version? You can use IEnumerable.Aggregate
Below is an XUnit Test
using System;
using System.Linq;
using Xunit;
namespace PipeTest
{
public class UnitTest1
{
[Fact]
public void Test2()
{
Func<int, int>[] funcs = { AddOne, AddTwo, AddThree, AddFour };
int initialValue = 1;
int finalTotal = Pipe(initialValue, funcs);
Assert.Equal(11, finalTotal);
}
public int Pipe(int initialValue, Func<int, int>[] funcs)
{
int finalTotal =
funcs.Aggregate(initialValue,
(accumulator, next) => next(accumulator),
finalResult => {
return finalResult;
}
);
return finalTotal;
}
public int AddOne(int value)
{
return value + 1;
}
public int AddTwo(int value)
{
return value + 2;
}
public int AddThree(int value)
{
return value + 3;
}
public int AddFour(int value)
{
return value + 4;
}
}
}