Created
September 13, 2016 17:05
-
-
Save JoeyEremondi/76cbed71906734ddf57546fdf27298a6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
use std::boxed::Box; | |
enum List<T> { | |
Null, | |
Cons(T, Box<List<T>>), | |
} | |
fn map_noref<A, B, F>(f: &F, lst: List<A>) -> List<B> | |
where F: Fn(A) -> B | |
{ | |
match (lst) { | |
List::Null => List::Null, | |
List::Cons(h, t) => { | |
let newHead = f(h); | |
let newTail = map_noref(f, *t); | |
List::Cons(newHead, Box::new(newTail)) | |
} | |
} | |
} | |
fn map_ref<A, B, F>(f: &F, lst: &List<A>) -> List<B> | |
where F: Fn(&A) -> B | |
{ | |
match (*lst) { | |
List::Null => List::Null, | |
List::Cons(ref h, ref t) => { | |
let newHead = f(h); | |
let newTail = map_ref(f, t); | |
List::Cons(newHead, Box::new(newTail)) | |
} | |
} | |
} | |
fn main() {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment