// Result type utilities
// For working with computations that may fail

/// Wrap a value in Ok
pub fn ok<T, E>(value: T): Result<T, E> = Ok(value)

/// Wrap an error in Err
pub fn err<T, E>(error: E): Result<T, E> = Err(error)

/// Check if result is Ok
pub fn isOk<T, E>(r: Result<T, E>): Bool =
    match r {
        Ok(_) => true,
        Err(_) => false
    }

/// Check if result is Err
pub fn isErr<T, E>(r: Result<T, E>): Bool =
    match r {
        Ok(_) => false,
        Err(_) => true
    }

/// Get value or default
pub fn unwrapOr<T, E>(r: Result<T, E>, default: T): T =
    match r {
        Ok(v) => v,
        Err(_) => default
    }

/// Map over the success value
pub fn mapOk<T, U, E>(r: Result<T, E>, f: fn(T): U): Result<U, E> =
    match r {
        Ok(v) => Ok(f(v)),
        Err(e) => Err(e)
    }

/// Map over the error value
pub fn mapErr<T, E, F>(r: Result<T, E>, f: fn(E): F): Result<T, F> =
    match r {
        Ok(v) => Ok(v),
        Err(e) => Err(f(e))
    }
