Skip to content
This repository
Newer
Older
100644 56 lines (45 sloc) 1.76 kb
fccc6851 » MLstate
2011-06-21 Initial open-source release
1 (*
2 Copyright © 2011 MLstate
3
4 This file is part of OPA.
5
6 OPA is free software: you can redistribute it and/or modify it under the
7 terms of the GNU Affero General Public License, version 3, as published by
8 the Free Software Foundation.
9
10 OPA is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
13 more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with OPA. If not, see <http://www.gnu.org/licenses/>.
17 *)
18 (* cf mli *)
19 (* guidlines: define step by step whenever you need, in the same order than in Base.List *)
20 module List = Base.List
21 type 'a t = 'a * 'a list
22 external unwrap : 'a t -> 'a list = "%identity"
23 let wrap = function
24 | t::q -> t, q
25 | _ -> invalid_arg "HdList.wrap"
26 let singleton a = a, []
27
28 let length (_, l) = succ (List.length l)
29 let hd = fst
30 let tl = snd
31
32 let last (a, l) =
33 match l with
34 | [] -> a
35 | tl -> hd (wrap (List.rev tl))
36
37 let nth (hd, tl) i = if i = 0 then hd else List.nth tl (pred i)
38 let rev (hd, tl) = wrap (List.rev (hd::tl))
39
40 let iter f (hd, tl) = f hd; List.iter f tl
41 let map f (hd, tl) = f hd, List.map f tl
42
43 let fold_left f acc (hd, tl) = List.fold_left f (f acc hd) tl
44 let fold_right f acc (hd, tl) =
45 let tl = List.rev tl in
46 let acc = List.fold_left f acc tl in
47 f acc hd
48 (* List.fold_left f acc (rev hdl) with some more tuple cons/decons *)
49
50 let reduce_left f (hd, tl) = List.fold_left f hd tl
51 let reduce_right f hdl = reduce_left f (rev hdl)
52
53 let fold_left_map f acc (hd, tl) =
54 let acc, hd = f acc hd in
55 let acc, tl = List.fold_left_map f acc tl in
56 acc, (hd, tl)
Something went wrong with that request. Please try again.