Skip to content

Instantly share code, notes, and snippets.

@hsavit1
Forked from mbrandonw/gist:ba93c363f67291c2b5ec
Created December 12, 2015 06:28
Show Gist options
  • Select an option

  • Save hsavit1/753cdcbaf94b30fabb50 to your computer and use it in GitHub Desktop.

Select an option

Save hsavit1/753cdcbaf94b30fabb50 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