Last active
September 13, 2016 16:51
-
-
Save JoeyEremondi/2843cce16c984ac8c4b3b7582718b643 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::rc::Rc; | |
enum List<T> { | |
Null, | |
Cons(Rc<T>, Rc<List<T>>), | |
} | |
fn map_noref<A, B, F>(f: &F, lst: Rc<List<A>>) -> Rc<List<B>> | |
where F: Fn(Rc<A>) -> Rc<B> | |
{ | |
match (*lst) { | |
List::Null => Rc::new(List::Null), | |
List::Cons(ref h, ref t) => { | |
let newHead = f(h.clone()); | |
let newTail = map_noref(f, t.clone()); | |
Rc::new(List::Cons(newHead, newTail)) | |
} | |
} | |
} | |
fn map_ref<A, B, F>(f: &F, lst: Rc<List<A>>) -> Rc<List<B>> | |
where F: Fn(&Rc<A>) -> Rc<B> | |
{ | |
match (*lst) { | |
List::Null => Rc::new(List::Null), | |
List::Cons(ref h, ref t) => { | |
let newHead = f(h); | |
let newTail = map_ref(f, t.clone()); | |
Rc::new(List::Cons(newHead, newTail)) | |
} | |
} | |
} | |
fn main() {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment