mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
CCList: add unfold
This commit is contained in:
parent
50b478366f
commit
73e68dae7c
4 changed files with 35 additions and 0 deletions
|
|
@ -281,6 +281,11 @@ let fold_flat_map_i f acc l =
|
||||||
in
|
in
|
||||||
aux f acc 0 [] l
|
aux f acc 0 [] l
|
||||||
|
|
||||||
|
let rec unfold f seed =
|
||||||
|
match f seed with
|
||||||
|
| None -> []
|
||||||
|
| Some (v, next) -> v :: unfold f next
|
||||||
|
|
||||||
[@@@iflt 5.1]
|
[@@@iflt 5.1]
|
||||||
|
|
||||||
(* keep this because it's tailrec for < 5.1 *)
|
(* keep this because it's tailrec for < 5.1 *)
|
||||||
|
|
|
||||||
|
|
@ -102,6 +102,16 @@ val fold_flat_map_i :
|
||||||
list to a list of lists that is then [flatten]'d.
|
list to a list of lists that is then [flatten]'d.
|
||||||
@since 2.8 *)
|
@since 2.8 *)
|
||||||
|
|
||||||
|
val unfold : ('seed -> ('b * 'seed) option) -> 'seed -> 'b list
|
||||||
|
(** [unfold f init] builds up a list from a seed value.
|
||||||
|
When [f] produces [Some (next_seed, value)], [value] is added to the output list and
|
||||||
|
[next_seed] is used in the next call to [f]. However,
|
||||||
|
when [f] produces [None], list production ends.
|
||||||
|
{b NOTE} if [f] never produces [None], then a {b stack overflow will occur}. Therefore,
|
||||||
|
great care must be taken to ensure that [f] will produce [None].
|
||||||
|
@since 3.13
|
||||||
|
*)
|
||||||
|
|
||||||
val count : ('a -> bool) -> 'a list -> int
|
val count : ('a -> bool) -> 'a list -> int
|
||||||
(** [count p l] counts how many elements of [l] satisfy predicate [p].
|
(** [count p l] counts how many elements of [l] satisfy predicate [p].
|
||||||
@since 1.5, but only
|
@since 1.5, but only
|
||||||
|
|
|
||||||
|
|
@ -135,6 +135,16 @@ val fold_flat_map_i :
|
||||||
list to a list of lists that is then [flatten]'d.
|
list to a list of lists that is then [flatten]'d.
|
||||||
@since 2.8 *)
|
@since 2.8 *)
|
||||||
|
|
||||||
|
val unfold : f:('seed -> ('b * 'seed) option) -> init:'seed -> 'b list
|
||||||
|
(** [unfold ~f ~init] builds up a list from a seed value.
|
||||||
|
When [f] produces [Some (next_seed, value)], [value] is added to the output list and
|
||||||
|
[next_seed] is used in the next call to [f]. However,
|
||||||
|
when [f] produces [None], list production ends.
|
||||||
|
{b NOTE} if [f] never produces [None], then a {b stack overflow will occur}. Therefore,
|
||||||
|
great care must be taken to ensure that [f] will produce [None].
|
||||||
|
@since 3.13
|
||||||
|
*)
|
||||||
|
|
||||||
val count : f:('a -> bool) -> 'a list -> int
|
val count : f:('a -> bool) -> 'a list -> int
|
||||||
(** [count ~f l] counts how many elements of [l] satisfy predicate [f].
|
(** [count ~f l] counts how many elements of [l] satisfy predicate [f].
|
||||||
@since 1.5, but only
|
@since 1.5, but only
|
||||||
|
|
|
||||||
|
|
@ -131,6 +131,16 @@ q
|
||||||
= (List.rev l, flat_map (fun x -> [ x; x + 10 ]) l))
|
= (List.rev l, flat_map (fun x -> [ x; x + 10 ]) l))
|
||||||
;;
|
;;
|
||||||
|
|
||||||
|
t @@ fun () ->
|
||||||
|
let f x =
|
||||||
|
if x <= 5 then
|
||||||
|
Some (2 * x, x + 1)
|
||||||
|
else
|
||||||
|
None
|
||||||
|
in
|
||||||
|
unfold f 0 = [ 0; 2; 4; 6; 8; 10 ]
|
||||||
|
;;
|
||||||
|
|
||||||
t @@ fun () -> init 0 (fun _ -> 0) = [];;
|
t @@ fun () -> init 0 (fun _ -> 0) = [];;
|
||||||
t @@ fun () -> init 1 (fun x -> x) = [ 0 ];;
|
t @@ fun () -> init 1 (fun x -> x) = [ 0 ];;
|
||||||
t @@ fun () -> init 1000 (fun x -> x) = 0 -- 999;;
|
t @@ fun () -> init 1000 (fun x -> x) = 0 -- 999;;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue