Skip to content

Commit b1790a9

Browse files
authored
Code updates for 0.11.3 (paf31#76)
* Code updates for 0.11.3 * Update chapters 1, 2 and 3 * Chapter 2 fix * Fix paf31#88 * Fix paf31#71 * Fix paf31#79 and paf31#74 * Chapter 4 * Fix mentions of let and old style constrained types * Typo, fix paf31#80 * Chapter 5 * Chapter 6 * Chapter 7 * Chapter 8 * REPL fixes * More kinds * More kinds * Chapter 10 * exception :: EXCEPTION * typo
1 parent faf79fa commit b1790a9

File tree

38 files changed

+338
-365
lines changed

38 files changed

+338
-365
lines changed

exercises/chapter10/bower.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@
1717
"package.json"
1818
],
1919
"dependencies": {
20-
"purescript-console": "^2.0.0",
21-
"purescript-dom": "^3.0.0",
22-
"purescript-foreign": "^3.0.0",
23-
"purescript-react-dom": "^2.0.0",
24-
"purescript-react": "^2.0.0",
25-
"purescript-strings": "^2.0.0",
26-
"purescript-validation": "^2.0.0"
20+
"purescript-console": "^3.0.0",
21+
"purescript-dom": "^4.0.0",
22+
"purescript-foreign": "^4.0.0",
23+
"purescript-react-dom": "^3.0.0",
24+
"purescript-react": "^3.0.0",
25+
"purescript-strings": "^3.0.0",
26+
"purescript-validation": "^3.0.0",
27+
"purescript-foreign-generic": "^4.0.0"
2728
},
2829
"devDependencies": {
29-
"purescript-psci-support": "^2.0.0"
30+
"purescript-psci-support": "^3.0.0"
3031
}
3132
}

exercises/chapter10/src/Control/Monad/Eff/Alert.purs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ module Control.Monad.Eff.Alert where
22

33
import Prelude
44

5-
import Control.Monad.Eff (Eff)
5+
import Control.Monad.Eff (kind Effect, Eff)
66

7-
foreign import data ALERT :: !
7+
foreign import data ALERT :: Effect
88

99
foreign import alert :: forall eff. String -> Eff (alert :: ALERT | eff) Unit

exercises/chapter10/src/Control/Monad/Eff/Storage.purs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ module Control.Monad.Eff.Storage where
22

33
import Prelude
44

5-
import Control.Monad.Eff (Eff)
5+
import Control.Monad.Eff (kind Effect, Eff)
66
import Data.Foreign (Foreign)
77

8-
foreign import data STORAGE :: !
8+
foreign import data STORAGE :: Effect
99

1010
foreign import setItem :: forall eff. String -> String -> Eff (storage :: STORAGE | eff) Unit
1111

exercises/chapter10/src/Data/JSON.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

exercises/chapter10/src/Data/JSON.purs

Lines changed: 0 additions & 5 deletions
This file was deleted.

exercises/chapter10/src/Main.purs

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
module Main where
22

