Last active
May 16, 2016 18:44
-
-
Save j1r1k/e7ae3d77e08c787fe366ca110b5016a9 to your computer and use it in GitHub Desktop.
PureScript by Example: Chapter 10 (The Foreign Function Interface) exercise 10.19.4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Tagged where | |
import Prelude | |
import Data.Either | |
import Data.Foreign | |
import Data.Foreign.Class | |
newtype Tagged a b = Tagged (Either a b) | |
tagKey :: String | |
tagKey = "tag" | |
valueKey :: String | |
valueKey = "value" | |
instance taggedIsForeign :: (IsForeign a, IsForeign b) => IsForeign (Tagged a b) where | |
read value = do | |
tag <- readProp tagKey value | |
case tag of | |
"Left" -> (Tagged <<< Left) <$> readProp valueKey value | |
"Right" -> (Tagged <<< Right) <$> readProp valueKey value | |
_ -> Left $ JSONError $ "Unrecoginzed tag " ++ tag | |
instance showTagged :: (Show a, Show b) => Show (Tagged a b) where | |
show (Tagged e) = "Tagged " ++ show e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment