Created
April 19, 2015 02:31
-
-
Save mwhittaker/e757f99bcf1b4a9d6fcf to your computer and use it in GitHub Desktop.
Rust Maybe Monad
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
use std::ops::Shr; | |
use Maybe::{Nothing, Just}; | |
#[derive(Debug)] | |
enum Maybe<A> { | |
Nothing, | |
Just(A) | |
} | |
impl<A, B, F> Shr<F> for Maybe<A> where F: Fn(A) -> Maybe<B> { | |
type Output = Maybe<B>; | |
fn shr(self, f: F) -> Maybe<B> { | |
match self { | |
Nothing => Nothing, | |
Just(x) => f(x) | |
} | |
} | |
} | |
fn main() { | |
let m = | |
Just(1) >> |x| | |
Just(x + 2) >> |y| | |
Just(x * 3) >> |z| | |
Just(x + y + z); | |
println!("{:?}", m); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment