This commit is contained in:
Simon Cruanes 2017-02-06 18:04:27 +01:00
parent 1e9f5a9a21
commit 827da5a01b
2 changed files with 11 additions and 1 deletions

View file

@ -558,6 +558,7 @@ let sublists_of_len ?(last=fun _ -> None) ?offset n l =
[[1;2];[2;3]] (subs 2 ~offset:1 [1;2;3])
[[1;2];[4;5]] (subs 2 ~offset:3 [1;2;3;4;5;6])
[[1;2;3];[4]] (subs 3 ~last:CCOpt.return [1;2;3;4])
[[1;2]; [3;4]] (subs 2 [1;2;3;4;5])
*)
let take_while p l =

View file

@ -104,13 +104,22 @@ val sublists_of_len :
(** [sublists_of_len n l] returns sub-lists of [l] that have length [n].
By default, these sub-lists are non overlapping:
[sublists_of_len 2 [1;2;3;4;5;6]] returns [[1;2]; [3;4]; [5;6]]
@param offset the number of elements dropped between two consecutive
Examples:
- [sublists_of_len 2 [1;2;3;4;5;6] = [[1;2]; [3;4]; [5;6]]]
- [sublists_of_len 2 ~offset:3 [1;2;3;4;5;6] = [1;2];[4;5]]
- [sublists_of_len 3 ~last:CCOpt.return [1;2;3;4] = [1;2;3];[4]]
- [sublists_of_len 2 [1;2;3;4;5] = [[1;2]; [3;4]]]
@param offset the number of elements skipped between two consecutive
sub-lists. By default it is [n]. If [offset < n], the sub-lists
will overlap; if [offset > n], some elements will not appear at all.
@param last if provided and the last group of elements [g] is such
that [length g < n], [last g] is called. If [last g = Some g'],
[g'] is appended; otherwise [g] is dropped.
If [last = CCOpt.return], it will simply keep the last group.
By default, [last = fun _ -> None], i.e. the last group is dropped if shorter than [n].
@raise Invalid_argument if [offset <= 0] or [n <= 0]
@since NEXT_RELEASE *)