Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a simple implementation for "Async.sequence" (the name came from here), but I would like someone to review it for performance, tail call optimization, and functional style.

It has the following signature, which is similar to Async.Parallel:

Async<'a> list -> Async<'a list>

Async.Parallel would be ideal for performance, but it may be important to run the operations in order even though they happen to be async.

let sequence list =
    let rec loop l acc =
        async {
            match l with
            | [] ->
                return List.rev acc
            | h::t ->
                let! h' = h
                return! loop t (h'::acc)
        }
    loop list []
share|improve this question

As far as I can tell, this should be properly tail-call optimized (see this post), but I haven't actually checked.


Consider using AsyncSeq, it could make dealing with asynchronous collections easier.

share|improve this answer
    
I may consider AsyncSeq, but so far sequence is the only Async/List combination I've needed and I didn't see a function in that library to address it. – Kasey Speakman Aug 1 '16 at 18:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.