Sha256: 07abf5e9b9eb6c786115508e0a4f8d2232f466027d0e26437c9360f0383e4082

Contents?: true

Size: 1.74 KB

Versions: 149

Compression:

Stored size: 1.74 KB

Contents

open Core
open Yojson.Basic
open Yojson.Basic.Util

let map2 (f: 'a -> 'b -> 'c) (r1: ('a, 'e) Result.t) (r2: ('b, 'e) Result.t): ('c, 'e) Result.t = match (r1, r2) with
  | (Error x, _) -> Error x
  | (_, Error x) -> Error x
  | (Ok a, Ok b) -> Ok (f a b)

let map2_option (f: 'a -> 'b -> 'c) (r1: 'a option) (r2: 'b option): 'c option = match (r1, r2) with
  | (None, _) -> None
  | (_, None) -> None
  | (Some a, Some b) -> Some (f a b)

let sequence (rs: (('a, 'e) Result.t) list): (('a list), 'e) Result.t =
  List.fold_right rs ~init:(Ok []) ~f:(map2 (fun x xs -> x :: xs))

let sequence_option (rs: ('a option) list): ('a list) option =
  List.fold_right rs ~init:(Some []) ~f:(map2_option (fun x xs -> x :: xs))

let to_list_option json =
  try Some (to_list json) with Type_error _ -> None

let to_list_note error json =
  try Ok (to_list json) with Type_error _ -> Error error

let to_assoc_note error json =
  try Ok (to_assoc json) with Type_error _ -> Error error

let to_assoc_option json =
  try Some (to_assoc json) with Type_error _ -> None

let to_string_note error json =
  try Ok (to_string json) with Type_error _ -> Error error

let to_string_option json =
  try Some (to_string json) with _ -> None

let safe_to_int_option json =
  try Some (to_int json) with Type_error _ -> None

let member_note error m json =
  try Ok (member m json) with Type_error _ -> Error error

let find_note (xs: ('a, 'b) List.Assoc.t) (key: 'a) (error: 'e): ('b, 'e) Result.t = 
  match List.Assoc.find ~equal:(=) xs key with
  | Some v -> Ok v
  | None -> Error error

let (>>) f g = Fn.compose f g

let find_arrayi ?start:(start = 0) xs ~f =
  let rec go i =
    if i >= Array.length xs then None
    else if f xs.(i) then Some (i, xs.(i))
    else go (i + 1) in
  go start

Version data entries

149 entries across 149 versions & 1 rubygems

Version Path
trackler-2.2.1.28 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.27 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.26 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.25 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.24 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.23 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.22 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.21 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.20 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.19 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.18 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.17 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.16 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.15 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.14 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.13 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.12 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.11 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.10 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.9 tracks/ocaml/tools/test-generator/src/utils.ml