Skip to content

Instantly share code, notes, and snippets.

@patrickt
Last active August 27, 2020 02:38
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickt/e48c08db9314211aa0f0 to your computer and use it in GitHub Desktop.
Save patrickt/e48c08db9314211aa0f0 to your computer and use it in GitHub Desktop.
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