Sha256: b15fffa13638acd6091de91ae06c01e3c86c15fe8376625b5bc67a69f5e8a3d7

Contents?: true

Size: 1.74 KB

Versions: 70

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

70 entries across 70 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.179 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.178 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.177 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.176 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.175 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.174 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.173 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.172 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.171 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.170 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.169 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.167 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.166 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.165 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.164 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.163 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.162 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.161 tracks/ocaml/tools/test-generator/src/utils.ml
trackler-2.2.1.160 tracks/ocaml/tools/test-generator/src/utils.ml