Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seq.concat (+ Seq.concat_map as alias to Seq.flat_map) #10352

Merged
merged 1 commit into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ Working version
(Oghenevwogaga Ebresafe, review by Nicolás Ojeda Bär,
Gabriel Scherer and Xavier Van de Woestyne)

- #10352: Seq.(concat : 'a t t -> 'a t)
Seq.concat_map as an alias to Seq.flat_map,
(Gabriel Scherer, review by Ulugbek Abdullaev and Daniel Bünzli
and Nicolás Ojeda Bär and Florian Angeletti)

### Other libraries:

- #10047: Add `Unix.realpath`
Expand Down
13 changes: 7 additions & 6 deletions stdlib/seq.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,17 @@ let rec filter f seq () = match seq() with
then Cons (x, filter f next)
else filter f next ()

let rec flat_map f seq () = match seq () with
let rec concat seq () = match seq () with
| Nil -> Nil
| Cons (x, next) ->
flat_map_app f (f x) next ()
append x (concat next) ()

(* this is [append seq (flat_map f tail)] *)
and flat_map_app f seq tail () = match seq () with
| Nil -> flat_map f tail ()
let rec flat_map f seq () = match seq () with
| Nil -> Nil
| Cons (x, next) ->
Cons (x, flat_map_app f next tail)
append (f x) (flat_map f next) ()

let concat_map = flat_map

let fold_left f acc seq =
let rec aux f acc seq = match seq () with
Expand Down
12 changes: 12 additions & 0 deletions stdlib/seq.mli
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,24 @@ val filter_map : ('a -> 'b option) -> 'a t -> 'b t
This transformation is lazy, it only applies when the result is
traversed. *)

val concat : 'a t t -> 'a t
(** concatenate a sequence of sequences.

@since 4.13
*)

val flat_map : ('a -> 'b t) -> 'a t -> 'b t
(** Map each element to a subsequence, then return each element of this
sub-sequence in turn.
This transformation is lazy, it only applies when the result is
traversed. *)

val concat_map : ('a -> 'b t) -> 'a t -> 'b t
(** Alias for {!flat_map}.

@since 4.13
*)

val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a
(** Traverse the sequence from left to right, combining each element with the
accumulator using the given function.
Expand Down
7 changes: 7 additions & 0 deletions testsuite/tests/lib-seq/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,11 @@ let () =
()
;;

(* concat *)
let () =
assert (
List.concat [[1]; []; [2; 3];]
= (let (!?) = List.to_seq in
List.of_seq (Seq.concat !?[!?[1]; !?[]; !?[2; 3]])))

let () = print_endline "OK";;