Last active
August 27, 2020 02:38
-
-
Save patrickt/e48c08db9314211aa0f0 to your computer and use it in GitHub Desktop.
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
enum Either<A, B> { | |
case Left(A) | |
case Right(B) | |
} | |
func isLeft<A,B>(it : Either<A,B>) -> Bool { | |
switch it { case .Left: return true; case .Right: return false } | |
} | |
func isRight<A,B>(it : Either<A,B>) -> Bool { | |
return !isLeft(it) | |
} | |
func either<A, L, R>(leftCase : (L -> A), rightCase : (R -> A), upon : Either<L, R>) -> A { | |
switch upon { | |
case let .Left(a): return leftCase(a) | |
case let .Right(b) : return rightCase(b) | |
} | |
} | |
func lefts<A,B>(it : Either<A,B>[]) -> A[] { | |
return it.reduce([]) { | |
switch $1 { | |
case let .Left(a): return ($0 + [a]) | |
case _ : return $0 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment