Skip to content

Instantly share code, notes, and snippets.

@Vake93
Created August 30, 2021 17:23
Show Gist options
  • Save Vake93/4d0b4a5aafed585cc4751e5a52b7642d to your computer and use it in GitHub Desktop.
Save Vake93/4d0b4a5aafed585cc4751e5a52b7642d to your computer and use it in GitHub Desktop.
int x = Pipe<string>.Unit
| "Hello there..."
| (g => $"{g} General Kenobi")
| Console.WriteLine
| Pipe<string, int>.Select(s => s.Length)
| Console.WriteLine;
class Pipe<T>
{
public Pipe(T item)
{
Item = item;
}
public T Item { get; }
public static Pipe<T> Unit => new (default);
public static Pipe<T> operator |(Pipe<T> _, Pipe<T> pipe) => pipe;
public static Pipe<T> operator |(Pipe<T> pipe, Func<T, T> func) => new (func(pipe.Item));
public static Pipe<T> operator |(Pipe<T> pipe, Action<T> func)
{
func(pipe.Item);
return pipe;
}
public static implicit operator Pipe<T>(T item) => new (item);
public static implicit operator T(Pipe<T> pipe) => pipe.Item;
}
class Pipe<TS, TD>
{
public Pipe(Func<TS, TD> projection)
{
Projection = projection;
}
public Func<TS, TD> Projection { get; }
public static Pipe<TD> operator |(Pipe<TS> src, Pipe<TS, TD> dst) =>
new(dst.Projection(src.Item));
public static Pipe<TS, TD> Select(Func<TS, TD> map) => new(map);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment