Compare commits

...

4 commits

Author SHA1 Message Date
Simon Cruanes
eab2e1d33f
try to make the test_random uniformity test more robust
Some checks failed
format / format (push) Has been cancelled
Build and Test / build (push) Has been cancelled
just increase the sample size.
2025-12-20 11:19:45 -05:00
Simon Cruanes
c72b60fd6f
do simplify the code a bit
closer to the original solution, too. Avoids a redundant `:= []`.
2025-12-20 11:15:15 -05:00
Simon Cruanes
ddc87518a7
another test for lists 2025-12-20 11:08:39 -05:00
Simon Cruanes
15b421c54e
faster List.take_drop thanks to a trick by nojb
https://discuss.ocaml.org/t/efficient-implementation-of-list-split/17616/2
pretty cool.
2025-12-20 11:08:30 -05:00
3 changed files with 33 additions and 9 deletions

View file

@ -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 =

View file

@ -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"

View file

@ -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;;