mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
bugfix in CCList.take
This commit is contained in:
parent
e61039152f
commit
a8dc42024b
1 changed files with 10 additions and 1 deletions
|
|
@ -144,7 +144,10 @@ let take n l =
|
||||||
let rec direct i n l = match l with
|
let rec direct i n l = match l with
|
||||||
| [] -> []
|
| [] -> []
|
||||||
| _ when i=0 -> safe n [] l
|
| _ when i=0 -> safe n [] l
|
||||||
| x::l' -> x :: direct (i-1) (n-1) l'
|
| x::l' ->
|
||||||
|
if n > 0
|
||||||
|
then x :: direct (i-1) (n-1) l'
|
||||||
|
else []
|
||||||
and safe n acc l = match l with
|
and safe n acc l = match l with
|
||||||
| [] -> List.rev acc
|
| [] -> List.rev acc
|
||||||
| _ when n=0 -> List.rev acc
|
| _ when n=0 -> List.rev acc
|
||||||
|
|
@ -152,6 +155,12 @@ let take n l =
|
||||||
in
|
in
|
||||||
direct _direct_depth n l
|
direct _direct_depth n l
|
||||||
|
|
||||||
|
(*$T
|
||||||
|
take 2 [1;2;3;4;5] = [1;2]
|
||||||
|
take 10_000 (range 0 100_000) |> List.length = 10_000
|
||||||
|
take 10_000 (range 0 2_000) = range 0 2_000
|
||||||
|
*)
|
||||||
|
|
||||||
let rec drop n l = match l with
|
let rec drop n l = match l with
|
||||||
| [] -> []
|
| [] -> []
|
||||||
| _ when n=0 -> l
|
| _ when n=0 -> l
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue