How to serialize function type to json in haskell?

后端 未结 2 369
情歌与酒
情歌与酒 2021-01-28 06:06
data Task = Task
    { id :: String
    , description :: String
    , dependsOn :: [String]
    , dependentTasks :: [String]
    } deriving (Eq, Show, Generic, ToJSON, F         


        
2条回答
  •  北海茫月
    2021-01-28 06:44

    Make a data type for your functions and an evaluation function:

    data TaskFunction = AddTask Task | RemoveTask String 
      deriving (Eq, Show, Generic, ToJSON, FromJSON)
    
    eval :: TaskFunction -> Storage -> Storage
    eval (AddTask t) = addTask t
    eval (RemoveTask t) = removeTask t
    
    changes = [AddTask (Task "1" "Description" [] []), RemoveTask "1"]
    
    main = putStrLn . show $ foldl (\s c -> c s) s (eval <$> changes)
    

提交回复
热议问题