package atd

  1. Overview
  2. Docs

Source file import.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
module String = struct
  [@@@ocaml.warning "-3-32"]
  let lowercase_ascii = StringLabels.lowercase
  let uppercase_ascii = StringLabels.uppercase
  let capitalize_ascii = StringLabels.capitalize
  include String
end

module Char = struct
  [@@@ocaml.warning "-3-32"]
  let uppercase_ascii = Char.uppercase
  include Char
end

module List = struct
  include List

  let rec filter_map f = function
      [] -> []
    | x :: l ->
        match f x with
          None -> filter_map f l
        | Some y -> y :: filter_map f l

  let concat_map f l =
    List.map f l
    |> List.flatten

  let map_first f = function
    | [] -> []
    | x :: l ->
        let y = f ~is_first:true x in
        y :: List.map (f ~is_first:false) l

  let init n f = Array.to_list (Array.init n f)

  let mapi l f =
    Array.of_list l
    |> Array.mapi f
    |> Array.to_list

  let rec find_map f = function
    | [] -> None
    | x :: l ->
        match f x with
          None -> find_map f l
        | Some _ as y -> y

  (* replace first occurrence, if any *)
  let rec assoc_update k v = function
    |  (k', _) as x :: l ->
        if k = k' then
          (k, v) :: l
        else
          x :: assoc_update k v l
    | [] ->
        []

  let rec insert_sep t ~sep =
    match t with
    | []
    | [_] -> t
    | x :: xs -> x :: sep @ (insert_sep xs ~sep)

  let split3 l =
    let (x, y, z) =
      List.fold_left (fun (xs, ys, zs) (x, y, z) ->
        (x::xs, y::ys, z::zs)
      ) ([], [], []) l in
    (List.rev x, List.rev y, List.rev z)

  let assoc_exn = assoc

  let assoc key xs =
    match List.assoc key xs with
    | s -> Some s
    | exception Not_found -> None
end

module Option = struct
  let map f = function
    | None -> None
    | Some s -> Some (f s)

  let value_exn = function
    | None -> failwith "Option.value_exn"
    | Some s -> s

  let value ~default = function
    | None -> default
    | Some s -> s

  let is_some = function
    | None -> false
    | Some _ -> true

  module O = struct
    let (>>=) x f =
      match x with
      | None -> None
      | Some x -> f x
  end
end

let sprintf = Printf.sprintf
let printf = Printf.printf
let eprintf = Printf.eprintf
let bprintf = Printf.bprintf
let fprintf = Printf.fprintf
OCaml

Innovation. Community. Security.