Skip to content

Instantly share code, notes, and snippets.

@mwhittaker
Created April 19, 2015 02:31
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 mwhittaker/e757f99bcf1b4a9d6fcf to your computer and use it in GitHub Desktop.
Save mwhittaker/e757f99bcf1b4a9d6fcf to your computer and use it in GitHub Desktop.
Rust Maybe Monad
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