either.swift

Sign up for a GitHub account Sign in All Gists public patrickt / either.swift Created 2014-06-03 Gist Detail Revisions 1 Stars 7 Download Gist Clone this gist Embed this gist Link to this gist either.swift Raw File suppressed. Click to show. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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) } } Sign up for free to join this conversation on GitHub . A...

Linked on 2014-06-03 19:46:25 | Similar Links