Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save blairdrummond/a95d66047247c650bde7b7cf1300d8b7 to your computer and use it in GitHub Desktop.
Save blairdrummond/a95d66047247c650bde7b7cf1300d8b7 to your computer and use it in GitHub Desktop.
Newer clumps.hs
clumps :: (Num a, Ord a) => GHC.Int.Int64 -> GHC.Int.Int64 -> a -> T.Text -> [T.Text]
clumps k l t text = M.keys $ M.filter (>=t) $ (\(m,_,_,_) -> m) $ T.foldl'
(\x y -> slideDrop $ slideInsert x y)
(M.singleton prefix 1, prefix, prefix, k)
text'
where
(prefix, text') = T.splitAt k text
slideInsert (m, word, window, delay) c = let wtail = (snd . fromJust . T.uncons) word
word' = T.snoc wtail c
in (M.alter increment word' m, word', T.snoc window c, min l (delay + 1))
increment Nothing = Just 1
increment (Just n) = if n > t
then Just n
else Just (n+1)
slideDrop (m, word, window, delay)
| delay < l = (m, word, window, delay)
| otherwise = let falloff = T.take k window
window' = snd $ fromJust $ T.uncons window
in (M.alter decrement falloff m, word, window', delay)
decrement Nothing = Nothing
decrement (Just 1) = Nothing
decrement (Just n) = if n <= t
then Just (n-1)
else Just n
main :: IO ()
main = do
text <- TI.getContents
let result = clumps 9 500 3 text
mapM_ TI.putStrLn result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment