corbyn3
corbyn3

Reputation: 97

Create alist of Async objects for F# parallel processing

I know this is not very F# like, but I would like to do the following

[a; b; c; d]
|> Async.Parallel
|> Async.RunSynchronously
|> ignore

where a, b, c and d are generated by a function like this

let myasync (stringparameter: string) =  [async{some_code}]

where some_code is for getting information from Internet and saving to database. The function itself should return nothing (returns unit).

I want to create [a,b,c,d] by using

let mutable mylist = []
for i in [1..n] do
   myfunction
   mylist <- List.append mylist myfunction

I got the error: The expression was expected to have type 'a list' but here has type 'string -> Async list'

Any help would be greatly appreciated thanks!

Upvotes: 1

Views: 76

Answers (1)

Stuart
Stuart

Reputation: 5496

The immediate error you are seeing is because List.append wants 2 list parameters, it's not like Array.Add(), you can fix it by doing the following:

let mutable mylist = []
for i in [1..n] do
   myfunction
   mylist <- List.append mylist [myfunction]

However, there are more succinct ways to do this without using mutability. Example:

let mylist =
    [ for i in [1..n] do
            let myfunction() =
                async {
                    // your code
                    return () }
            yield myfunction ]

Upvotes: 3

Related Questions