Sha256: 25c264e9f8a6d973c39234c8f3710b95d22372a18d671085d60053b0aa7b97d4

Contents?: true

Size: 846 Bytes

Versions: 302

Compression:

Stored size: 846 Bytes

Contents

module Frequency (frequency) where

import           Control.Parallel.Strategies (using, parListChunk, rdeepseq)
import           Data.Char (isLetter, toLower)
import           Data.Map (Map)
import qualified Data.Map as Map
import           Data.Ratio ((%))
import           Data.Text (Text)
import qualified Data.Text as T

-- | Compute the frequency of letters in the text using the given number of
--   parallel workers.
frequency :: Int -> [Text] -> Map Char Int
frequency workers texts = 
    let chunkSize = ceiling (length texts % workers)
        freqs = map countLetters texts `using` parListChunk chunkSize rdeepseq
    in Map.unionsWith (+) freqs

countLetters :: Text -> Map Char Int
countLetters = T.foldl' process Map.empty
  where
    process m c | isLetter c = Map.insertWith (+) (toLower c) 1 m
                | otherwise  = m

Version data entries

302 entries across 302 versions & 1 rubygems

Version Path
trackler-2.2.1.84 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.83 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.82 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.81 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.80 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.79 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.78 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.77 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.76 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.75 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.74 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.73 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.72 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.71 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.70 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.69 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.68 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.67 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.66 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.65 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs