From 0195b4bd03dfca7bb5f3980367b2c2382e47844b Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Fri, 6 Jul 2018 14:41:29 +0700 Subject: [PATCH] Add some helpers to impldement %{deps[i]} Signed-off-by: Rudi Grinberg --- src/stdune/int.ml | 6 ++++++ src/stdune/int.mli | 2 ++ src/stdune/list.ml | 6 ++++++ src/stdune/list.mli | 2 ++ 4 files changed, 16 insertions(+) diff --git a/src/stdune/int.ml b/src/stdune/int.ml index c11dc3da..d5c4a717 100644 --- a/src/stdune/int.ml +++ b/src/stdune/int.ml @@ -13,3 +13,9 @@ include T module Set = Set.Make(T) module Map = Map.Make(T) + +let of_string_exn s = + match int_of_string s with + | exception Failure _ -> + failwith (Printf.sprintf "of_string_exn: invalid int %S" s) + | s -> s diff --git a/src/stdune/int.mli b/src/stdune/int.mli index e9ff5063..62aeba20 100644 --- a/src/stdune/int.mli +++ b/src/stdune/int.mli @@ -3,3 +3,5 @@ val compare : t -> t -> Ordering.t module Set : Set.S with type elt = t module Map : Map.S with type key = t + +val of_string_exn : string -> t diff --git a/src/stdune/list.ml b/src/stdune/list.ml index 395119d6..2cc7655a 100644 --- a/src/stdune/list.ml +++ b/src/stdune/list.ml @@ -104,3 +104,9 @@ let rec assoc t x = | (k, v) :: t -> if x = k then Some v else assoc t x let singleton x = [x] + +let rec nth t i = + match t, i with + | [], _ -> None + | x :: _, 0 -> Some x + | _ :: xs, i -> nth xs (i - 1) diff --git a/src/stdune/list.mli b/src/stdune/list.mli index b19ace2b..3e310f4a 100644 --- a/src/stdune/list.mli +++ b/src/stdune/list.mli @@ -40,3 +40,5 @@ val compare : 'a t -> 'a t -> compare:('a -> 'a -> Ordering.t) -> Ordering.t val assoc : ('a * 'b) t -> 'a -> 'b option val singleton : 'a -> 'a t + +val nth : 'a t -> int -> 'a option