Async.sequence implementation 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&lt;'a&gt; list -&gt; Async&lt;'a list&gt;


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
            | [] -&gt;
                return List.rev acc
            | h::t -&gt;
                let! h' = h
                return! loop t (h'::acc)
        }
    loop list []


Update

I came up with a minimal implementation built from other basic operations: map + apply (to get map2) and retn. cons is just a helper for the :: operation, since that is not actually a function in F#.

let sequenceList list =
    List.foldBack (map2 cons) list (retn [])


It is probably better to use a library already built, but I like defining these functions myself to better understand how the operations relate. Now that I have the formulation of sequence, I can literally copy and paste this code into my Result module which also has its own map2 and retn defined, and it will work the same.
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.