Skip to content

Instantly share code, notes, and snippets.

@JoeyEremondi
Created September 13, 2016 17:05
Show Gist options
  • Save JoeyEremondi/76cbed71906734ddf57546fdf27298a6 to your computer and use it in GitHub Desktop.
Save JoeyEremondi/76cbed71906734ddf57546fdf27298a6 to your computer and use it in GitHub Desktop.
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