33
import Prelude
4-
import React.DOM as D
5-
import React.DOM.Props as P
4+
65
import Control.Monad.Eff (Eff)
76
import Control.Monad.Eff.Alert (ALERT, alert)
87
import Control.Monad.Eff.Console (CONSOLE, log)
@@ -18,18 +17,19 @@ import Data.AddressBook (Address(..), Person(..), PhoneNumber(..), PhoneType(..)
1817
import Data.AddressBook.Validation (Errors, validatePerson')
1918
import Data.Array ((..), length, modifyAt, zipWith)
2019
import Data.Either (Either(..))
21-
import Data.Foldable (for_)
22-
import Data.Foreign (ForeignError, readString, toForeign)
23-
import Data.Foreign.Class (class IsForeign, readJSON, read, readProp)
24-
import Data.Foreign.Index (prop)
25-
import Data.Foreign.Null (unNull)
26-
import Data.JSON (stringify)
20+
import Data.Foldable (foldMap, for_)
21+
import Data.Foreign (ForeignError, readNullOrUndefined, readString, renderForeignError, toForeign)
22+
import Data.Foreign.Class (class Decode, class Encode)
23+
import Data.Foreign.Generic (decodeJSON, defaultOptions, encodeJSON, genericDecode, genericEncode)
24+
import Data.Foreign.Index (index)
25+
import Data.Generic.Rep (class Generic)
2726
import Data.List.NonEmpty (NonEmptyList)
2827
import Data.Maybe (Maybe(..), fromJust, fromMaybe)
29-
import Data.Nullable (toMaybe)
3028
import Data.Traversable (traverse)
3129
import Partial.Unsafe (unsafePartial)
3230
import React (ReactClass, ReadWrite, ReactState, Event, ReactThis, createFactory, readState, spec, createClass, writeState)
31+
import React.DOM as D
32+
import React.DOM.Props as P
3333
import ReactDOM (render)
3434

3535
newtype AppState = AppState
@@ -65,24 +65,13 @@ newtype FormData = FormData
6565
, cellPhone :: String
6666
}
6767

68-
instance formDataIsForeign :: IsForeign FormData where
69-
read value = do
70-
firstName <- readProp "firstName" value
71-
lastName <- readProp "lastName" value
72-
street <- readProp "street" value
73-
city <- readProp "city" value
74-
state <- readProp "state" value
75-
homePhone <- readProp "homePhone" value
76-
cellPhone <- readProp "cellPhone" value
77-
pure $ FormData
78-
{ firstName
79-
, lastName
80-
, street
81-
, city
82-
, state
83-
, homePhone
84-
, cellPhone
85-
}
68+
derive instance genericFormData :: Generic FormData _
69+
70+
instance decodeFormData :: Decode FormData where
71+
decode = genericDecode (defaultOptions { unwrapSingleConstructors = true })
72+
73+
instance encodeFormData :: Encode FormData where
74+
encode = genericEncode (defaultOptions { unwrapSingleConstructors = true })
8675

8776
toFormData :: Partial => Person -> FormData
8877
toFormData (Person p@{ homeAddress: Address a
@@ -113,12 +102,12 @@ loadSavedData = do
113102
let
114103
savedData :: Either (NonEmptyList ForeignError) (Maybe FormData)
115104
savedData = runExcept do
116-
jsonOrNull <- read item
117-
traverse readJSON (unNull jsonOrNull)
105+
jsonOrNull <- traverse readString =<< readNullOrUndefined item
106+
traverse decodeJSON jsonOrNull
118107

119108
case savedData of
120109
Left err -> do
121-
alert $ "Unable to read saved form data: " <> show err
110+
alert $ "Unable to read saved form data: " <> foldMap (("\n" <> _) <<< renderForeignError) err
122111
pure Nothing
123112
Right mdata -> pure mdata
124113

@@ -136,13 +125,13 @@ validateAndSaveEntry person = do
136125
case validatePerson' person of
137126
Left errs -> alert $ "There are " <> show (length errs) <> " validation errors."
138127
Right result -> do
139-
setItem "person" $ stringify $ toForeign $ unsafePartial toFormData result
128+
setItem "person" $ encodeJSON $ unsafePartial toFormData result
140129
alert "Saved"
141130

142131
valueOf :: Event -> Either (NonEmptyList ForeignError) String
143132
valueOf e = runExcept do
144-
target <- prop "target" (toForeign e)
145-
value <- prop "value" target
133+
target <- index (toForeign e) "target"
134+
value <- index target "value"
146135
readString value
147136

148137
updateAppState
@@ -249,4 +238,4 @@ main = void do
249238
let component = D.div [] [ createFactory (addressBook (initialState formData)) unit ]
250239
doc <- window >>= document
251240
ctr <- getElementById (ElementId "main") (documentToNonElementParentNode (htmlDocumentToDocument doc))
252-
render component (unsafePartial fromJust (toMaybe ctr))
241+
render component (unsafePartial fromJust ctr)

exercises/chapter11/bower.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
"package.json"
1818
],
1919
"dependencies": {
20-
"purescript-maps": "^2.0.0",
21-
"purescript-node-readline": "^2.0.0",
22-
"purescript-sets": "^2.0.0",
23-
"purescript-strings": "^2.0.0",
24-
"purescript-transformers": "^2.0.0",
25-
"purescript-yargs": "^2.0.0"
20+
"purescript-maps": "^3.0.0",
21+
"purescript-node-readline": "^3.0.0",
22+
"purescript-sets": "^3.0.0",
23+
"purescript-strings": "^3.0.0",
24+
"purescript-transformers": "^3.0.0",
25+
"purescript-yargs": "^3.0.0"
2626
},
2727
"devDependencies": {
28-
"purescript-psci-support": "^2.0.0"
28+
"purescript-psci-support": "^3.0.0"
2929
}
3030
}

exercises/chapter11/src/Main.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import Node.Yargs.Setup (usage)
2020
runGame
2121
:: forall eff
2222
. GameEnvironment
23-
-> Eff ( err :: EXCEPTION
23+
-> Eff ( exception :: EXCEPTION
2424
, readline :: RL.READLINE
2525
, console :: CONSOLE
2626
| eff
@@ -33,7 +33,7 @@ runGame env = do
3333
lineHandler
3434
:: GameState
3535
-> String
36-
-> Eff ( err :: EXCEPTION
36+
-> Eff ( exception :: EXCEPTION
3737
, console :: CONSOLE
3838
, readline :: RL.READLINE
3939
| eff
@@ -51,7 +51,7 @@ runGame env = do
5151

5252
pure unit
5353

54-
main :: Eff ( err :: EXCEPTION
54+
main :: Eff ( exception :: EXCEPTION
5555
, console :: CONSOLE
5656
, readline :: RL.READLINE
5757
) Unit

exercises/chapter12/bower.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
"package.json"
1818
],
1919
"dependencies": {
20-
"purescript-console": "^2.0.0",
21-
"purescript-functions": "^2.0.0",
22-
"purescript-lists": "^3.0.0",
23-
"purescript-parallel": "^2.0.0",
24-
"purescript-refs": "^2.0.0",
25-
"purescript-strings": "^2.0.0",
26-
"purescript-transformers": "^2.0.0"
20+
"purescript-console": "^3.0.0",
21+
"purescript-functions": "^3.0.0",
22+
"purescript-lists": "^4.0.0",
23+
"purescript-parallel": "^3.0.0",
24+
"purescript-refs": "^3.0.0",
25+
"purescript-strings": "^3.0.0",
26+
"purescript-transformers": "^3.0.0"
2727
},
2828
"devDependencies": {
29-
"purescript-psci-support": "^2.0.0"
29+
"purescript-psci-support": "^3.0.0"
3030
}
3131
}

exercises/chapter12/src/Files.purs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ module Files where
33
import Prelude
44

55
import Control.Monad.Cont.Trans (ContT(..))
6-
import Control.Monad.Eff (Eff)
6+
import Control.Monad.Eff (kind Effect, Eff)
77
import Control.Monad.Except.Trans (ExceptT(..))
88
import Data.Either (Either(..))
99
import Data.Function.Uncurried (Fn4, Fn3, runFn4, runFn3)
1010
import Types (Async)
1111

12-
foreign import data FS :: !
12+
foreign import data FS :: Effect
1313

1414
type ErrorCode = String
1515

0 commit comments

Comments
 (0)