Guest User

Untitled

a guest
Jan 12th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. public enum Either<L, R> {
  2. case left(L)
  3. case right(R)
  4. }
  5.  
  6. extension Either {
  7.  
  8. public var left: L? {
  9. switch self {
  10. case .left(let value): return value
  11. default: return nil
  12. }
  13. }
  14.  
  15. public var right: R? {
  16. switch self {
  17. case .right(let value): return value
  18. default: return nil
  19. }
  20. }
  21.  
  22. public func either<U>(ifLeft: (L) -> U, ifRight: (R) -> U ) -> U {
  23. switch self {
  24. case .left(let value): return ifLeft(value)
  25. case .right(let value): return ifRight(value)
  26. }
  27. }
  28.  
  29. }
Add Comment
Please, Sign In to add comment