Last active
June 24, 2019 16:02
-
-
Save tiagodll/0432b63fb0b3cf62e928507173bf651e to your computer and use it in GitHub Desktop.
adds pipe functionality to C#
This file contains 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; | |
namespace pipe_sharp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var total = Pipe<int> | |
.Take(12) | |
.Then<int>(x => x + 10) | |
.Then<int>(x => "01" + x.ToString()) | |
.Return<string>(x => int.Parse(x)); | |
Console.WriteLine("total: " + total); | |
Console.ReadKey(); | |
} | |
} | |
class Pipe<T> | |
{ | |
private object param; | |
public Pipe(object param) | |
{ | |
this.param = param; | |
} | |
public static Pipe<T> Take(object param) | |
{ | |
var pipe = new Pipe<T>(param); | |
return pipe; | |
} | |
public Pipe<T> Then(Func<object, object> f) | |
{ | |
this.param = f.DynamicInvoke(param); | |
return this; | |
} | |
public Pipe<T> Then<T1>(Func<T1, object> f) | |
{ | |
this.param = f.DynamicInvoke(param); | |
return this; | |
} | |
public T Return() | |
{ | |
return (T)param; | |
} | |
public T Return(Func<object, T> f) { | |
return (T)f.DynamicInvoke(param); | |
} | |
public T Return<T1>(Func<T1, T> f) | |
{ | |
return (T)f.DynamicInvoke(param); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment