Rewriting C # code using Task.WhenAll in F #

I have the following interface method:

Task<string[]> GetBlobsFromContainer(string containerName); 

and its implementation in C #:

 var container = await _containerClient.GetContainer(containerName); var tasks = container.ListBlobs() .Cast<CloudBlockBlob>() .Select(b => b.DownloadTextAsync()); return await Task.WhenAll(tasks); 

When I try to rewrite it in F #:

 member this.GetBlobsFromContainer(containerName : string) : Task<string[]> = let task = async { let! container = containerClient.GetContainer(containerName) |> Async.AwaitTask return container.ListBlobs() |> Seq.cast<CloudBlockBlob> |> Seq.map (fun b -> b.DownloadTextAsync()) |> ?? } task |> ?? 

I am stuck in the last lines.

How to return to Task<string[]> from F #?

+7
f # c # -to-f # task-parallel-library async-await
source share
2 answers

I had to guess what the containerClient type was and the closest I found was CloudBlobClient (which does not have getContainer: string -> Task<CloubBlobContainer> , but it should not be too difficult to adapt). Then your function might look like this:

 open System open System.Threading.Tasks open Microsoft.WindowsAzure.Storage.Blob open Microsoft.WindowsAzure.Storage let containerClient : CloudBlobClient = null let GetBlobsFromContainer(containerName : string) : Task<string[]> = async { let container = containerClient.GetContainerReference(containerName) return! container.ListBlobs() |> Seq.cast<CloudBlockBlob> |> Seq.map (fun b -> b.DownloadTextAsync() |> Async.AwaitTask) |> Async.Parallel } |> Async.StartAsTask 

I changed the return type as Task<string[]> instead of Task<string seq> , since I assume that you want to save the interface. Otherwise, I suggest getting rid of Task and using Async in F # -one code.

+6
source share

Will this work?

 member this.GetBlobsFromContainer(containerName : string) : Task<string seq> = let aMap fx = async { let! a = x return fa } let task = async { let! container = containerClient.GetContainer(containerName) |> Async.AwaitTask return! container.ListBlobs() |> Seq.cast<CloudBlockBlob> |> Seq.map (fun b -> b.DownloadTextAsync() |> Async.AwaitTask) |> Async.Parallel |> aMap Array.toSeq } task |> Async.StartAsTask 

I had to make some assumptions about containerClient , etc., so I could not verify this, but at least it compiles.

+4
source share

All Articles