Created
September 3, 2017 19:43
-
-
Save ploeh/8867b31fbe66748932cbd40ed518061d to your computer and use it in GitHub Desktop.
Translation of http://ccd-school.de/2017/06/stratified-design-over-layered-design to Haskell
This file contains hidden or 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
-- Translation to Haskell of: | |
-- http://ccd-school.de/2017/06/stratified-design-over-layered-design | |
-- I've deliberately translated each method to a function, in order to show the | |
-- similarity. Some functions, like extractWords, are redundant (already built | |
-- in) or almost too simple to get their own function. | |
module Main where | |
import Data.List (partition) | |
-- Business: | |
countWords :: Foldable t => t String -> String -> Int | |
countWords stopWords = length . removeStopWords stopWords . extractWords | |
extractWords :: String -> [String] | |
extractWords = words | |
removeStopWords :: (Foldable t, Eq a) => t a -> [a] -> [a] | |
removeStopWords stopWords = snd . partition (`elem` stopWords) | |
-- Presentation | |
askForText :: IO String | |
askForText = do | |
putStrLn "Text: " | |
getLine | |
displayWordCount :: Show a => a -> IO () | |
displayWordCount n = putStrLn $ "Number of words: " ++ show n | |
-- Data | |
loadStopWords :: IO [String] | |
loadStopWords = lines <$> readFile "./stopwords.txt" | |
-- App: | |
countWords' :: String -> IO Int | |
countWords' text = do | |
stopWords <- loadStopWords | |
return $ countWords stopWords text | |
main :: IO () | |
main = do | |
text <- askForText | |
n <- countWords' text | |
displayWordCount n |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment