fun 0.0.1

A small prelude for functional programming in Rust
/// The empty type; no values exist or can be constructed;
#[derive(Clone)]
#[derive(Copy)]
#[derive(Eq)]
#[derive(Hash)]
#[derive(Ord)]
#[derive(PartialEq)]
#[derive(PartialOrd)]
#[derive(Rand)]
#[derive(Show)]
pub enum Either<A, B> {
    Inl(A),
    Inr(B),
}

#[inline]
pub fn elim<A, B, R, FL, FR>(inl: FL, inr: FR, m: Either<A, B>) -> R where
    FL: Fn(A) -> R,
    FR: Fn(B) -> R,
{
    match m {
        Either::Inl(a) => { inl(a) },
        Either::Inr(b) => { inr(b) },
    }
}

impl<A, B> Either<A, B> {
    #[inline]
    pub fn elim<A, B, R, FL, FR>(inl: FL, inr: FR, m: Either<A, B>) -> R where
        FL: Fn(A) -> R,
        FR: Fn(B) -> R,
    {
        elim(inl, inr, m)
    }

    #[inline]
    pub fn to_result(self) -> Result<A, B> { to_result(self) }
}

#[inline]
pub fn to_either<A, B>(m: Result<A, B>) -> Either<A, B> {
    match m {
        Ok(a) => { Either::Inl(a) },
        Err(b) => { Either::Inr(b) },
    }
}

#[inline]
pub fn to_result<A, B>(m: Either<A, B>) -> Result<A, B> {
    elim(|&: a| { Ok(a) },
         |&: b| { Err(b) },
         m)
}