let concat xs =
let rec find_next_non_empty_stream xs =
match peek xs with
| Some stream when is_empty stream ->
junk xs ;
find_next_non_empty_stream xs
| x -> x
in
let current = ref (empty ()) in
let aux _ =
match next !current with
| None -> (
match find_next_non_empty_stream xs with
| None -> None
| Some stream ->
current := stream ;
next stream
)
| x -> x
in
from aux