1. paradoxiology
  2. HReddit

Commits

paradoxiology  committed 0c4137d

Refactoring and code simplification

  • Participants
  • Parent commits f50b173
  • Branches default

Comments (0)

Files changed (1)

File src/Navigation.hs

View file
 execCommand (GotoArticle index) = do
                                       readingState <- gets curRState
                                       case readingState of
-                                          ReadingListing articleList _ -> do 
-                                                                              a <- lookupArticle index
+                                          ReadingListing articleList _ -> lookupArticle index (\ a -> do
                                                                               comments <- downloadComments (articleLink a)
                                                                               liftIO $ displayDetailArticle a
                                                                               let (ccs, ncs) = splitAt 4 (serialiseComments 0 comments)                                                                       
                                                                               liftIO $ displayComments ccs
-                                                                              modify (appendStateToHistory $ ReadingComments ccs ncs a)
+                                                                              modify (appendStateToHistory $ ReadingComments ccs ncs a) )
                                                                            
                                           otherwise -> throwError "Can't go to any article from here"
 
-execCommand (OpenArticleURL index)  = do
-                                          a <- lookupArticle index
-                                          startBrowser (articleURL a)
+execCommand (OpenArticleURL index)  = lookupArticle index (startBrowser . articleURL)
                                        
-execCommand (OpenArticleDetails index)  = do
-                                              a <- lookupArticle index
-                                              liftIO $ displayDetailArticle a
+execCommand (OpenArticleDetails index)  = lookupArticle index (liftIO . displayDetailArticle)
 
 
 execCommand (OpenCommentURL globPat) = do
 
 
 execCommand (UpvoteArticle index) = do
-                                        article <- lookupArticle index
                                         user    <- gets userInfo
-                                        voteArticle user (Upvote article)
+                                        lookupArticle index (voteArticle user . Upvote)
 
 execCommand (DownvoteArticle index) = do
-                                        article <- lookupArticle index
                                         user    <- gets userInfo
-                                        voteArticle user (Downvote article)
+                                        lookupArticle index (voteArticle user . Downvote)
 
 execCommand (UnvoteArticle index) = do
-                                        article <- lookupArticle index
                                         user    <- gets userInfo
-                                        voteArticle user (Neutral article)
+                                        lookupArticle index (voteArticle user . Neutral)
+
 
 execCommand NextPage = do
                            readingState <- gets curRState
 startBrowser url = liftIO $ system ("start " ++ url) >> return ()
 
 
-lookupArticle :: ArticleIndex -> R Article
-lookupArticle index = do
-                          readingState <- gets curRState
-                          case readingState of
-                              ReadingComments _ _ a        -> return a
-                              ReadingListing articleList _ -> lookupM index "Invalid article number" (listing articleList) 
-                              otherwise                    -> throwError "Error"
+lookupArticle :: ArticleIndex -> (Article -> R a) -> R a
+lookupArticle index f = do
+                            readingState <- gets curRState
+                            case readingState of
+                                ReadingComments _ _ a        -> f a
+                                ReadingListing articleList _ -> lookupM index "Invalid article number" (listing articleList) >>= f
+                                otherwise                    -> throwError "Error"
 
 
 lookupM :: (Monad m, Eq k) => k -> String -> [(k, e)] -> m e