Skip to content

Commit 748bc4b

Browse files
committed
Update examples
1 parent 2e53044 commit 748bc4b

File tree

12 files changed

+56
-92
lines changed

12 files changed

+56
-92
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
/bower_components/
66
/node_modules/
77
/output/
8+
package-lock.json

examples/Applicative.purs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ module Example.Applicative where
22

33
import Prelude
44

5-
import Control.Monad.Eff (Eff)
6-
import Control.Monad.Eff.Console (CONSOLE, logShow)
75
import Control.Monad.Except (runExcept)
8-
6+
import Effect (Effect)
7+
import Effect.Console (logShow)
8+
import Example.Util.Value (foreignValue)
99
import Foreign (F, Foreign, readNumber)
1010
import Foreign.Index ((!))
1111

12-
import Example.Util.Value (foreignValue)
13-
1412
data Point = Point Number Number Number
1513

1614
instance showPoint :: Show Point where
@@ -23,7 +21,7 @@ readPoint value = do
2321
<*> (value ! "y" >>= readNumber)
2422
<*> (value ! "z" >>= readNumber)
2523

26-
main :: Eff (console :: CONSOLE) Unit
24+
main :: Effect Unit
2725
main =
2826
logShow $ runExcept $
2927
readPoint =<< foreignValue """{ "x": 1, "y": 2, "z": 3 }"""

examples/Arrays.purs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@ module Example.Arrays where
22

33
import Prelude
44

5-
import Control.Monad.Eff (Eff)
6-
import Control.Monad.Eff.Console (CONSOLE, logShow)
75
import Control.Monad.Except (runExcept)
8-
9-
import Foreign (readArray, readNumber, readString)
106
import Data.Traversable (traverse)
11-
7+
import Effect (Effect)
8+
import Effect.Console (logShow)
129
import Example.Util.Value (foreignValue)
10+
import Foreign (readArray, readNumber, readString)
1311

14-
main :: Eff (console :: CONSOLE) Unit
12+
main :: Effect Unit
1513
main = do
1614
logShow $ runExcept $
1715
traverse readString =<< readArray =<< foreignValue """["hello", "world"]"""

examples/Complex.purs

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,44 @@ module Example.Complex where
22

33
import Prelude
44

5-
import Control.Monad.Eff (Eff)
6-
import Control.Monad.Eff.Console (CONSOLE, logShow)
75
import Control.Monad.Except (runExcept)
8-
9-
import Foreign (F, Foreign, readArray, readBoolean, readNumber, readString, readNullOrUndefined)
10-
import Foreign.Index ((!))
11-
import Data.Traversable (traverse)
126
import Data.Maybe (Maybe)
13-
7+
import Data.Traversable (traverse)
8+
import Effect (Effect)
9+
import Effect.Console (logShow)
1410
import Example.Util.Value (foreignValue)
11+
import Foreign (F, Foreign, readArray, readBoolean, readNumber, readString, readNullOrUndefined)
12+
import Foreign.Index ((!))
1513

16-
newtype SomeObject =
17-
SomeObject
18-
{ foo :: String
19-
, bar :: Boolean
20-
, baz :: Number
21-
, list :: Array ListItem
22-
}
23-
24-
instance showSomeObject :: Show SomeObject where
25-
show (SomeObject o) =
26-
"(SomeObject { foo: " <> show o.foo <>
27-
", bar: " <> show o.bar <>
28-
", baz: " <> show o.baz <>
29-
", list: " <> show o.list <>
30-
"})"
14+
type SomeObject =
15+
{ foo :: String
16+
, bar :: Boolean
17+
, baz :: Number
18+
, list :: Array ListItem
19+
}
3120

3221
readSomeObject :: Foreign -> F SomeObject
3322
readSomeObject value = do
3423
foo <- value ! "foo" >>= readString
3524
bar <- value ! "bar" >>= readBoolean
3625
baz <- value ! "baz" >>= readNumber
3726
list <- value ! "list" >>= readArray >>= traverse readListItem
38-
pure $ SomeObject { foo, bar, baz, list }
39-
40-
newtype ListItem =
41-
ListItem
42-
{ x :: Number
43-
, y :: Number
44-
, z :: Maybe Number
45-
}
27+
pure { foo, bar, baz, list }
4628

47-
instance showListItem :: Show ListItem where
48-
show (ListItem o) =
49-
"(ListItem { x: " <> show o.x <>
50-
", y: " <> show o.y <>
51-
", z: " <> show o.z <>
52-
" })"
29+
type ListItem =
30+
{ x :: Number
31+
, y :: Number
32+
, z :: Maybe Number
33+
}
5334

5435
readListItem :: Foreign -> F ListItem
5536
readListItem value = do
5637
x <- value ! "x" >>= readNumber
5738
y <- value ! "y" >>= readNumber
5839
z <- value ! "z" >>= readNullOrUndefined >>= traverse readNumber
59-
pure $ ListItem { x, y, z }
40+
pure { x, y, z }
6041

61-
main :: Eff (console :: CONSOLE) Unit
42+
main :: Effect Unit
6243
main = do
6344
let json = """{"foo":"hello","bar":true,"baz":1,"list":[{"x":1,"y":2},{"x":3,"y":4,"z":999}]}"""
6445
logShow $ runExcept $ readSomeObject =<< foreignValue json

examples/MaybeNullable.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ module Example.MaybeNullable where
22

33
import Prelude
44

5-
import Control.Monad.Eff (Eff)
6-
import Control.Monad.Eff.Console (CONSOLE, logShow)
5+
import Effect (Effect)
6+
import Effect.Console (logShow)
77
import Control.Monad.Except (runExcept)
88

99
import Foreign (readBoolean, readNull)
@@ -13,7 +13,7 @@ import Example.Util.Value (foreignValue)
1313

