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

Proposing AsyncSeq.ofSeqAsync and AsyncSeq.concat #99

Open
blumu opened this issue Dec 29, 2018 · 3 comments
Open

Proposing AsyncSeq.ofSeqAsync and AsyncSeq.concat #99

blumu opened this issue Dec 29, 2018 · 3 comments

Comments

@blumu
Copy link

blumu commented Dec 29, 2018

Description

Would the maintainers consider including the following two helpers:

  • We already have AsyncSeq.init and AsyncSeq.unfoldAsync to generate new
    AsyncSeq, but I could not find any helper to create one from an
    existing sequence of asynchronous computations (seq<Async<_>>).
    The following helper ofSeqAsync would help with that:
module AsyncSeq =
    let ofSeqAsync (s:seq<Async<'t>>) : AsyncSeq<'t> =
        asyncSeq {
            for asyncElement in s do
                let! v =  asyncElement
                yield v
        }
  • Using AsyncSeq.concatSeq, it's currently possible to concatenate sequences of AsyncSeq (as in seq<AsyncSeq<_>>) but not an AsyncSeq of AsyncSeqs.
    I propose adding the following function.
module AsyncSeq =
    let concat (s:AsyncSeq<AsyncSeq<'t>>) : AsyncSeq<'t> =
        asyncSeq {
            for innerSeq in s do
                for e in innerSeq do
                    yield e
        }

Example use case

Suppose you are given:

  • enumerateFiles : string -> Async<AsyncSeq<string>> that asynchronously returns
    an asynchronous enumeration of all files under a given directory;
  • and enumerateDirectories : string -> AsyncSeq<string> that
    asynchronously enumerates all sub-directories under a given directory;

and you want to enumerate all files under all directories under c:\
then with the two helpers you could write it as follows:

enumerateDirectories "c:\"       // : Async<AsyncSeq<string>>
|> Seq.map enumerateFiles       // : seq<Async<AsyncSeq<string>>>
|> AsyncSeq.ofSeqAsync          // : AsyncSeq<AsyncSeq<string>>
|> AsyncSeq.concat              // : AsyncSeq<string> 

I undertand that the function AsyncSeq.mergeAll could be used to achieve the same goal, except that it would require first enumerating all directories and convert them into a list, before doing the concatenation.

Or perhaps there is a better way to do this?

@dsyme
Copy link
Contributor

dsyme commented Jan 8, 2019

Yes, these are both sensible design additions

@eulerfx
Copy link
Contributor

eulerfx commented Jan 9, 2019

Yes, I think all of these make sense, PR would be great :)

@blumu
Copy link
Author

blumu commented Jan 13, 2019

@eulerfx @dsyme I've submitted PR #102 to add those two helpers. Let me know if the unit tests meet your expectations or if you think more is needed.

wilsoncg added a commit that referenced this issue Jan 19, 2019
Add ofSeqAsync and concat #99
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants