Created
December 2, 2016 15:41
-
-
Save nsmaciej/3b88fb86da562bcad8d4e0ebcb570b8e to your computer and use it in GitHub Desktop.
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
| import Control.Monad.Trans.State | |
| import Data.List (intercalate) | |
| import Control.Monad.IO.Class (liftIO) | |
| type TodoState a = StateT [Todo] IO a | |
| type Category = String | |
| data Todo = Todo { text :: String | |
| , categories :: [Category] | |
| } deriving (Show) | |
| addItem :: String -> [Category] -> TodoState () | |
| addItem txt cs = modify (Todo txt cs:) | |
| updateItem :: String -> String -> TodoState () | |
| updateItem old new = modify (map (\x -> if text x == old then x {text = new} else x)) | |
| viewList :: [Category] -> TodoState () | |
| viewList cs = do | |
| list <- get | |
| liftIO $ do | |
| putStrLn $ "# " ++ intercalate " & " cs | |
| mapM_ (putStrLn . ("- " ++) . text) $ filterCategory cs list | |
| putStr "\n" | |
| filterCategory :: [Category] -> [Todo] -> [Todo] | |
| filterCategory cs = filter (any (`elem` cs) . categories) | |
| runTodo :: TodoState () -> IO () | |
| runTodo = flip evalStateT [] | |
| main :: IO () | |
| main = runTodo $ do | |
| addItem "Go to work" ["Programming"] | |
| addItem "Create Sine Waves in C" ["Music", "Programming"] | |
| addItem "Play my synth" ["Music"] | |
| viewList ["Music"] | |
| updateItem "Create Sine Waves in C" "Create Sine Waves in Python" | |
| viewList ["Programming"] | |
| viewList ["Programming", "Music"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment