move to use expressive results instead of Some/None

This commit is contained in:
NunoSempere 2023-10-15 00:52:33 +01:00
parent 4959255947
commit 4708c6f198
5 changed files with 17 additions and 17 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -18,8 +18,8 @@ let cumsumXs xs =
let rec nth xs (n: int) = let rec nth xs (n: int) =
match xs with match xs with
| [] -> None | [] -> Error "nth function finds no match"
| y :: ys -> if n = 0 then Some(y) else nth ys (n-1) | y :: ys -> if n = 0 then Ok(y) else nth ys (n-1)
(* (*
Note that this is O(n) access. Note that this is O(n) access.
That is the cost of using the nice match syntax, That is the cost of using the nice match syntax,
@ -29,19 +29,19 @@ let rec nth xs (n: int) =
let findIndex xs test = let findIndex xs test =
let rec recursiveHelper ys i = let rec recursiveHelper ys i =
match ys with match ys with
| [] -> None | [] -> Error "findIndex doesn't find an index"
| z :: zs -> if test z then Some i else recursiveHelper zs (i+1) | z :: zs -> if test z then Ok(i) else recursiveHelper zs (i+1)
in in
recursiveHelper xs 0 recursiveHelper xs 0
let rec unwind xs = let rec unwind xs =
match xs with match xs with
| [] -> Some([]) | [] -> Ok([])
| None :: ys -> None | Error e:: ys -> Error e
| Some(y) :: ys -> ( | Ok(y) :: ys -> (
match unwind ys with match unwind ys with
| Some(zs) -> Some(y :: zs) | Ok(zs) -> Ok(y :: zs)
| None -> None | Error e -> Error e
) )
(* Basic samplers *) (* Basic samplers *)
@ -64,21 +64,21 @@ let sampleTo low high =
let logstd = (loghigh -. loglow) /. (2.0 -. normal_95_ci_length ) in let logstd = (loghigh -. loglow) /. (2.0 -. normal_95_ci_length ) in
sampleLognormal logmean logstd sampleLognormal logmean logstd
let mixture (samplers: (unit -> float) list) (weights: float list): float option = let mixture (samplers: (unit -> float) list) (weights: float list): (float, string) result =
if (List.length samplers == List.length weights) if (List.length samplers == List.length weights)
then None then Error "in mixture function, List.length samplers != List.length weights"
else else
let normalized_weights = normalizeXs weights in let normalized_weights = normalizeXs weights in
let cumsummed_normalized_weights = cumsumXs normalized_weights in let cumsummed_normalized_weights = cumsumXs normalized_weights in
let p = sampleZeroToOne () in let p = sampleZeroToOne () in
let chosenSamplerIndex = findIndex cumsummed_normalized_weights (fun x -> x < p) in let chosenSamplerIndex = findIndex cumsummed_normalized_weights (fun x -> x < p) in
let sampler = match chosenSamplerIndex with let sampler = match chosenSamplerIndex with
| None -> None | Error e -> Error e
| Some(i) -> nth samplers i | Ok(i) -> nth samplers i
in in
let sample = match sampler with let sample = match sampler with
| None -> None | Error e -> Error e
| Some(f) -> Some(f ()) | Ok(f) -> Ok(f ())
in in
sample sample
@ -95,8 +95,8 @@ let () =
let n = 1_000_000 in let n = 1_000_000 in
let samples = List.init n (fun _ -> sampler ()) in let samples = List.init n (fun _ -> sampler ()) in
match unwind samples with match unwind samples with
| None -> Printf.printf "error" | Error err -> Printf.printf "Error %s\n" err
| Some(xs) -> ( | Ok(xs) -> (
let mean = sumFloats xs /. float_of_int(n) in let mean = sumFloats xs /. float_of_int(n) in
Printf.printf "Mean: %f" mean Printf.printf "Mean: %f" mean
) )