// Generated by bynkc — do not edit by hand.
// commons combinators

import { Ok, Err, Some, None, type Result, type Option, type ValidationError } from "./runtime.js";

export function doubledOr(o: Option<number>, fallback: number): number {
  return ((__o: Option<number>, __d: number) => __o.tag === "Some" ? __o.value : __d)(((__o: Option<number>, __f: (x: number) => number) => __o.tag === "Some" ? Some(__f(__o.value)) : None)(o, (n) => n * 2), fallback);
}

export function halve(n: number): Option<number> {
  return (n === 0 ? None : Some(Math.trunc(n / 2)));
}

export function quartered(o: Option<number>): Option<number> {
  return ((__o: Option<number>, __f: (x: number) => Option<number>) => __o.tag === "Some" ? __f(__o.value) : None)(((__o: Option<number>, __f: (x: number) => Option<number>) => __o.tag === "Some" ? __f(__o.value) : None)(o, halve), halve);
}

export function required(o: Option<string>): Result<string, string> {
  return ((__o: Option<string>, __e: string) => __o.tag === "Some" ? Ok(__o.value) : Err(__e))(o, "missing");
}

export function present(o: Option<number>): boolean {
  return (o.tag === "Some");
}

export function renumber(r: Result<number, string>): Result<number, string> {
  return ((__r: Result<number, string>, __f: (x: number) => number) => __r.tag === "Ok" ? Ok(__f(__r.value)) : __r)(r, (n) => n);
}

export function positive(n: number): Result<number, string> {
  return (n > 0 ? Ok(n) : Err("negative"));
}

export function chained(r: Result<number, string>): boolean {
  return (((__r: Result<number, string>, __f: (e: string) => string) => __r.tag === "Err" ? Err(__f(__r.error)) : __r)(((__r: Result<number, string>, __f: (x: number) => Result<number, string>) => __r.tag === "Ok" ? __f(__r.value) : __r)(r, positive), (e) => e.concat("!")).tag === "Ok");
}

export function viaLambda(r: Result<number, string>): Result<number, string> {
  return ((__r: Result<number, string>, __f: (x: number) => Result<number, string>) => __r.tag === "Ok" ? __f(__r.value) : __r)(r, (n) => {
    const out: Result<number, string> = (n > 0 ? Ok(n) : Err("negative"));
    return out;
  });
}

export function orZero(r: Result<number, string>): number {
  return ((__r: Result<number, string>, __d: number) => __r.tag === "Ok" ? __r.value : __d)(r, 0);
}

