Skip to content

Instantly share code, notes, and snippets.

@danielSanchezQ
Created December 17, 2018 08:10
Show Gist options
  • Save danielSanchezQ/c7d57aac5d6fc97973577acf98505f85 to your computer and use it in GitHub Desktop.
Save danielSanchezQ/c7d57aac5d6fc97973577acf98505f85 to your computer and use it in GitHub Desktop.
Functor trait and example rust
trait Functor <A, B, C, F> where F : Fn(&A) -> B {
fn fmap(&self, f: F) -> C;
}
enum List<T> {
Nil,
List(T, Box<List<T>>)
}
impl<A, B, F> Functor<A, B, List<B>, F> for List<A>
where F : Fn(&A) -> B {
fn fmap(&self, f: F) -> List<B> {
match self {
List::Nil => {return List::Nil},
List::List(v, next) => {
List::List(
f(v),
Box::new(next.fmap(f))
)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment