3 ms·
Well it's 51 minutes later, and I got to the third bullet point :D EDIT: I tried to focus on readability here. Things could be quite a bit more concise but tha
by codygman 9y ago
Well it's 51 minutes later, and I got to the third bullet point :D
EDIT: I tried to focus on readability here. Things could be quite a bit more concise but that requires firmer grasp of interactions between >>=, <$>, and point-free functions.
Here's what I have so far:
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Prelude hiding (appendFile)
import Data.Monoid ((<>))
import Network.Wreq
import Control.Lens
import Control.Concurrent.Async
import Data.ByteString.Lazy (ByteString, appendFile)
main = do
urls <- promptUserURLs
responses <- mapConcurrently get urls
let responseBodys = fmap (view responseBody) responses
responseInfos = fmap (\(url,body) -> (take 5 url, body)) (zip urls responseBodys)
httpUrls = filter (\(url,_) -> url == "http:") responseInfos
httpsUrls = filter (\(url,_) -> url == "https") responseInfos
mapM_ (appendFile "output1.txt") httpUrls
exampleSiteResponses <- mapConcurrently get httpUrls
let exampleSiteResponses' = fmap (view responseBody) exampleSiteResponses
mapM_ (appendFile "output2.txt") exampleSiteResponses
promptUserURLs :: IO [String]
promptUserURLs = go []
where go :: [String] -> IO [String]
go urls = do
line <- getLine
if line == ("q" :: String)
then return urls
else go (urls <> [line]) -- lol, this is a very bad way to snoc
- i336_ 9y agoAnd surprisingly, I can actually follow it. (I have that thing where people say "yeah I can read this but don't expect me to write it" right now though...) This is very interesting. Does Haskell not have reasonably easy-to-use EOF indication? I'm reasonably impressed this is as short as it is; being able to shove everything into maps (and get concurrency for free) is nice. I can understand why Haskell is less widely used though; this is... OH, "take 5" is substring, that's why you did "http:" and "https"! ...I was going to say "hard". :P Thanks for taking the time to write this!!
- codygman 9y agoEh, I had a mistake. {-# LANGUAGE OverloadedStrings #-} module Main where import Prelude hiding (appendFile) import Data.Monoid ((<>)) import Network.Wreq import Control.Lens import Control.Concurrent.Async import Data.ByteString.Lazy (ByteString, appendFile) main = do urls <- promptUserURLs responses <- mapConcurrently get urls let responseBodys = fmap (view responseBody) responses responseInfos = fmap (\(url,body) -> (take 5 url, body)) (zip urls responseBodys) httpUrls = fmap fst (filter (\(url,_) -> url == "http:") responseInfos) httpsResponses = fmap snd (filter (\(url,_) -> url == "https") responseInfos) mapM_ (appendFile "output1.txt") httpsResponses exampleSiteResponses <- mapConcurrently get httpUrls let exampleSiteResponses' = fmap (view responseBody) (exampleSiteResponses) mapM_ (appendFile "output2.txt") exampleSiteResponses' promptUserURLs :: IO [String] promptUserURLs = go [] where go :: [String] -> IO [String] go urls = do line <- getLine if line == ("q" :: String) then return urls else go (urls <> [line]) -- lol, this is a very bad way to snoc
- i336_ 9y agoHuh. So Haskell is the language of the missing apostrophe instead of the missing semicolon. (In all fairness, so is Lisp, but it puts the apostrophes on the other side.) I'm curious about "(zip urls response" though. Does that extend to the "responseInfos)" on the next line, or am I reading it wrong? (I noticed the "fmap fst" and "fmap snd".) Wonders where to start to figure out how this works
- codygman 9y agoYep, I've missed apostrophe's quite a few times. It's was a common cause of infinite loops for me in the past, but I suppose I've trained myself to spot them more quickly. Perhaps zip can be more simply explained by a simple example: out: zip ["a","b","c"] [0,1,2] out: [("a",0),("b",1),("c",2)] out: fmap fst [(0,1),(2,3),(4,5)] out: [0,2,4] out: fmap snd [(0,1),(2,3),(4,5)] out: [1,3,5]
- i336_ 9y agoOhhhhh. zip is cool! And I wondered whether "fst" was "first" and "snd" was "second"... is there a "trd" and "fth"? Also - is [] a list (or mixed type) and () an array (or same-item type)?
- dllthomas 9y agoHopefully the ticked version is a different type than the unticked version and would be caught by the typechecker. Failing that, hopefully the ticked version is now unused and will be caught by the linter. But yeah, this is definitely a thing that can happen. If you're doing a lot of successive transformations of data of the same type, putting it in State will prevent you from messing up the ordering (or needing to come up with fresh names).