aboutsummaryrefslogtreecommitdiff
path: root/lib/util.ml
diff options
context:
space:
mode:
Diffstat (limited to 'lib/util.ml')
-rw-r--r--lib/util.ml25
1 files changed, 25 insertions, 0 deletions
diff --git a/lib/util.ml b/lib/util.ml
index bb50238860..f3bc44ebf4 100644
--- a/lib/util.ml
+++ b/lib/util.ml
@@ -159,6 +159,29 @@ let list_subset l1 l2 =
in
look l1
+let list_splitby p =
+ let rec splitby_loop x y =
+ match y with
+ | [] -> ([],[])
+ | (a::l) -> if (p a) then (x,y) else (splitby_loop (x@[a]) l)
+ in
+ splitby_loop []
+
+let list_firstn n l =
+ let rec aux acc = function
+ | (0, l) -> List.rev acc
+ | (n, (h::t)) -> aux (h::acc) (pred n, t)
+ | _ -> failwith "firstn"
+ in
+ aux [] (n,l)
+
+let list_lastn n l =
+ let len = List.length l in
+ let rec aux m l =
+ if m = n then l else aux (m - 1) (List.tl l)
+ in
+ if len < n then failwith "lastn" else aux len l
+
(* Arrays *)
let array_exists f v =
@@ -297,6 +320,8 @@ let intmap_to_list m = Intmap.fold (fun n v l -> (n,v)::l) m []
let intmap_inv m b = Intmap.fold (fun n v l -> if v = b then n::l else l) m []
+let in_some x = Some x
+
let out_some = function
| Some x -> x
| None -> failwith "out_some"