Skip to content

Instantly share code, notes, and snippets.

@lostmsu
Created January 25, 2022 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lostmsu/6922f6e1f5b4f07c88358e92d6e8b312 to your computer and use it in GitHub Desktop.
Save lostmsu/6922f6e1f5b4f07c88358e92d6e8b312 to your computer and use it in GitHub Desktop.
using System;
Func<int, int, int> addImpl = (a, b) => a + b;
var add = Infix.Create(addImpl);
Console.WriteLine(41 |add| 1);
public class Infix<T1, T2, TResult> {
public Func<T1, T2, TResult> Op;
public static Infix<T2, TResult> operator |
(T1 arg0, Infix<T1, T2, TResult> op)
=> new (arg => op.Op(arg0, arg));
}
public class Infix<T, TResult> {
public Func<T, TResult> Op {get;}
public Infix(Func<T, TResult> op) {
this.Op = op;
}
public static TResult operator |
(Infix<T, TResult> op, T arg)
=> op.Op(arg);
}
public static class Infix {
public static Infix<T1, T2, TResult> Create<T1, T2, TResult>(Func<T1, T2, TResult> op)
=> new Infix<T1, T2, TResult>{ Op = op };
}
{
"version": 1,
"target": "Run",
"mode": "Debug"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment