Created
July 28, 2018 03:47
-
-
Save blairdrummond/a95d66047247c650bde7b7cf1300d8b7 to your computer and use it in GitHub Desktop.
Newer clumps.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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