Skip to content

Instantly share code, notes, and snippets.

@purpleP
Created March 9, 2017 22:33
Show Gist options
  • Select an option

  • Save purpleP/ce84b570b654b00f383dca364f0320cd to your computer and use it in GitHub Desktop.

Select an option

Save purpleP/ce84b570b654b00f383dca364f0320cd to your computer and use it in GitHub Desktop.

Revisions

  1. purpleP created this gist Mar 9, 2017.
    42 changes: 42 additions & 0 deletions changes.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    {-# LANGUAGE DeriveGeneric, DeriveAnyClass, StandaloneDeriving #-}

    import Data.Map (empty, fromList, insert, delete, Map)
    import GHC.Generics
    import Data.Aeson
    import qualified Data.Text.Lazy.IO as T
    import qualified Data.Text.Lazy.Encoding as T

    data Task = Task
    { id :: String
    , description :: String
    , dependsOn :: [String]
    , dependentTasks :: [String]
    } deriving (Eq, Show, Generic, ToJSON, FromJSON)

    type Storage = Map String Task
    type Change = Storage -> Storage

    s :: Storage
    s = empty

    addTask :: Task -> Change
    addTask (Task id desc dep dept) = insert id (Task id desc dep dept)

    instance ToJSON Change where
    parseJSON (Array v) = do
    name <- parseJSON $ v V.! 0
    task <- parseJSON $ v V.! 1
    return (addTask task)

    removeTask :: String -> Change
    removeTask tid = delete tid

    changes = [addTask (Task "1" "Description" [] []), removeTask "1"]

    -- main = putStrLn . show $ foldl (\s c -> c s) s changes

    d = "[addTask, {\"id\": \"1\", \"description\": \"d\", \"dependsOn\": [], \"dependentTasks\": []}]"

    main = putStrLn . show $ decode d $ s

    -- main = T.putStrLn . T.decodeUtf8 . encode $ changes