[Haskell-cafe] How to simplify this code?

Thomas Davie tom.davie at gmail.com
Fri Jan 16 03:12:22 EST 2009


> add2 :: JSON a => MyData -> String -> a -> MyData
> add2 m k v = fromJust $ (\js -> m { json = js }) `liftM` (showJSON
> `liftM` (toJSObject `liftM` (((k, showJSON v):) `liftM` (fromJSObject
> `liftM` (jsObj $ json m)))))


setJSON m js = m {json = js}

add2 m k v = fromJust $ setJSON m <$> showJSON <$> toJSObjct <$> ((k,  
showJSON v):) <$> fromJSObject <$> (jsObj . json $ m)

now let's push all the fmaps together:

add2 m k v = fromJust . fmap (setJSON m . showJSON . toJSObject . ((k,  
showJSON v):) . fromJSObject) . jsObj . json $ m

much better :)

Bob


More information about the Haskell-Cafe mailing list