Skip to content

Instantly share code, notes, and snippets.

@heavypackets
Created June 30, 2020 16:53
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/fbacb593662ab628f7579e907462fb03 to your computer and use it in GitHub Desktop.
Save heavypackets/fbacb593662ab628f7579e907462fb03 to your computer and use it in GitHub Desktop.
use partial_application::partial;
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 xs = [10, 30, 42, 4, 6];
let qs: Vec<i32> = xs
.iter()
.cloned()
.map(partial!(calc => Ops::Div, _, 2))
.map(partial!(calc => Ops::Mult, _, 3))
.collect();
assert_eq!(qs, [15, 45, 63, 6, 9]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment