Advertisement
DoctorRynerNew

Untitled

Nov 23rd, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.60 KB | None | 0 0
  1. protocol Functor {
  2.     associatedtype A
  3.     associatedtype F: Functor = Self
  4.  
  5.     func fmap<B> (_ f: (A) -> B) -> F where F.A == B
  6. }
  7.  
  8. enum Maybe<A> {
  9.     case Nothing
  10.     case Just (A)
  11. }
  12.  
  13. extension Maybe: Functor {
  14.     func fmap<B>(_ f: (A) -> B) -> Maybe<B> {
  15.         switch self {
  16.         case let .Just (x): return .Just (f (x))
  17.         case .Nothing: return .Nothing
  18.         }
  19.     }
  20. }
  21.  
  22. let test1: Maybe<Int>    = Maybe.Just (0)
  23.   , test2: Maybe<String> = test1.fmap { String ($0) }
  24.  
  25. print (test2)
  26.  
  27. func fmap<A, B> (_ f: (A) -> B, _ functor: Functor) -> Functor { functor.fmap (f) }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement