import { Ok, Result } from ".";

export function and<T, U, E>(r: Result<T, E>, u: Result<U, E>): Result<U, E> {
  return r.andThen(() => u);
}

export function or<T, E, F>(r: Result<T, E>, u: Result<T, F>): Result<T, F> {
  return r.orElse(() => u);
}

export async function mapAsync<T, U, E>(
  r: Result<T, E>,
  f: (t: T) => Promise<U>,
): Promise<Result<U, E>> {
  return await r.andThenAsync(async (t) => Ok(await f(t)));
}
export function mapSync<T, U, E>(
  r: Result<T, E>,
  f: (t: T) => U,
): Result<U, E> {
  return r.andThen((t) => Ok(f(t)));
}
