mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
add CCList.fold_map
This commit is contained in:
parent
42c4c310b4
commit
5e6ade9f68
2 changed files with 24 additions and 0 deletions
|
|
@ -152,6 +152,25 @@ let rec fold_while f acc = function
|
|||
fold_while (fun acc b -> if b then acc+1, `Continue else acc, `Stop) 0 [true;true;false;true] = 2
|
||||
*)
|
||||
|
||||
let fold_map f acc l =
|
||||
let rec aux f acc map_acc l = match l with
|
||||
| [] -> acc, List.rev map_acc
|
||||
| x :: l' ->
|
||||
let acc, y = f acc x in
|
||||
aux f acc (y :: map_acc) l'
|
||||
in
|
||||
aux f acc [] l
|
||||
|
||||
(*$=
|
||||
(6, ["1"; "2"; "3"]) \
|
||||
(fold_map (fun acc x->acc+x, string_of_int x) 0 [1;2;3])
|
||||
*)
|
||||
|
||||
(*$Q
|
||||
Q.(list int) (fun l -> \
|
||||
fold_map (fun acc x -> x::acc, x) [] l = (List.rev l, l))
|
||||
*)
|
||||
|
||||
let init len f =
|
||||
let rec init_rec acc i f =
|
||||
if i=0 then f i :: acc
|
||||
|
|
|
|||
|
|
@ -66,6 +66,11 @@ val fold_while : ('a -> 'b -> 'a * [`Stop | `Continue]) -> 'a -> 'b t -> 'a
|
|||
indicated by the accumulator
|
||||
@since 0.8 *)
|
||||
|
||||
val fold_map : ('acc -> 'a -> 'acc * 'b) -> 'acc -> 'a list -> 'acc * 'b list
|
||||
(** [fold_map f acc l] is a [fold_left]-like function, but it also maps the
|
||||
list to another list.
|
||||
@since NEXT_RELEASE *)
|
||||
|
||||
val init : int -> (int -> 'a) -> 'a t
|
||||
(** Similar to {!Array.init}
|
||||
@since 0.6 *)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue