Skip to content

Instantly share code, notes, and snippets.

@dogweather
Last active February 1, 2018 06:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dogweather/1dcdf169b9e9f365202a8c42d9583eb7 to your computer and use it in GitHub Desktop.
Save dogweather/1dcdf169b9e9f365202a8c42d9583eb7 to your computer and use it in GitHub Desktop.
Haskell implementation of five string operations
module StringOps(cleanUp, fixHyphenation, fixWhitespace) where
import Data.List (isSuffixOf)
import Data.String.Utils (replace, split)
cleanUp ∷ String → String
cleanUp = fixWhitespace
>>> fixHyphenation
>>> splitIntoSentences
>>> first
fixWhitespace ∷ String → String
fixWhitespace = replace "\n" " "
fixHyphenation ∷ String → String
fixHyphenation = replace "- " ""
splitIntoSentences ∷ String → [String]
splitIntoSentences = split ". " ⋙ map ensureEndsWithPeriod
ensureEndsWithPeriod :: String -> String
ensureEndsWithPeriod sentence =
sentence ++ (if "." `isSuffixOf` sentence then "" else ".")
module StringOpsSpec where
import StringOps
import Test.Hspec
main :: IO ()
main = hspec spec
spec :: Spec
spec = do
describe "fixHyphenation" $ do
it "rejoins a word" $ do
fixHyphenation "auto- mobile" `shouldBe` "automobile"
describe "fixWhitespace" $ do
it "changes a newline to a space" $ do
fixWhitespace "and\nthe story" `shouldBe` "and the story"
describe "cleanUp" $ do
it "handles extra text" $ do
let input = "Relating to the state transient lodging tax; creating\nnew provisions; amending ORS 284.131 and\n320.305; prescribing an effective date; and pro-\nviding for revenue raising that requires approval\nby a three-fifths majority.\nWhereas Enrolled House Bill 2267 (chapter 818,"
cleanUp input `shouldBe` "Relating to the state transient lodging tax; creating new provisions; amending ORS 284.131 and 320.305; prescribing an effective date; and providing for revenue raising that requires approval by a three-fifths majority."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment