From ed583b7651f4656f3a1bf0ce5b2e744b7bedf2aa Mon Sep 17 00:00:00 2001 From: Jeremie Dimino Date: Sat, 2 Jun 2018 16:20:41 +0100 Subject: [PATCH] Add List.assoc Signed-off-by: Jeremie Dimino --- src/main.ml | 2 +- src/stdune/list.ml | 5 +++++ src/stdune/list.mli | 2 ++ src/utils.ml | 6 +++--- test/blackbox-tests/cram.mll | 6 +++--- 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/main.ml b/src/main.ml index 2c70393d..56d38f49 100644 --- a/src/main.ml +++ b/src/main.ml @@ -218,7 +218,7 @@ let bootstrap () = Arg.Symbol (List.map Config.Display.all ~f:fst, fun s -> - display := Some (List.assoc s Config.Display.all)) + display := List.assoc Config.Display.all s) in let concurrency = ref None in let concurrency_arg x = diff --git a/src/stdune/list.ml b/src/stdune/list.ml index 06c7cb7d..5342445f 100644 --- a/src/stdune/list.ml +++ b/src/stdune/list.ml @@ -97,3 +97,8 @@ let rec compare a b ~compare:f : Ordering.t = match (f x y : Ordering.t) with | Eq -> compare a b ~compare:f | ne -> ne + +let rec assoc t x = + match t with + | [] -> None + | (k, v) :: t -> if x = k then Some v else assoc t x diff --git a/src/stdune/list.mli b/src/stdune/list.mli index 4cfe2dfd..c636079c 100644 --- a/src/stdune/list.mli +++ b/src/stdune/list.mli @@ -36,3 +36,5 @@ val sort : 'a t -> compare:('a -> 'a -> Ordering.t) -> 'a t val stable_sort : 'a t -> compare:('a -> 'a -> Ordering.t) -> 'a t val compare : 'a t -> 'a t -> compare:('a -> 'a -> Ordering.t) -> Ordering.t + +val assoc : ('a * 'b) t -> 'a -> 'b option diff --git a/src/utils.ml b/src/utils.ml index 065e4556..c8fb0a8b 100644 --- a/src/utils.ml +++ b/src/utils.ml @@ -60,9 +60,9 @@ let signal_name = ] in fun n -> - match List.assoc n table with - | exception Not_found -> sprintf "%d\n" n - | s -> s + match List.assoc table n with + | None -> sprintf "%d\n" n + | Some s -> s type target_kind = | Regular of string * Path.t diff --git a/test/blackbox-tests/cram.mll b/test/blackbox-tests/cram.mll index ddc953bf..fdfc0e0d 100644 --- a/test/blackbox-tests/cram.mll +++ b/test/blackbox-tests/cram.mll @@ -24,9 +24,9 @@ and postprocess tbl b = parse | eof { Buffer.contents b } | ([^ '/'] as c) (ext as e) { Buffer.add_char b c; - begin match List.assoc e tbl with - | res -> Buffer.add_string b res - | exception Not_found -> Buffer.add_string b e + begin match List.assoc tbl e with + | Some res -> Buffer.add_string b res + | None -> Buffer.add_string b e end; postprocess tbl b lexbuf }