Skip to content
Snippets Groups Projects
Commit 5aa48dbe authored by Iustin Pop's avatar Iustin Pop
Browse files

Generalize some Result function into monad ones

We don't really needed, but is more clean like this.
parent 942403e6
No related branches found
No related tags found
No related merge requests found
...@@ -73,9 +73,9 @@ parseInstance a = ...@@ -73,9 +73,9 @@ parseInstance a =
(show `liftM` disk) |+ (show `liftM` disk) |+
running |+ pnode |+ snode running |+ pnode |+ snode
boolToYN :: Bool -> Result String boolToYN :: (Monad m) => Bool -> m String
boolToYN True = Ok "Y" boolToYN True = return "Y"
boolToYN _ = Ok "N" boolToYN _ = return "N"
parseNode :: JSObject JSValue -> Result String parseNode :: JSObject JSValue -> Result String
parseNode a = parseNode a =
......
...@@ -57,9 +57,9 @@ instance Monad Result where ...@@ -57,9 +57,9 @@ instance Monad Result where
return = Ok return = Ok
fail = Bad fail = Bad
fromJResult :: J.Result a -> Result a fromJResult :: Monad m => J.Result a -> m a
fromJResult (J.Error x) = Bad x fromJResult (J.Error x) = fail x
fromJResult (J.Ok x) = Ok x fromJResult (J.Ok x) = return x
-- | Comma-join a string list. -- | Comma-join a string list.
commaJoin :: [String] -> String commaJoin :: [String] -> String
...@@ -111,42 +111,42 @@ readData nd = ...@@ -111,42 +111,42 @@ readData nd =
exitWith $ ExitFailure 1 exitWith $ ExitFailure 1
Ok x -> return x) Ok x -> return x)
readEitherString :: J.JSValue -> Result String readEitherString :: (Monad m) => J.JSValue -> m String
readEitherString v = readEitherString v =
case v of case v of
J.JSString s -> Ok $ J.fromJSString s J.JSString s -> return $ J.fromJSString s
_ -> Bad "Wrong JSON type" _ -> fail "Wrong JSON type"
loadJSArray :: String -> Result [J.JSObject J.JSValue] loadJSArray :: (Monad m) => String -> m [J.JSObject J.JSValue]
loadJSArray s = fromJResult $ J.decodeStrict s loadJSArray s = fromJResult $ J.decodeStrict s
fromObj :: J.JSON a => String -> J.JSObject J.JSValue -> Result a fromObj :: (J.JSON a, Monad m) => String -> J.JSObject J.JSValue -> m a
fromObj k o = fromObj k o =
case lookup k (J.fromJSObject o) of case lookup k (J.fromJSObject o) of
Nothing -> Bad $ printf "key '%s' not found" k Nothing -> fail $ printf "key '%s' not found" k
Just val -> fromJResult $ J.readJSON val Just val -> fromJResult $ J.readJSON val
getStringElement :: String -> J.JSObject J.JSValue -> Result String getStringElement :: (Monad m) => String -> J.JSObject J.JSValue -> m String
getStringElement = fromObj getStringElement = fromObj
getIntElement :: String -> J.JSObject J.JSValue -> Result Int getIntElement :: (Monad m) => String -> J.JSObject J.JSValue -> m Int
getIntElement = fromObj getIntElement = fromObj
getBoolElement :: String -> J.JSObject J.JSValue -> Result Bool getBoolElement :: (Monad m) => String -> J.JSObject J.JSValue -> m Bool
getBoolElement = fromObj getBoolElement = fromObj
getListElement :: String -> J.JSObject J.JSValue -> Result [J.JSValue] getListElement :: (Monad m) => String -> J.JSObject J.JSValue -> m [J.JSValue]
getListElement = fromObj getListElement = fromObj
getObjectElement :: String -> J.JSObject J.JSValue getObjectElement :: (Monad m) => String -> J.JSObject J.JSValue
-> Result (J.JSObject J.JSValue) -> m (J.JSObject J.JSValue)
getObjectElement = fromObj getObjectElement = fromObj
asJSObject :: J.JSValue -> Result (J.JSObject J.JSValue) asJSObject :: (Monad m) => J.JSValue -> m (J.JSObject J.JSValue)
asJSObject (J.JSObject a) = Ok a asJSObject (J.JSObject a) = return a
asJSObject _ = Bad "not an object" asJSObject _ = fail "not an object"
asObjectList :: [J.JSValue] -> Result [J.JSObject J.JSValue] asObjectList :: (Monad m) => [J.JSValue] -> m [J.JSObject J.JSValue]
asObjectList = sequence . map asJSObject asObjectList = sequence . map asJSObject
-- | Function to concat two strings with a separator under a monad -- | Function to concat two strings with a separator under a monad
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment