1

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!

asked Dec 5, 2019 at 14:51
2
  • Where do you get the value for stringparameter? Is it the same for all generated functions? Commented Dec 5, 2019 at 17:19
  • Stuart''s second solution works perfectly for me. I was able to get a list of <Async>. I tried to do something similar but did not know I had to use the yield keyword. @SergeyBerezovskiy stringparameter is actually from a global variable which absolutely does not play nice with async parallel. I am rewriting the function now so that it will work more F# like. As of now, I do not get the intended effect but I am marking this question as answered because this step is finished. I have another question for the rewrite actually =( Commented Dec 6, 2019 at 2:26

1 Answer 1

3

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 ]
answered Dec 5, 2019 at 16:11
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.