Skip to content

Commit

Permalink
Merge pull request #544 from UnixJunkie/batlist_remove_at
Browse files Browse the repository at this point in the history
added remove_at with its unit tests, ocamldoc and interface
  • Loading branch information
c-cube committed Mar 5, 2014
2 parents 8433b41 + c3a4246 commit 3aa7fbe
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/batList.ml
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,31 @@ let remove_assq x lst =
loop dummy lst;
dummy.tl

let remove_at i lst =
let rec loop dst i = function
| [] -> invalid_arg "BatList.remove_at"
| x :: xs ->
if i = 0 then
dst.tl <- xs
else
loop (Acc.accum dst x) (i - 1) xs
in
if i < 0 then
invalid_arg "BatList.remove_at"
else
let dummy = Acc.dummy () in
loop dummy i lst;
dummy.tl

(*$T remove_at
try ignore (remove_at 0 []) ; false with Invalid_argument _ -> true
try ignore (remove_at 1 [0]); false with Invalid_argument _ -> true
remove_at 0 [0] = []
remove_at 0 [0; 1; 2] = [1; 2]
remove_at 1 [0; 1; 2] = [0; 2]
remove_at 2 [0; 1; 2] = [0; 1]
*)

let rfind p l = find p (rev l)

let find_all p l =
Expand Down
10 changes: 9 additions & 1 deletion src/batList.mli
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,14 @@ val remove_if : ('a -> bool) -> 'a list -> 'a list
(** [remove_if cmp l] is similar to [remove], but with [cmp] used
instead of ( = ). *)

val remove_at : int -> 'a list -> 'a list
(** [remove_at i l] returns the list [l] without the element at index [i].
@raise Invalid_argument if [i] is outside of [l] size bounds.
@since NEXT_RELEASE
*)

val remove_all : 'a list -> 'a -> 'a list
(** [remove_all l x] is similar to [remove] but removes all elements that
are equal to [x] and not only the first one. *)
Expand All @@ -544,7 +552,7 @@ val ntake : int -> 'a list -> 'a list list
Each list in the result has size n, except the last
one which may have fewer elements in case [l] was too short.
Example: [ntake 2 [1; 2; 3; 4; 5] = [[1; 2]; [3; 4]; [5]]]
@since 2.2.0 *)

val drop : int -> 'a list -> 'a list
Expand Down

0 comments on commit 3aa7fbe

Please sign in to comment.