Created
September 14, 2016 04:09
-
-
Save JoeyEremondi/c288c5263cd4596031f5aa18b9142d4a 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; | |
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