Skip to content

Commit

Permalink
Merge pull request #1019 from samer--/improve_list_conversions
Browse files Browse the repository at this point in the history
Make Lwt_seq.of_list lazier, Lwt_seq.to_list tail recursive
  • Loading branch information
raphael-proust authored May 14, 2024
2 parents 2d27668 + bf0cb79 commit a9dcafc
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions src/core/lwt_seq.ml
Original file line number Diff line number Diff line change
Expand Up @@ -283,22 +283,16 @@ let unfold_lwt f u () =
| None -> return_nil
| Some (x, u') -> Lwt.return (Cons (x, unfold_lwt f u'))

let rec of_list = function
| [] -> empty
| h :: t -> cons h (of_list t)

let rec to_list seq =
seq () >>= function
| Nil -> Lwt.return_nil
| Cons (x, next) ->
let+ l = to_list next in
x :: l
let to_list seq =
Lwt.apply seq () >>= function
| Nil -> Lwt.return_nil
| Cons (x, next) ->
let+ l = to_list next in
x :: l
let rec of_list l () =
Lwt.return (match l with [] -> Nil | h :: t -> Cons (h, of_list t))

let to_list (seq : 'a t) =
let rec aux f seq =
Lwt.bind (seq ()) (function
| Nil -> Lwt.return (f [])
| Cons (h, t) -> aux (fun x -> f (h :: x)) t)
in
aux (fun x -> x) (Lwt.apply seq)

let rec of_seq seq () =
match seq () with
Expand Down

0 comments on commit a9dcafc

Please sign in to comment.