Skip to content

Commit

Permalink
Replace of_list with lazier version, and to_list to tail recursive ve…
Browse files Browse the repository at this point in the history
…rsion
  • Loading branch information
samer-- committed May 8, 2024
1 parent 31703a1 commit bf0cb79
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 bf0cb79

Please sign in to comment.