\$\begingroup\$
\$\endgroup\$
1
I have made some simple functions to find the first SO chat message. How can I improve my code?
Even if there is a stackoverflow link that will get the job done (I'd be glad to know), I'd like to improve upon my already written code.
import Network.HTTP
import Text.Regex.Posix
type UserID = String
type MessageLink = String
type PageNumber = Integer
findLast :: UserID -> IO MessageLink
findLast = searchLast 1 "Not found"
searchLast :: PageNumber -> MessageLink -> UserID -> IO MessageLink
searchLast pg lnk id = do
let link = "http://chat.stackoverflow.com/users/" ++
id ++ "?tab=recent&page=" ++ show pg
c <- simpleHTTP (getRequest link) >>= getResponseBody
let match = c =~ "/transcript/[^\"]+" :: [[String]]
if null (c =~ "monologue" :: String)
then return $ "http://chat.stackoverflow.com" ++ lnk
else searchLast (pg + 1) (head . last $ match) id
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Aug 18, 2015 at 22:29
-
\$\begingroup\$ Follow-up question \$\endgroup\$200_success– 200_success2015年09月09日 18:14:49 +00:00Commented Sep 9, 2015 at 18:14
1 Answer 1
\$\begingroup\$
\$\endgroup\$
- Avoid partial functions like
head
andlast
. http://chat.stackoverflow.com
is repeated twice insearchLast
. Maybe throw it into its own let-binding to make the function more DRY.
lang-hs