aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordan <[email protected]>2021-04-11 10:23:39 +0200
committerdan <[email protected]>2021-04-11 10:23:39 +0200
commit1eca5369046f242d745f64a836c2f5720e2a7b71 (patch)
tree009a6742c3c6ae63b8e6c3e1f44d67eb15222d2a
parent772e403fa6755a8d94d3d6770fd1cc2375dc3476 (diff)
downloadbizexp-1eca5369046f242d745f64a836c2f5720e2a7b71.tar.gz
bizexp-1eca5369046f242d745f64a836c2f5720e2a7b71.tar.bz2
bizexp-1eca5369046f242d745f64a836c2f5720e2a7b71.zip
refactor eSum
-rw-r--r--src/MyLib.hs16
1 files changed, 5 insertions, 11 deletions
diff --git a/src/MyLib.hs b/src/MyLib.hs
index a5146b5..cc84930 100644
--- a/src/MyLib.hs
+++ b/src/MyLib.hs
@@ -60,12 +60,7 @@ parseLevel = go "" (Just [])
go (prev ++ [x]) l xs
evalAst :: Ast -> Maybe Value
-evalAst (Expr name ast) =
- case evalLevel ast of
- Nothing -> Nothing
- Just vals -> case getFunc name of
- Nothing -> Nothing
- Just f -> f vals
+evalAst (Expr name ast) = fromMaybe Nothing (getFunc name <*> evalLevel ast)
evalAst (Val val) = Just val
evalLevel :: [Ast] -> Maybe [Value]
@@ -81,11 +76,10 @@ funcs0 = [("sum",(eSum, 0))]
eSum :: [Value] -> Maybe Value
eSum [] = Just $ IntVal 0
-eSum (x:xs) = case coerceToInt x of
- Nothing -> Nothing
- Just a0 ->
+eSum (x:xs) =
let
- Just (IntVal a1) = eSum xs
+ a1 = coerceToInt =<< eSum xs
+ a0 = coerceToInt x
in
- Just $ IntVal (a0 + a1)
+ IntVal <$> ((+) <$> a1 <*> a0)