Created
June 8, 2014 02:11
-
-
Save mwhittaker/f036cab4abc5f2ab3dc0 to your computer and use it in GitHub Desktop.
Rust Option Bind
This file contains hidden or 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
// Option bind for rust 0.10 | |
fn inv(f: f32) -> Option<f32> { | |
if f == 0.0 { | |
None | |
} | |
else { | |
Some(1.0 / f) | |
} | |
} | |
fn bind<A, B>(o: Option<A>, f: fn(A) -> Option<B>) -> Option<B> { | |
match o { | |
Some(x) => f(x), | |
None => None | |
} | |
} | |
fn main() { | |
println!("{}", bind(Some(2.0), inv)) | |
println!("{}", bind(Some(0.0), inv)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To build and run, enter