Skip to content

Instantly share code, notes, and snippets.

@andrevidela
Created April 14, 2017 13:57
Show Gist options
  • Save andrevidela/45c734248a3cb0a0507123c89df08699 to your computer and use it in GitHub Desktop.
Save andrevidela/45c734248a3cb0a0507123c89df08699 to your computer and use it in GitHub Desktop.
/// Partially applies the right argument of a binary function
func partialRightApply<A, B, C>(_ f: @escaping (A, B) -> C, arg rhs: B) -> (A) -> C {
return {lhs in f(lhs, rhs)}
}
prefix operator >
prefix func > <T: Comparable>(rhs: T) -> (T) -> Bool {
return partialRightApply(>, arg: rhs)
}
prefix operator ?<
prefix func ?< <T: Comparable>(rhs: T) -> (T) -> Bool {
return partialRightApply(<, arg: rhs)
}
prefix operator ?>
prefix func ?> <T: Comparable>(rhs: T) -> (T) -> Bool {
return partialRightApply(>, arg: rhs)
}
/// Partially applies the left argument of a binary function
func partialLeftApply<A, B, C>(_ f: @escaping (A, B) -> C, arg lhs: A) -> (B) -> C {
return {rhs in f(lhs, rhs)}
}
postfix operator >?
postfix func >? <T: Comparable>(lhs: T) -> (T) -> Bool {
return partialLeftApply(>, arg: lhs)
}
postfix operator <?
postfix func <? <T: Comparable>(lhs: T) -> (T) -> Bool {
return partialLeftApply(<, arg: lhs)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment