0

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
asked Apr 3, 2018 at 7:52
4
  • 2
    I think you better ask all these questions here: chat.stackoverflow.com/rooms/51909/f Commented 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 room Commented 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. Commented 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. Commented Apr 3, 2018 at 16:59

1 Answer 1

3

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.

answered Apr 3, 2018 at 15:49
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.