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

Make Lwt_seq.of_list lazier, Lwt_seq.to_list tail recursive #1019

Merged
merged 1 commit into from
May 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading