misc/haskell/Freqs.hs

22 lines
446 B
Haskell
Raw Permalink Normal View History

2018-08-05 18:53:07 +02:00
#!/usr/bin/env nix-script
#!>haskell
{- Calculate words frequency. Use in a pipe. -}
import Data.List
import Data.Ord (comparing)
import Data.Map (toList, fromListWith)
freqs = sort' . toList . fromListWith (+) . map t
where
t x = (x, 1)
sort' = reverse . sortBy (comparing snd)
pretty = intercalate "\n" . map line
where line (x, y) = x ++ ": " ++ show y
best = filter ((>50) . snd)
main = interact (pretty . freqs . words)