Skip to content

Commit

Permalink
Seq: simplify the implementation of concat, concat_map
Browse files Browse the repository at this point in the history
suggestion from Ulugbek Abduallev
  • Loading branch information
gasche committed Apr 17, 2021
1 parent c203baf commit effa62f
Showing 1 changed file with 2 additions and 11 deletions.
13 changes: 2 additions & 11 deletions stdlib/seq.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,12 @@ let rec filter f seq () = match seq() with
let rec concat seq () = match seq () with
| Nil -> Nil
| Cons (x, next) ->
append_concat x next ()
(* [append_concat seq tail] is [append seq (concat tail)] *)
and append_concat seq tail () = match seq () with
| Nil -> concat tail ()
| Cons (x, next) -> Cons (x, append_concat next tail)
append x (concat next) ()

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

let concat_map = flat_map

Expand Down

0 comments on commit effa62f

Please sign in to comment.