Last active
November 22, 2021 22:04
-
-
Save mbrandonw/ba93c363f67291c2b5ec to your computer and use it in GitHub Desktop.
First attempt at Functor protocol in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Functor { | |
func fmap <A, B> (Self<A>, A -> B) -> Self<B> | |
// ^ ERROR: Cannot specialize non-generic type '`Self`' | |
} | |
enum Maybe<A> : Functor { | |
case Nothing | |
case Just(A) | |
func fmap<B>(x: Maybe<A>, f: A -> B) -> Maybe<B> { | |
switch x { | |
case .Nothing: return .Nothing | |
case .Just(let just): return f(just) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment