Skip to content

Instantly share code, notes, and snippets.

@j1r1k
Last active May 16, 2016 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j1r1k/e7ae3d77e08c787fe366ca110b5016a9 to your computer and use it in GitHub Desktop.
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
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