Skip to content

Instantly share code, notes, and snippets.

@heavypackets
Last active June 30, 2020 05:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heavypackets/8aedb6007c441ef77a1cc41707acdca4 to your computer and use it in GitHub Desktop.
Save heavypackets/8aedb6007c441ef77a1cc41707acdca4 to your computer and use it in GitHub Desktop.
Partial application fun in Rust
#[macro_use]
extern crate partial_application;
enum Ops {
Add, Sub, Div, Mult
}
fn calc(op: Ops, x: i32, y: i32) -> i32 {
match op {
Ops::Add => x + y,
Ops::Sub => x - y,
Ops::Div => x / y,
Ops::Mult => x * y
}
}
fn main() {
let div_by_two = partial!(calc => Ops::Div, _, 2);
let times_three = partial!(calc => Ops::Mult, _, 3);
let xs = [10, 30, 42, 4, 6];
let _qs: Vec<i32> = xs
.iter()
.cloned()
.map(div_by_two)
.map(times_three)
.inspect(|x| println!("{}", x))
.collect();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment