Created
December 17, 2018 08:10
-
-
Save danielSanchezQ/c7d57aac5d6fc97973577acf98505f85 to your computer and use it in GitHub Desktop.
Functor trait and example rust
This file contains 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
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