add CCList.chunks

This commit is contained in:
Simon Cruanes 2021-01-24 11:18:53 -05:00
parent e037ca1afa
commit ae886c2f08
3 changed files with 25 additions and 0 deletions

View file

@ -922,6 +922,17 @@ let sublists_of_len ?(last=fun _ -> None) ?offset n l =
[[1;2]; [3;4]] (subs 2 [1;2;3;4;5])
*)
let chunks n l = sublists_of_len ~last:(fun x -> Some x) n l
(*$Q
Q.(small_list small_int) (fun l -> \
l = (chunks 3 l |> List.flatten))
Q.(small_list small_int) (fun l -> \
l = (chunks 5 l |> List.flatten))
Q.(small_list small_int) (fun l -> \
List.for_all (fun u -> List.length u <= 5) (chunks 5 l))
*)
let intersperse x l =
let rec aux_direct i x l = match l with
| [] -> []

View file

@ -323,6 +323,13 @@ val sublists_of_len :
@since 1.0, but only
@since 1.5 with labels *)
val chunks : int -> 'a list -> 'a list list
(** [chunks n l] returns consecutives chunks of size at most [n] from [l].
Each item of [l] will occur in exactly one chunk. Only the last chunk
might be of length smaller than [n].
Invariant: [(chunks n l |> List.flatten) = l].
@since NEXT_RELEASE *)
val intersperse : 'a -> 'a list -> 'a list
(** [intersperse x l] inserts the element [x] between adjacent elements of the list [l].
@since 2.1, but only

View file

@ -327,6 +327,13 @@ val sublists_of_len :
@since 1.0, but only
@since 1.5 with labels *)
val chunks : int -> 'a list -> 'a list list
(** [chunks n l] returns consecutives chunks of size at most [n] from [l].
Each item of [l] will occur in exactly one chunk. Only the last chunk
might be of length smaller than [n].
Invariant: [(chunks n l |> List.flatten) = l].
@since NEXT_RELEASE *)
val intersperse : x:'a -> 'a list -> 'a list
(** [intersperse ~x l] inserts the element [x] between adjacent elements of the list [l].
@since 2.1, but only