Skip to content

Instantly share code, notes, and snippets.

@JoeyEremondi
Created September 14, 2016 04:09
Show Gist options
  • Save JoeyEremondi/c288c5263cd4596031f5aa18b9142d4a to your computer and use it in GitHub Desktop.
Save JoeyEremondi/c288c5263cd4596031f5aa18b9142d4a to your computer and use it in GitHub Desktop.
use std::boxed::Box;
use std::borrow::Borrow;
#[derive(Debug)]
enum List<T> {
Null,
Cons(T, Box<List<T>>),
}
fn map<A, B, F: Fn(&A) -> B, L: Borrow<List<A>>>(f: F, lst: L) -> List<B> {
match lst.borrow() {
&List::Null => List::Null,
&List::Cons(ref h, ref t) => {
let new_head = f(h);
let new_tail = map(f, &**t);
List::Cons(new_head, Box::new(new_tail))
}
}
}
#[derive(Debug)]
enum Foo {Foo}
fn main() {
let il: List<Foo> = List::Cons(Foo::Foo, Box::new(List::Null));
println!("{:?}", il);
let il = map(|i| i, il);
println!("{:?}", il);
let ill = map(|i| 4, &il);
println!("{:?}", ill);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment