Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protocol Functor {
- associatedtype A
- associatedtype F: Functor = Self
- func fmap<B> (_ f: (A) -> B) -> F where F.A == B
- }
- enum Maybe<A> {
- case Nothing
- case Just (A)
- }
- extension Maybe: Functor {
- func fmap<B>(_ f: (A) -> B) -> Maybe<B> {
- switch self {
- case let .Just (x): return .Just (f (x))
- case .Nothing: return .Nothing
- }
- }
- }
- let test1: Maybe<Int> = Maybe.Just (0)
- , test2: Maybe<String> = test1.fmap { String ($0) }
- print (test2)
- func fmap<A, B> (_ f: (A) -> B, _ functor: Functor) -> Functor { functor.fmap (f) }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement