mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-09 12:45:34 -05:00
Merge pull request #365 from jberdine/gar
feat(CCRAL): add `get_and_remove_exn` operation
This commit is contained in:
commit
0d9a3b82fa
2 changed files with 18 additions and 1 deletions
|
|
@ -125,6 +125,19 @@ let remove l i = _remove [] l i
|
||||||
[1;2;4] (to_list @@ remove (of_list [1;2;3;4]) 2)
|
[1;2;4] (to_list @@ remove (of_list [1;2;3;4]) 2)
|
||||||
*)
|
*)
|
||||||
|
|
||||||
|
let rec _get_and_remove_exn prefix l i =
|
||||||
|
let x, l' = front_exn l in
|
||||||
|
if i=0
|
||||||
|
then (x, List.fold_left (fun l x -> cons x l) l' prefix)
|
||||||
|
else _get_and_remove_exn (x::prefix) l' (i-1)
|
||||||
|
|
||||||
|
let get_and_remove_exn l i =
|
||||||
|
_get_and_remove_exn [] l i
|
||||||
|
|
||||||
|
(*$= & ~printer:Q.Print.(pair int (list int))
|
||||||
|
(3,[1;2;4]) (CCPair.map_snd to_list @@ get_and_remove_exn (of_list [1;2;3;4]) 2)
|
||||||
|
*)
|
||||||
|
|
||||||
let rec _map_tree f t = match t with
|
let rec _map_tree f t = match t with
|
||||||
| Leaf x -> Leaf (f x)
|
| Leaf x -> Leaf (f x)
|
||||||
| Node (x, l, r) -> Node (f x, _map_tree f l, _map_tree f r)
|
| Node (x, l, r) -> Node (f x, _map_tree f l, _map_tree f r)
|
||||||
|
|
|
||||||
|
|
@ -65,7 +65,11 @@ val set : 'a t -> int -> 'a -> 'a t
|
||||||
@raise Invalid_argument if the list has less than [i+1] elements. *)
|
@raise Invalid_argument if the list has less than [i+1] elements. *)
|
||||||
|
|
||||||
val remove : 'a t -> int -> 'a t
|
val remove : 'a t -> int -> 'a t
|
||||||
(** [remove l i] removes the [i]-th element of [v].
|
(** [remove l i] removes the [i]-th element of [l].
|
||||||
|
@raise Invalid_argument if the list has less than [i+1] elements. *)
|
||||||
|
|
||||||
|
val get_and_remove_exn : 'a t -> int -> 'a * 'a t
|
||||||
|
(** [get_and_remove_exn l i] accesses and removes the [i]-th element of [l].
|
||||||
@raise Invalid_argument if the list has less than [i+1] elements. *)
|
@raise Invalid_argument if the list has less than [i+1] elements. *)
|
||||||
|
|
||||||
val append : 'a t -> 'a t -> 'a t
|
val append : 'a t -> 'a t -> 'a t
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue