Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.