Skip to content

Instantly share code, notes, and snippets.

@tiziano88
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiziano88/0c400c9e892ad603faa7 to your computer and use it in GitHub Desktop.
Save tiziano88/0c400c9e892ad603faa7 to your computer and use it in GitHub Desktop.
Rust HKT
// Option A.
trait Functor {
fn fmap(self: &Self<A>, Fn(A) -> B) -> Self<B>
}
// Option B.
trait Functor<A,B> {
fn fmap(&self, Fn(&A) -> &B) -> ???
}
// Option C.
trait Functor {
type A
type B
fn fmap(&self, Fn(&Self::A) -> &Self::B) -> ???
}
impl Functor for Option<?> {
fn fmap(self: &Option<A>, Fn(A) -> B) -> Option<B> { ... }
}
impl Functor for Iterator<?> {
fn fmap(self: &Iterator<A>, Fn(A) -> B) -> Iterator<B> { ... } // Iterator has no generics!
}
impl Functor for Result<?> {
fn fmap(self: &Result<A>, Fn(A) -> B) -> Result<B> { ... }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment