Last active
February 3, 2018 18:24
-
-
Save pavel-agarkov/89b7599a577f7197b6ff68894bbd0a55 to your computer and use it in GitHub Desktop.
Naive C# Pipe implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
public static class Test | |
{ | |
static Test() | |
{ | |
1d.Pipe(TimeSpan.FromDays, AddHours(3), ToString, ToLower); | |
Enumerable.Range(0, 100).Pipe( | |
Select(n => n * 2), | |
Where(n => n % 2 > 0), | |
Enumerable.Sum, | |
Convert.ToString | |
); | |
} | |
public static Func<TimeSpan, TimeSpan> AddHours(int hours) | |
=> (TimeSpan timeSpan) => timeSpan.Add(TimeSpan.FromHours(hours)); | |
public static string ToString(this TimeSpan timeSpan) | |
=> timeSpan.ToString(); | |
public static string ToLower(this string str) | |
=> str.ToLower(); | |
public static Func<IEnumerable<int>, IEnumerable<int>> Select(Func<int, int> selector) | |
=> (IEnumerable<int> list) => list.Select(selector); | |
public static Func<IEnumerable<int>, IEnumerable<int>> Where(Func<int, bool> filter) | |
=> (IEnumerable<int> list) => list.Where(filter); | |
} | |
public static class PipeExtensions | |
{ | |
public static TResult Pipe<TInput, TResult> | |
(this TInput input, | |
Func<TInput, TResult> func) | |
=> func(input); | |
public static TResult1 Pipe<TInput, TResult, TResult1> | |
(this TInput input, | |
Func<TInput, TResult> func, | |
Func<TResult, TResult1> func1) | |
=> func1(func(input)); | |
public static TResult2 Pipe<TInput, TResult, TResult1, TResult2> | |
(this TInput input, | |
Func<TInput, TResult> func, | |
Func<TResult, TResult1> func1, | |
Func<TResult1, TResult2> func2) | |
=> func2(func1(func(input))); | |
public static TResult3 Pipe<TInput, TResult, TResult1, TResult2, TResult3> | |
(this TInput input, | |
Func<TInput, TResult> func, | |
Func<TResult, TResult1> func1, | |
Func<TResult1, TResult2> func2, | |
Func<TResult2, TResult3> func3) | |
=> func3(func2(func1(func(input)))); | |
public static TResult4 Pipe<TInput, TResult, TResult1, TResult2, TResult3, TResult4> | |
(this TInput input, | |
Func<TInput, TResult> func, | |
Func<TResult, TResult1> func1, | |
Func<TResult1, TResult2> func2, | |
Func<TResult2, TResult3> func3, | |
Func<TResult3, TResult4> func4) | |
=> func4(func3(func2(func1(func(input))))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment