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.64 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.63 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.62 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.61 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.60 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.59 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.58 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.57 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.56 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.55 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.54 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.53 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.52 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.51 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.50 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.49 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.48 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.47 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.46 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs
trackler-2.2.1.45 tracks/haskell/exercises/parallel-letter-frequency/examples/success-standard/src/Frequency.hs