Skip to content

Commit a9dcafc

Browse files
Merge pull request #1019 from samer--/improve_list_conversions
Make Lwt_seq.of_list lazier, Lwt_seq.to_list tail recursive
2 parents 2d27668 + bf0cb79 commit a9dcafc

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

src/core/lwt_seq.ml

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -283,22 +283,16 @@ let unfold_lwt f u () =
283283
| None -> return_nil
284284
| Some (x, u') -> Lwt.return (Cons (x, unfold_lwt f u'))
285285

286-
let rec of_list = function
287-
| [] -> empty
288-
| h :: t -> cons h (of_list t)
289-
290-
let rec to_list seq =
291-
seq () >>= function
292-
| Nil -> Lwt.return_nil
293-
| Cons (x, next) ->
294-
let+ l = to_list next in
295-
x :: l
296-
let to_list seq =
297-
Lwt.apply seq () >>= function
298-
| Nil -> Lwt.return_nil
299-
| Cons (x, next) ->
300-
let+ l = to_list next in
301-
x :: l
286+
let rec of_list l () =
287+
Lwt.return (match l with [] -> Nil | h :: t -> Cons (h, of_list t))
288+
289+
let to_list (seq : 'a t) =
290+
let rec aux f seq =
291+
Lwt.bind (seq ()) (function
292+
| Nil -> Lwt.return (f [])
293+
| Cons (h, t) -> aux (fun x -> f (h :: x)) t)
294+
in
295+
aux (fun x -> x) (Lwt.apply seq)
302296

303297
let rec of_seq seq () =
304298
match seq () with

0 commit comments

Comments
 (0)