Last active
February 1, 2018 06:10
-
-
Save dogweather/1dcdf169b9e9f365202a8c42d9583eb7 to your computer and use it in GitHub Desktop.
Haskell implementation of five string operations
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
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 ".") |
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
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