Last active
June 30, 2020 05:44
-
-
Save heavypackets/8aedb6007c441ef77a1cc41707acdca4 to your computer and use it in GitHub Desktop.
Partial application fun in Rust
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
#[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