Skip to content

Instantly share code, notes, and snippets.

@quux00
Last active January 4, 2016 21:19
Show Gist options
  • Save quux00/8679757 to your computer and use it in GitHub Desktop.
Save quux00/8679757 to your computer and use it in GitHub Desktop.
Hof in Rust
fn double(a: int) -> int { a * 2 }
fn ntimes(f: proc(int) -> int, times: int) -> proc(int) -> int {
proc(x: int) {
match times {
0 => { x },
_ => { f(ntimes(f, times - 1)(x)) } // swap the order relative to the original example
}
}
}
fn main() {
let quadruple = ntimes(double, 3);
println!("quad: {:d}", quadruple(3));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment