-
-
Save dustinlacewell/d4d3fdf5269c3355df509a37a92a62db to your computer and use it in GitHub Desktop.
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
{- Capitalize sentences in a string -} | |
-- represents work to be processed | |
data Work s d = Work { | |
input :: Maybe d, -- Nothing indicates work completed | |
output :: d, | |
state :: s -- use-case specific work state | |
} deriving (Show) | |
-- processes work over f until input is Nothing | |
doWork _ (Work Nothing o _) = o | |
doWork f w@(Work (Just i) o s)= | |
f w & doWork f | |
type StringWork = Work Bool String | |
capSentences' :: StringWork -> StringWork | |
capSentences' w@(Work Nothing _ _) = w | |
capSentences' w@(Work (Just "") o s) = Work Nothing (reverse o) s | |
capSentences' (Work (Just ('.':i')) o s) = | |
Work (Just i') ('.':o) True | |
capSentences' (Work (Just (c:i')) o False) = | |
Work (Just i') (c:o) False | |
capSentences' (Work (Just (c:i')) o True) = | |
if isAlpha c then | |
Work (Just i') (toUpper c : o) False | |
else | |
Work (Just i') (c:o) True | |
capSentences s = doWork capSentences' (Work (Just s) "" True) | |
main = do | |
print $ capSentences "blah. woot ha." |
mstksg
commented
Sep 9, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment