diff --git a/src/lib_stdlib/option.ml b/src/lib_stdlib/option.ml index eb546f2fc..0585feb02 100644 --- a/src/lib_stdlib/option.ml +++ b/src/lib_stdlib/option.ml @@ -27,6 +27,10 @@ let unopt_map ~f ~default = function | None -> default | Some x -> f x +let unopt_exn err = function + | Some x -> x + | _ -> raise err + let first_some a b = match a, b with | None, None -> None | None, Some v -> Some v diff --git a/src/lib_stdlib/option.mli b/src/lib_stdlib/option.mli index 720c34f5f..0d3a90c5f 100644 --- a/src/lib_stdlib/option.mli +++ b/src/lib_stdlib/option.mli @@ -22,6 +22,9 @@ val unopt: default:'a -> 'a option -> 'a (** [unopt_map f d x] is [y] if [x] is [Some y], [d] if [x] is [None] **) val unopt_map: f:('a -> 'b) -> default:'b -> 'a option -> 'b +(** [unopt_exn exn x] is [y] if [x] is [Some y], or raises [exn] if [x] is [None] *) +val unopt_exn : exn -> 'a option -> 'a + (** First input of form [Some x], or [None] if none **) val first_some: 'a option -> 'a option -> 'a option diff --git a/src/lib_stdlib/tzList.ml b/src/lib_stdlib/tzList.ml index 3b05942f0..8463f2341 100644 --- a/src/lib_stdlib/tzList.ml +++ b/src/lib_stdlib/tzList.ml @@ -133,3 +133,11 @@ let select n l = | x :: xs -> loop (pred n) (x :: acc) xs in loop n [] l + +let shift = function + | [] -> [] + | hd :: tl -> tl@[hd] + +let rec product a b = match a with + | [] -> [] + | hd :: tl -> (List.map (fun x -> (hd , x)) b) @ product tl b diff --git a/src/lib_stdlib/tzList.mli b/src/lib_stdlib/tzList.mli index aa01c1c64..607d86031 100644 --- a/src/lib_stdlib/tzList.mli +++ b/src/lib_stdlib/tzList.mli @@ -13,6 +13,12 @@ val remove: int -> 'a list -> 'a list (** [repeat n x] is a list of [n] [x]'s **) val repeat: int -> 'a -> 'a list +(** [shift (hd :: tl)] computes [tl @ [hd]] *) +val shift : 'a list -> 'a list + +(** [product a b] computes the cartesian product of two lists [a] and [b]. *) +val product : 'a list -> 'b list -> ('a * 'b) list + (** [take_n n l] returns the [n] first elements of [l]. When [compare] is provided, it returns the [n] greatest element of [l]. *) val take_n: ?compare:('a -> 'a -> int) -> int -> 'a list -> 'a list