Skip to content

Instantly share code, notes, and snippets.

@saniul
Created February 2, 2015 07:29
Show Gist options
  • Save saniul/407ef9b2b09c23d0c228 to your computer and use it in GitHub Desktop.
Save saniul/407ef9b2b09c23d0c228 to your computer and use it in GitHub Desktop.
//Based on https://github.com/klmr/named-operator
// Playground - noun: a place where people can play
struct NamedOperator<T1, T2, R> {
let f: (T1, T2) -> R;
};
struct NamedOperatorLHS<T1, T2, R> {
let f: (T1, T2) -> R;
let value: T1;
};
func < <T1, T2, R>(lhs: T1 , rhs: NamedOperator<T1, T2, R>) -> NamedOperatorLHS<T1, T2, R> {
return NamedOperatorLHS(f: rhs.f, value: lhs)
}
func > <T1, T2, R>(lhs: NamedOperatorLHS<T1, T2, R> , rhs: T2) -> R {
return lhs.f(lhs.value, rhs)
}
infix operator < {
associativity left
//totally arbitrary
precedence 150
}
infix operator > {
associativity left
//totally arbitrary
precedence 100
}
////////////////
let plus: NamedOperator<Int, Int, Int> = NamedOperator(+)
let one = 1
let two = one<plus>one
let four = two<plus>two
println(four)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment