From 39ca91cd5733f4c86687f892fafe1c9f043dca89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Sat, 2 Jun 2018 16:33:45 +0200 Subject: [PATCH] Stdlib: add `List.split_n` --- src/lib_stdlib/tzList.ml | 8 +++++--- src/lib_stdlib/tzList.mli | 2 ++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/lib_stdlib/tzList.ml b/src/lib_stdlib/tzList.ml index c091bc769..98f14760e 100644 --- a/src/lib_stdlib/tzList.ml +++ b/src/lib_stdlib/tzList.ml @@ -69,13 +69,15 @@ let rec remove nb = function let rec repeat n x = if n <= 0 then [] else x :: repeat (pred n) x -let take_n_unsorted n l = +let split_n n l = let rec loop acc n = function - | [] -> l - | _ when n <= 0 -> List.rev acc + | [] -> l, [] + | rem when n <= 0 -> List.rev acc, rem | x :: xs -> loop (x :: acc) (pred n) xs in loop [] n l +let take_n_unsorted n l = fst (split_n n l) + module Bounded(E: Set.OrderedType) : sig type t diff --git a/src/lib_stdlib/tzList.mli b/src/lib_stdlib/tzList.mli index 31099e35d..cfa313c45 100644 --- a/src/lib_stdlib/tzList.mli +++ b/src/lib_stdlib/tzList.mli @@ -23,6 +23,8 @@ val product : 'a list -> 'b list -> ('a * 'b) list is provided, it returns the [n] greatest element of [l]. *) val take_n: ?compare:('a -> 'a -> int) -> int -> 'a list -> 'a list +val split_n: int -> 'a list -> 'a list * 'a list + (** Bounded sequence: keep only the [n] greatest elements. *) module Bounded(E: Set.OrderedType) : sig type t