1414
-- Parsing values that are allowed to null or undefined is possible by
1515
-- using Maybe types.
16-
main :: Eff (console :: CONSOLE) Unit
16+
main :: Effect Unit
1717
main = do
1818
logShow $ runExcept $
1919
traverse readBoolean =<< readNull =<< foreignValue "null"

examples/Nested.purs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ module Example.Nested where
22

33
import Prelude
44

5-
import Control.Monad.Eff (Eff)
6-
import Control.Monad.Eff.Console (CONSOLE, logShow)
75
import Control.Monad.Except (runExcept)
8-
6+
import Effect (Effect)
7+
import Effect.Console (logShow)
8+
import Example.Util.Value (foreignValue)
99
import Foreign (F, Foreign, readNumber, readString)
1010
import Foreign.Index ((!))
1111

12-
import Example.Util.Value (foreignValue)
13-
1412
data Foo = Foo Bar Baz
1513

1614
data Bar = Bar String
@@ -32,7 +30,7 @@ readFoo value = do
3230
n <- value ! "foo" ! "baz" >>= readNumber
3331
pure $ Foo (Bar s) (Baz n)
3432

35-
main :: Eff (console :: CONSOLE) Unit
33+
main :: Effect Unit
3634
main =
3735
logShow $ runExcept $
3836
readFoo =<< foreignValue """{ "foo": { "bar": "bar", "baz": 1 } }"""

examples/Objects.purs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,22 @@ module Example.Objects where
22

33
import Prelude
44

5-
import Control.Monad.Eff (Eff)
6-
import Control.Monad.Eff.Console (CONSOLE, logShow)
75
import Control.Monad.Except (runExcept)
8-
6+
import Effect (Effect)
7+
import Effect.Console (logShow)
8+
import Example.Util.Value (foreignValue)
99
import Foreign (F, Foreign, readNumber)
1010
import Foreign.Index ((!))
1111

12-
import Example.Util.Value (foreignValue)
13-
14-
newtype Point = Point { x :: Number, y :: Number }
15-
16-
instance showPoint :: Show Point where
17-
show (Point { x, y }) =
18-
"(Point { x: " <> show x <> ", y: " <> show y <> " })"
12+
type Point = { x :: Number, y :: Number }
1913

2014
readPoint :: Foreign -> F Point
2115
readPoint value = do
2216
x <- value ! "x" >>= readNumber
2317
y <- value ! "y" >>= readNumber
24-
pure $ Point { x, y }
18+
pure { x, y }
2519

26-
main :: Eff (console :: CONSOLE) Unit
20+
main :: Effect Unit
2721
main = do
2822
logShow $ runExcept $
2923
readPoint =<< foreignValue """{ "x": 1, "y": 2 }"""

examples/ParseErrors.purs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ module Example.ParseErrors where
22

33
import Prelude
44

5-
import Control.Monad.Eff (Eff)
6-
import Control.Monad.Eff.Console (CONSOLE, logShow)
75
import Control.Monad.Except (runExcept)
8-
9-
import Foreign (F, Foreign, readArray, readBoolean, readNumber, readString)
10-
import Foreign.Index ((!))
116
import Data.Traversable (traverse)
12-
7+
import Effect (Effect)
8+
import Effect.Console (logShow)
139
import Example.Util.Value (foreignValue)
10+
import Foreign (F, Foreign, readArray, readBoolean, readNumber, readString)
11+
import Foreign.Index ((!))
1412

1513
newtype Point = Point { x :: Number, y :: Number }
1614

@@ -23,7 +21,7 @@ readPoint value = do
2321
y <- value ! "y" >>= readNumber
2422
pure $ Point { x: x, y: y }
2523

26-
main :: Eff (console :: CONSOLE) Unit
24+
main :: Effect Unit
2725
main = do
2826

2927
-- When trying to parse invalid JSON we catch an exception from

examples/SimpleTypes.purs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@ module Example.SimpleTypes where
22

33
import Prelude
44

5-
import Control.Monad.Eff (Eff)
6-
import Control.Monad.Eff.Console (CONSOLE, logShow)
75
import Control.Monad.Except (runExcept)
8-
9-
import Foreign (readString, readNumber, readBoolean)
10-
6+
import Effect (Effect)
7+
import Effect.Console (logShow)
118
import Example.Util.Value (foreignValue)
9+
import Foreign (readString, readNumber, readBoolean)
1210

1311
-- Parsing of the simple JSON String, Number and Boolean types is provided
1412
-- out of the box.
15-
main :: Eff (console :: CONSOLE) Unit
13+
main :: Effect Unit
1614
main = do
1715
logShow $ runExcept $
1816
readString =<< foreignValue "\"a JSON string\""

examples/Union.purs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ module Example.Union where
22

33
import Prelude
44

5-
import Control.Monad.Eff (Eff)
6-
import Control.Monad.Eff.Console (CONSOLE, logShow)
75
import Control.Monad.Except (runExcept)
8-
6+
import Effect (Effect)
7+
import Effect.Console (logShow)
8+
import Example.Util.Value (foreignValue)
99
import Foreign (F, Foreign, readBoolean, readString)
1010
import Foreign.Index ((!))
1111

12-
import Example.Util.Value (foreignValue)
13-
1412
data StringList = Nil | Cons String StringList
1513

1614
instance showStringList :: Show StringList where
@@ -28,7 +26,7 @@ readStringList value =
2826
<$> (value ! "head" >>= readString)
2927
<*> (value ! "tail" >>= readStringList)
3028

31-
main :: Eff (console :: CONSOLE) Unit
29+
main :: Effect Unit
3230
main = do
3331

3432
logShow $ runExcept $

0 commit comments

Comments
 (0)