Created
December 16, 2013 09:23
-
-
Save codygman/7984368 to your computer and use it in GitHub Desktop.
How to get the length of a webpage in haskell imperatively, using Network.HTTP, and using Network.HTTP.Conduit
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
import qualified Data.ByteString.Char8 as BC | |
import qualified Data.ByteString.Lazy.Char8 as BCL | |
import qualified Network.HTTP.Conduit as C | |
import Network.HTTP | |
pageCountShort :: IO () | |
-- prints word count of a page using Network.HTTP.Conduit | |
pageCountShort = getLine >>= C.simpleHttp >>= print . length . words . BCL.unpack | |
main :: IO () | |
main = do | |
-- pageCountVerbose | |
pageCountShort | |
-- pageCountShortAlt | |
-- other ways of doing this | |
pageCountVerbose :: IO () | |
-- imperative and verbose first try getting number of words on a webpage | |
pageCountVerbose = do | |
putStrLn "Enter a url: " | |
url <- getLine | |
pageText <- C.simpleHttp url | |
let pageTextCount = length . words . BCL.unpack $ pageText | |
print $ "There are " ++ show pageTextCount ++ " words!" | |
pageCountShortAlt :: IO () | |
-- print word count of a page using Network.HTTP | |
pageCountShortAlt = getLine >>= simpleHTTP . getRequest >>= getResponseBody >>= print . length . words |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment