Merge pull request #365 from jberdine/gar

feat(CCRAL): add `get_and_remove_exn` operation
This commit is contained in:
Simon Cruanes 2021-04-16 10:51:17 -04:00 committed by GitHub
commit 0d9a3b82fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View file

@ -125,6 +125,19 @@ let remove l i = _remove [] l i
[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
| Leaf x -> Leaf (f x)
| Node (x, l, r) -> Node (f x, _map_tree f l, _map_tree f r)

View file

@ -65,7 +65,11 @@ val set : 'a t -> int -> 'a -> 'a t
@raise Invalid_argument if the list has less than [i+1] elements. *)
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. *)
val append : 'a t -> 'a t -> 'a t