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.108 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.107 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.106 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.105 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.104 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.103 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.102 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.101 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.100 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.99 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.98 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.97 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.96 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.95 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.94 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.93 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.92 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.91 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.90 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.89 tracks/ocaml/tools/test-generator/src/utils.ml