Skip to content

Instantly share code, notes, and snippets.

@JoeyEremondi
Last active September 13, 2016 16:51
Show Gist options
  • Save JoeyEremondi/2843cce16c984ac8c4b3b7582718b643 to your computer and use it in GitHub Desktop.
Save JoeyEremondi/2843cce16c984ac8c4b3b7582718b643 to your computer and use it in GitHub Desktop.
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