forked from purescript/purescript-foreign
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Union.purs
executable file
·58 lines (50 loc) · 1.2 KB
/
Union.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
module Example.Union where
import Prelude
import Control.Monad.Except (runExcept)
import Effect (Effect)
import Effect.Console (logShow)
import Example.Util.Value (foreignValue)
import Foreign (F, Foreign, readBoolean, readString)
import Foreign.Index ((!))
data StringList = Nil | Cons String StringList
instance showStringList :: Show StringList where
show Nil = "Nil"
show (Cons s l) = "(Cons " <> show s <> " " <> show l <> ")"
readStringList :: Foreign -> F StringList
readStringList value =
value ! "nil" >>=
readBoolean >>=
if _
then pure Nil
else
Cons
<$> (value ! "head" >>= readString)
<*> (value ! "tail" >>= readStringList)
main :: Effect Unit
main = do
logShow $ runExcept $
readStringList =<< foreignValue
"""
{ "nil": false
, "head": "Hello"
, "tail":
{ "nil": false
, "head": "World"
, "tail":
{ "nil": true }
}
}
"""
logShow $ runExcept $
readStringList =<< foreignValue
"""
{ "nil": false
, "head": "Hello"
, "tail":
{ "nil": false
, "head": 0
, "tail":
{ "nil": true }
}
}
"""