namespace Pentole module Result = let inline protect ([]f) x = try Ok (f x) with e -> Error e let inline pairwise_map fun_ (x: 'a, y: 'a) = match fun_ x with | Error e -> Error e | Ok o -> match fun_ y with | Ok o' -> Ok (o, o') | Error e -> Error e let of_option = function | Some s -> Ok s | None -> Error () let zip a b = match (a, b) with | Ok a, Ok b -> Ok (a, b) | Error e, _ -> Error e | _, Error e -> Error e let inline get r = match r with | Ok o -> o | Error e -> $"Tried to access value from Result r, error={e}" |> System.ArgumentException |> raise type ToStringWrapper(toString) = override _.ToString() = toString () let Result l = ToStringWrapper(fun _ -> match l with | Ok o -> sprintf "Ok %O" o | _ -> failwith "") module ResultList = let collect (lambda: 'a -> Result<'ok, 'err>) (seq_: 'a seq) = let rec iter_ acc seq_ = match Seq.tryHead seq_ with | None -> Ok acc | Some x -> match lambda x with | Error e -> Error e | Ok o -> iter_ (o::acc) (Seq.tail seq_) iter_ [] seq_