2024-10-26 12:15:44 +02:00
|
|
|
namespace Pentole
|
|
|
|
|
|
|
|
module Result =
|
2024-10-26 14:11:58 +02:00
|
|
|
|
2024-10-26 12:15:44 +02:00
|
|
|
let inline protect ([<InlineIfLambda>]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 ()
|
|
|
|
|
2024-10-26 14:11:58 +02:00
|
|
|
let zip a b =
|
|
|
|
match (a, b) with
|
|
|
|
| Ok a, Ok b -> Ok (a, b)
|
|
|
|
| Error e, _ -> Error e
|
|
|
|
| _, Error e -> Error e
|
|
|
|
|
2024-10-26 14:33:54 +02:00
|
|
|
let inline get r =
|
|
|
|
match r with
|
|
|
|
| Ok o -> o
|
|
|
|
| Error e ->
|
|
|
|
$"Tried to access value from Result r, error={e}"
|
|
|
|
|> System.ArgumentException
|
|
|
|
|> raise
|
|
|
|
|
2024-10-26 12:15:44 +02:00
|
|
|
|
|
|
|
type ToStringWrapper(toString) =
|
2024-10-26 14:11:58 +02:00
|
|
|
override _.ToString() = toString ()
|
2024-10-26 12:15:44 +02:00
|
|
|
|
|
|
|
let Result l = ToStringWrapper(fun _ ->
|
|
|
|
match l with
|
|
|
|
| Ok o -> sprintf "Ok %O" o
|
|
|
|
| _ -> failwith "")
|
|
|
|
|
2024-10-26 14:11:58 +02:00
|
|
|
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_
|