Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 106079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:32:25+00:00 2026-05-11T01:32:25+00:00

You always hear that functional code is inherently easier to parallelize than non-functional code,

  • 0

You always hear that functional code is inherently easier to parallelize than non-functional code, so I decided to write a function which does the following:

Given a input of strings, total up the number of unique characters for each string. So, given the input [ 'aaaaa'; 'bbb'; 'ccccccc'; 'abbbc' ], our method will returns a: 6; b: 6; c: 8.

Here’s what I’ve written:

(* seq<#seq<char>> -> Map<char,int> *) let wordFrequency input =     input     |> Seq.fold (fun acc text ->         (* This inner loop can be processed on its own thread *)         text         |> Seq.choose (fun char -> if Char.IsLetter char then Some(char) else None)         |> Seq.fold (fun (acc : Map<_,_>) item ->             match acc.TryFind(item) with             | Some(count) -> acc.Add(item, count + 1)             | None -> acc.Add(item, 1))             acc         ) Map.empty 

This code is ideally parallelizable, because each string in input can be processed on its own thread. Its not as straightforward as it looks since the innerloop adds items to a Map shared between all of the inputs.

I’d like the inner loop factored out into its own thread, and I don’t want to use any mutable state. How would I re-write this function using an Async workflow?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:32:25+00:00Added an answer on May 11, 2026 at 1:32 am

    As already pointed out, there’s update contention if you try to have different threads process different input strings, since each thread can increment the count of every letter. You can have each thread produce its own Map, and then ‘add up all the Maps’, but that final step may be expensive (and is not as well-suited to utilizing threads due to the shared data). I think large inputs are likely to run faster using an algorithm like the one below, where each thread processes a different letter-to-count (for all strings in the input). As a result, each thread has its own independent counter, so no update contention and no final step to combine the results. However we need preprocessing to discover the ‘set of unique letters’, and this step does have the same contention problem. (In practice, you probably know the universe of characters up front, e.g. alphabetics, and then can just creates 26 threads to process a-z, and bypass this issue.) In any case, presumably the question is mostly about exploring ‘how to write F# async code to divide work across threads’, so the code below demonstrates it.

    #light  let input = [| 'aaaaa'; 'bbb'; 'ccccccc'; 'abbbc' |]  // first discover all unique letters used let Letters str =      str |> Seq.fold (fun set c -> Set.add c set) Set.empty  let allLetters =      input |> Array.map (fun str ->          async { return Letters str })     |> Async.Parallel      |> Async.Run          |> Set.union_all // note, this step is single-threaded,          // if input has many strings, can improve this  // Now count each letter on a separate thread let CountLetter letter =     let mutable count = 0     for str in input do         for c in str do             if letter = c then                 count <- count + 1     letter, count let result =      allLetters |> Seq.map (fun c ->         async { return CountLetter c })     |> Async.Parallel      |> Async.Run  // print results for letter,count in result do     printfn '%c : %d' letter count 

    I have indeed ‘completely changed the algorithm’, mostly because I the original algorithm you had is not particularly suitable to direct data parallelization due to the update contention. Depending on exactly what you’re out to learn, this answer may or may not be particularly satisfactory to you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 70k
  • Answers 70k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Virtual functions can be inlined sometimes. An excerpt from the… May 11, 2026 at 12:59 pm
  • added an answer The simplest way is to do the correct thing: Use… May 11, 2026 at 12:59 pm
  • added an answer The original implementation of Markdown (by Gruber) and PHP Markdown… May 11, 2026 at 12:59 pm

Related Questions

Do you always follow the convention of putting branches, tags and trunk directories at
Do you always use a second-level cache in Hibernate or do you first try
Do you always lean towards thinking of db schema when starting or planning a
Or should you always create some other lock object?
What do you consider best practice when it comes to error handling errors in
I'm interested in hearing what technique(s) you're using to validate the internal state of
I work in an office which has been doing Agile for a while now.
I'm kind of curious how I should approach a problem with iPhones and communication

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.