Created
June 30, 2020 16:53
-
-
Save heavypackets/fbacb593662ab628f7579e907462fb03 to your computer and use it in GitHub Desktop.
This file contains 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 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