Skip to content

Instantly share code, notes, and snippets.

@mbrandonw
Last active November 22, 2021 22:04
Show Gist options
  • Save mbrandonw/ba93c363f67291c2b5ec to your computer and use it in GitHub Desktop.
Save mbrandonw/ba93c363f67291c2b5ec to your computer and use it in GitHub Desktop.
First attempt at Functor protocol in Swift
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