summaryrefslogtreecommitdiffstats
path: root/src/ubase/safelist.ml
blob: 678a8b55108c14f1bab8429d2f0f670b34caa834 (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
(* Unison file synchronizer: src/ubase/safelist.ml *)
(* Copyright 1999-2018, Benjamin C. Pierce

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*)


let filterBoth f l =
  let rec loop r1 r2 = function
    [] -> (List.rev r1, List.rev r2)
  | hd::tl ->
      if f hd then loop (hd::r1) r2 tl
      else loop r1 (hd::r2) tl
  in loop [] [] l

let filterMap f l =
  let rec loop r = function
    [] -> List.rev r
  | hd::tl -> begin
      match f hd with
        None -> loop r tl
      | Some x -> loop (x::r) tl
    end
  in loop [] l

let filterMap2 f l =
  let rec loop r s = function
    [] -> List.rev r, List.rev s
  | hd::tl -> begin
      let (a, b) = f hd in
      let r' = match a with None -> r | Some x -> x::r in
      let s' = match b with None -> s | Some x -> x::s in
      loop r' s' tl
    end
  in loop [] [] l

(* These are tail-recursive versions of the standard ones from the
   List module *)
let rec concat_rec accu =
  function
    [] -> List.rev accu
  | l::r -> concat_rec (List.rev_append l accu) r
let concat l = concat_rec [] l
let flatten = concat

let append l l' =
  match l' with [] -> l | _ -> List.rev_append (List.rev l) l'

let rev_map f l =
  let rec rmap_f accu = function
    | [] -> accu
    | a::l -> rmap_f (f a :: accu) l
  in
  rmap_f [] l

let map f l = List.rev (rev_map f l)

let rev_map2 f l1 l2 =
  let rec rmap2_f accu l1 l2 =
    match (l1, l2) with
    | ([], []) -> accu
    | (a1::l1, a2::l2) -> rmap2_f (f a1 a2 :: accu) l1 l2
    | (_, _) -> invalid_arg "List.rev_map2"
  in
  rmap2_f [] l1 l2
;;

let map2 f l1 l2 = List.rev (rev_map2 f l1 l2)

let rec allElementsEqual = function
    [] -> true
  | [a] -> true
  | a::b::rest -> a=b && (allElementsEqual (b::rest))

let rec fold_left f accu l =
  match l with
    [] -> accu
  | a::_ ->
      (* We don't want l to be live when f is called *)
      let l' = List.tl l in
      fold_left f (f accu a) l'

let split l =
  let rec loop acc1 acc2 = function
    [] -> (List.rev acc1, List.rev acc2)
  | (x,y)::l -> loop (x::acc1) (y::acc2) l
  in
    loop [] [] l

let rec transpose_rec accu l =
  match l with
    [] | []::_ ->
      accu
  | [x]::_ ->
      (map (function [x] -> x | _ -> invalid_arg "Safelist.transpose") l)::accu
  | _ ->
      let (l0, r) =
        fold_left
          (fun (l0, r) l1 ->
             match l1 with
               []    -> invalid_arg "Safelist.transpose (2)"
             | a::r1 -> (a::l0, r1::r))
          ([], []) l
      in
      transpose_rec ((List.rev l0)::accu) (List.rev r)

let transpose l = List.rev (transpose_rec [] l)

let combine l1 l2 =
  let rec loop acc = function
    ([], []) -> List.rev acc
  | (a1::l1r, a2::l2r) -> loop ((a1, a2)::acc) (l1r,l2r)
  | (_, _) -> invalid_arg "Util.combine"
  in
    loop [] (l1,l2)

let remove_assoc x l =
  let rec loop acc = function
  | [] -> List.rev acc
  | (a, b as pair) :: rest ->
      if a = x then loop acc rest else loop (pair::acc) rest
  in
    loop [] l

let fold_right f l accu =
  fold_left (fun x y -> f y x) accu (List.rev l)

let flatten_map f l = flatten (map f l)

let remove x l =
  let rec loop acc = function
  | [] -> List.rev acc
  | a :: rest ->
      if a = x then loop acc rest else loop (a::acc) rest
  in
    loop [] l

let iteri f l =
  let rec loop n = function
    | [] -> ()
    | h::t -> ((f n h); loop (n+1) t)
  in loop 0 l

(* These are already tail recursive in the List module *)
let iter = List.iter
let iter2 = List.iter2
let rev = List.rev
let rev_append = List.rev_append
let hd = List.hd
let tl = List.tl
let nth = List.nth
let length = List.length
let mem = List.mem
let assoc = List.assoc
let for_all = List.for_all
let exists = List.exists
let find = List.find
let filter = List.filter
let stable_sort = List.stable_sort
let sort = List.sort
let partition = List.partition