Skip to content

Instantly share code, notes, and snippets.

@Leonidas-from-XIV
Last active January 18, 2022 16:23
Show Gist options
  • Select an option

  • Save Leonidas-from-XIV/8d54d2756458ac7569112035e7a6ec3d to your computer and use it in GitHub Desktop.

Select an option

Save Leonidas-from-XIV/8d54d2756458ac7569112035e7a6ec3d to your computer and use it in GitHub Desktop.

Revisions

  1. Leonidas-from-XIV revised this gist Jan 18, 2022. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions short_circuit.ml
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,10 @@ let bigger_than x v = match v > x with true -> Some (v - 1) | false -> None
    (* Sample inputs *)
    let xs = [ 0; 1; 2; 3; 4; 5; 6; 7; 8; 9 ]

    (* Why `fold` here in the first place? Maybe your data structure doesn't have `filter_map` etc.
    * This is an example
    *)

    (* the classic way is to match and evaluate to `acc` on every branch, nesting deeper and deeper *)
    let classic xs =
    List.fold_left
  2. Leonidas-from-XIV created this gist Jan 18, 2022.
    29 changes: 29 additions & 0 deletions short_circuit.ml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    (* Some kind of function that needs to be [Some _] for the rest of the body *)
    let bigger_than x v = match v > x with true -> Some (v - 1) | false -> None

    (* Sample inputs *)
    let xs = [ 0; 1; 2; 3; 4; 5; 6; 7; 8; 9 ]

    (* the classic way is to match and evaluate to `acc` on every branch, nesting deeper and deeper *)
    let classic xs =
    List.fold_left
    (fun acc x ->
    match bigger_than 3 x with
    | None -> acc
    | Some condition1 -> (
    match bigger_than condition1 x with
    | None -> acc
    | Some condition2 -> condition2 :: acc))
    [] xs

    let new_style xs =
    List.fold_left
    (fun acc x ->
    (* instead, we define a custom operator that returns `acc` in the `None` case *)
    let ( let* ) a f = match a with Some a -> f a | None -> acc in
    let* condition1 = bigger_than 3 x in
    let* condition2 = bigger_than condition1 x in
    condition2 :: acc)
    [] xs

    (* of course the same is possible with operators like `>>=` but it is slightly more messy *)