mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2026-03-10 23:38:32 -04:00
Compare commits
4 commits
c1b13f1c7f
...
eab2e1d33f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eab2e1d33f | ||
|
|
c72b60fd6f | ||
|
|
ddc87518a7 | ||
|
|
15b421c54e |
3 changed files with 33 additions and 9 deletions
|
|
@ -778,6 +778,12 @@ let sorted_diff_uniq ~cmp l1 l2 =
|
|||
in
|
||||
recurse ~cmp [] l1 l2
|
||||
|
||||
let rec drop n l =
|
||||
match l with
|
||||
| [] -> []
|
||||
| _ when n = 0 -> l
|
||||
| _ :: l' -> drop (n - 1) l'
|
||||
|
||||
[@@@iflt 4.14]
|
||||
|
||||
let take n l =
|
||||
|
|
@ -798,6 +804,8 @@ let take n l =
|
|||
in
|
||||
direct direct_depth_default_ n l
|
||||
|
||||
let take_drop n l = take n l, drop n l
|
||||
|
||||
[@@@else_]
|
||||
|
||||
let[@tail_mod_cons] rec take n l =
|
||||
|
|
@ -809,20 +817,29 @@ let[@tail_mod_cons] rec take n l =
|
|||
else
|
||||
[]
|
||||
|
||||
[@@@endif]
|
||||
let take_drop n l =
|
||||
(* fun idea from nojb in
|
||||
https://discuss.ocaml.org/t/efficient-implementation-of-list-split/17616/2 *)
|
||||
let[@tail_mod_cons] rec loop (res_drop : _ ref) n l =
|
||||
match l with
|
||||
| [] -> []
|
||||
| x :: tl ->
|
||||
if n = 0 then (
|
||||
res_drop := l;
|
||||
[]
|
||||
) else
|
||||
x :: loop res_drop (n - 1) tl
|
||||
in
|
||||
let res_drop = ref [] in
|
||||
let res_take = loop res_drop n l in
|
||||
res_take, !res_drop
|
||||
|
||||
let rec drop n l =
|
||||
match l with
|
||||
| [] -> []
|
||||
| _ when n = 0 -> l
|
||||
| _ :: l' -> drop (n - 1) l'
|
||||
[@@@endif]
|
||||
|
||||
let hd_tl = function
|
||||
| [] -> failwith "hd_tl"
|
||||
| x :: l -> x, l
|
||||
|
||||
let take_drop n l = take n l, drop n l
|
||||
|
||||
let sublists_of_len ?(last = fun _ -> None) ?offset n l =
|
||||
if n < 1 then invalid_arg "sublists_of_len: n must be > 0";
|
||||
let offset =
|
||||
|
|
|
|||
|
|
@ -31,5 +31,5 @@ let uniformity_test ?(size_hint = 10) k rng st =
|
|||
|
||||
let () =
|
||||
let st = Random.State.make_self_init () in
|
||||
let ok = run ~st (uniformity_test 50_000 (split_list 10 ~len:3)) in
|
||||
let ok = run ~st (uniformity_test 500_000 (split_list 10 ~len:3)) in
|
||||
if not ok then failwith "uniformity check failed"
|
||||
|
|
|
|||
|
|
@ -721,6 +721,13 @@ q
|
|||
let i = abs i in
|
||||
let l1, l2 = take_drop i l in
|
||||
l1 @ l2 = l)
|
||||
;;
|
||||
|
||||
q
|
||||
(Q.pair (Q.list Q.small_int) Q.int)
|
||||
(fun (l, i) ->
|
||||
let i = abs i in
|
||||
take_drop i l = (take i l, drop i l))
|
||||
|
||||
let subs = sublists_of_len;;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue