Some eloquency questions:
A. How to add a list that was formed from parallel processing directly to the a Concurrent results array in an eloquent way.
let results = System.Collections.Concurrent.ConcurrentBag<string>()
let tasks = System.Collections.Generic.List<string>()
tasks.add("a")
tasks.add("b")
let answers = tasks
|> Seq.map asyncRequest
|> Async.Parallel
|> Async.RunSynchronously
|> Array.toList
Array.append results answers
Attempt Is there a way to append via pipe operator?
let answers = tasks
|> Seq.map asyncRequest
|> Async.Parallel
|> Async.RunSynchronously
|> Array.append results
B. Is there a way to add items via List constructor?
let tasks = System.Collections.Generic.List<string>()
tasks.add("a")
tasks.add("b")
C. Is there a way to construct a queue from array using Queue constructor?
let items: string[] = [|"a", "b", "c"|]
let jobs = System.Collections.Generic.Queue<string>()
items |> Array.map jobs.Enqueue |> ignore
-
2I think you better ask all these questions here: chat.stackoverflow.com/rooms/51909/fSzer– Szer2018年04月03日 08:42:47 +00:00Commented Apr 3, 2018 at 8:42
-
I don't see an option to post a message there. I can see the chat room. I think I'm not authorized in the chat roomDeveloper11– Developer112018年04月03日 09:06:20 +00:00Commented Apr 3, 2018 at 9:06
-
1@Developer11 - You need to have at least 20 reputation points to talk in chat rooms, so you should be able to. When I clicked on the chat room, there was a big bar across the bottom of the screen with a text box and two buttons: "Send" and "Upload...". The "Send" button would post a chat message. Do you see that bar and those buttons when you click on the chat room? If you don't, something unusual is going on. Update: Never mind, just noticed that you posted several times in that chat room, so apparently you figured it out.rmunn– rmunn2018年04月03日 16:40:07 +00:00Commented Apr 3, 2018 at 16:40
-
yes initially, the problem was clicking on the notification took me to chat transcript that doesn't offer sending a message, only going through all rooms and then F# worked.Developer11– Developer112018年04月03日 16:59:28 +00:00Commented Apr 3, 2018 at 16:59
1 Answer 1
A. you can't use Array.append on results, because results is a ConcurrentBag, but Array.append expects its argument to be an Array. To add stuff to ConcurrentBag, use its Add method. Add items one by one:
tasks
|> Seq.map asyncRequest
|> Async.Parallel
|> Async.RunSynchronously
|> Array.iter results.Add
Adding items one by one is a little inefficient. If your ConcurrentBag is really created right in the same function, as your example shows, you may consider using its constructor that takes an IEnumerable<T>:
let answers = tasks
|> Seq.map asyncRequest
|> Async.Parallel
|> Async.RunSynchronously
let results = System.Collections.Concurrent.ConcurrentBag<string>( answers )
B. yes, there is a way to add stuff to a System.Collections.Generic.List<T>. This class provides a handy Add method for this purpose:
tasks.Add "a"
tasks.Add "b"
Enclosing the argument in parentheses (as in your attempt) is not necessary, but allowed:
tasks.Add("a")
tasks.Add("b")
C. yes, there is a way to construct a queue from an array. The Queue class has a constructor that takes an IEnumerable<T>, and arrays implement IEnumerable<T>, so you can call that constructor on an array:
let jobs = System.Collections.Generic.Queue<string>( items )
Please note that you hardly needed my help to get any of the above information. Everything is freely available on MSDN (see links above) or from autocompletion/intellisense in your favorite code editor.