Skip to content

Commit 4293205

Browse files
authored
Merge pull request #27 from purescript-contrib/compiler/0.12
Updates for 0.12
2 parents e15eea5 + 31041d1 commit 4293205

File tree

9 files changed

+196
-258
lines changed

9 files changed

+196
-258
lines changed

.travis.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@ language: node_js
22
dist: trusty
33
sudo: required
44
node_js: stable
5+
env:
6+
- PATH=$HOME/purescript:$PATH
57
install:
8+
- TAG=$(wget -q -O - https://github.com/purescript/purescript/releases/latest --server-response --max-redirect 0 2>&1 | sed -n -e 's/.*Location:.*tag\///p')
9+
- wget -O $HOME/purescript.tar.gz https://github.com/purescript/purescript/releases/download/$TAG/linux64.tar.gz
10+
- tar -xvf $HOME/purescript.tar.gz -C $HOME/
11+
- chmod a+x $HOME/purescript
612
- npm install -g bower
713
- npm install
814
script:
915
- bower install --production
1016
- npm run -s build
1117
- bower install
12-
- npm -s test
18+
- npm run -s test
1319
after_success:
1420
- >-
1521
test $TRAVIS_TAG &&

README.md

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![Build Status](https://travis-ci.org/purescript-contrib/purescript-argonaut-core.svg?branch=master)](https://travis-ci.org/purescript-contrib/purescript-argonaut-core)
55
[![Maintainer: slamdata](https://img.shields.io/badge/maintainer-slamdata-lightgrey.svg)](http://github.com/slamdata)
66

7-
Core part of `purescript-argonaut` that contains basic types for `Json`, folds over them, tests, printer and parser.
7+
Core part of `purescript-argonaut` that contains basic types for `Json`, case analysis, printer and parser.
88

99
## Installation
1010

@@ -31,7 +31,7 @@ data Json
3131
| JNumber Number
3232
| JBoolean Boolean
3333
| JArray (Array Json)
34-
| JObject (StrMap Json)
34+
| JObject (Object Json)
3535
```
3636

3737
And indeed, some might even say this is the obvious approach.
@@ -45,25 +45,6 @@ both in terms of speed and memory churn.
4545

4646
Much of the design of Argonaut follows naturally from this design decision.
4747

48-
### Types
49-
50-
The most important type in this library is, of course, `Json`, which is the
51-
type of JSON data in its native JavaScript representation.
52-
53-
As the (hypothetical) algebraic data type declaration above indicates, there
54-
are six possibilities for a JSON value: it can be `null`, a string, a number, a
55-
boolean, an array of JSON values, or an object mapping string keys to JSON
56-
values.
57-
58-
For convenience, and to ensure that values have the appropriate underlying
59-
data representations, Argonaut also declares types for each of these individual
60-
possibilities, whose names correspond to the data constructor names above.
61-
62-
Therefore, `JString`, `JNumber`, and `JBoolean` are synonyms for the primitive
63-
PureScript types `String`, `Number`, and `Boolean` respectively; `JArray` is a
64-
synonym for `Array Json`; and `JObject` is a synonym for `StrMap Json`.
65-
Argonaut defines a type `JNull` as the type of the `null` value in JavaScript.
66-
6748
### Introducing Json values
6849

6950
(Or, where do `Json` values come from?)
@@ -97,7 +78,7 @@ follows:
9778

9879
```purescript
9980
import Data.Tuple (Tuple(..))
100-
import Data.StrMap as StrMap
81+
import Foreign.Object as StrMap
10182
import Data.Argonaut.Core as A
10283
10384
someNumber = A.fromNumber 23.6
@@ -113,32 +94,41 @@ someObject = A.fromObject (StrMap.fromFoldable [
11394

11495
### Eliminating/matching on `Json` values
11596

116-
We can perform case analysis for `Json` values using the `foldJson` function.
97+
We can perform case analysis for `Json` values using the `caseJson` function.
11798
This function is necessary because `Json` is not an algebraic data type. If
11899
`Json` were an algebraic data type, we would not have as much need for this
119100
function, because we could perform pattern matching with a `case ... of`
120101
expression instead.
121102

122-
The type of `foldJson` is:
103+
The type of `caseJson` is:
123104

124105
```purescript
125-
foldJson :: forall a.
126-
(JNull -> a) -> (JBoolean -> a) -> (JNumber -> a) ->
127-
(JString -> a) -> (JArray -> a) -> (JObject -> a) ->
128-
Json -> a
106+
caseJson
107+
:: forall a
108+
. (Unit -> a)
109+
-> (Boolean -> a)
110+
-> (Number -> a)
111+
-> (String -> a)
112+
-> (Array Json -> a)
113+
-> (Object Json -> a)
114+
-> Json
115+
-> a
129116
```
130117

131-
That is, `foldJson` takes six functions, which all must return values of some
132-
particular type `a`, together with one `Json` value. `foldJson` itself also
118+
That is, `caseJson` takes six functions, which all must return values of some
119+
particular type `a`, together with one `Json` value. `caseJson` itself also
133120
returns a value of the same type `a`.
134121

135-
A use of `foldJson` is very similar to a `case ... of` expression, as it allows
136-
you to handle each of the six possibilities for the `Json` value you passed in.
137-
Thinking of it this way, each of the six function arguments is like one of the
138-
case alternatives. Just like in a `case ... of` expression, the final value
122+
A use of `caseJson` is very similar to a `case ... of` expression, as it allows
123+
you to handle each of the six possibilities for the `Json` value you passed in. Thinking of it this way, each of the six function arguments is like one of the
124+
case alternatives.
125+
126+
The function that takes `Unit` as an argument is for matching `null` values. As there is only one possible `null` value, we use the PureScript `Unit` type, as correspondingly there is only one possible `Unit` value.
127+
128+
Just like in a `case ... of` expression, the final value
139129
that the whole expression evaluates to comes from evaluating exactly one of the
140130
'alternatives' (functions) that you pass in. In fact, you can tell that this
141-
is the case just by looking at the type signature of `foldJson`, because of a
131+
is the case just by looking at the type signature of `caseJson`, because of a
142132
property called *parametricity* (although a deeper explanation of parametricity
143133
is outside the scope of this tutorial).
144134

@@ -151,15 +141,15 @@ exports.anotherArray = [0.0, {foo: 'bar'}, false];
151141
exports.anotherObject = {foo: 1, bar: [2,2]};
152142
```
153143

154-
Then we can match on them in PureScript using `foldJson`:
144+
Then we can match on them in PureScript using `caseJson`:
155145

156146
```purescript
157147
foreign import anotherNumber :: Json
158148
foreign import anotherArray :: Json
159149
foreign import anotherObject :: Json
160150
161151
basicInfo :: Json -> String
162-
basicInfo = foldJson
152+
basicInfo = caseJson
163153
(const "It was null")
164154
(\b -> "Got a boolean: " <>
165155
if b then "it was true!" else "It was false.")
@@ -168,7 +158,7 @@ basicInfo = foldJson
168158
" characters long.")
169159
(\xs -> "Got an array, which had " <> Data.Array.length xs <>
170160
" items.")
171-
(\obj -> "Got an object, which had " <> Data.StrMap.size obj <>
161+
(\obj -> "Got an object, which had " <> Foreign.Object.size obj <>
172162
" items.")
173163
```
174164

@@ -178,20 +168,20 @@ basicInfo anotherArray -- => "Got an array, which had 3 items."
178168
basicInfo anotherObject -- => "Got an object, which had 2 items."
179169
```
180170

181-
`foldJson` is the fundamental function for pattern matching on `Json` values;
182-
any kind of pattern matching you might want to do can be done with `foldJson`.
171+
`caseJson` is the fundamental function for pattern matching on `Json` values;
172+
any kind of pattern matching you might want to do can be done with `caseJson`.
183173

184-
However, `foldJson` is not always comfortable to use, so Argonaut provides a
185-
few other simpler versions for convenience. For example, the `foldJsonX`
174+
However, `caseJson` is not always comfortable to use, so Argonaut provides a
175+
few other simpler versions for convenience. For example, the `caseJsonX`
186176
functions can be used to match on a specific type. The first argument acts as a
187177
default value, to be used if the `Json` value turned out not to be that type.
188178
For example, we can write a function which tests whether a JSON value is the
189179
string "lol" like this:
190180

191181
```purescript
192-
foldJsonString :: forall a. a -> (JString -> a) -> Json -> a
182+
caseJsonString :: forall a. a -> (String -> a) -> Json -> a
193183
194-
isJsonLol = foldJsonString false (_ == "lol")
184+
isJsonLol = caseJsonString false (_ == "lol")
195185
```
196186

197187
If the `Json` value is not a string, the default `false` is used. Otherwise,
@@ -203,7 +193,7 @@ type, you'll get a `Just` value. Otherwise, you'll get `Nothing`. For example,
203193
we could have written `isJsonLol` like this, too:
204194

205195
```purescript
206-
toString :: Json -> Maybe JString
196+
toString :: Json -> Maybe String
207197
208198
isJsonLol json =
209199
case toString json of

bower.json

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,19 @@
2727
"package.json"
2828
],
2929
"dependencies": {
30-
"purescript-enums": "^3.0.0",
31-
"purescript-functions": "^3.0.0",
32-
"purescript-gen": "^1.0.0",
33-
"purescript-maps": "^3.0.0"
30+
"purescript-arrays": "^5.0.0",
31+
"purescript-control": "^4.0.0",
32+
"purescript-either": "^4.0.0",
33+
"purescript-foreign-object": "^1.0.0",
34+
"purescript-functions": "^4.0.0",
35+
"purescript-gen": "^2.0.0",
36+
"purescript-maybe": "^4.0.0",
37+
"purescript-nonempty": "^5.0.0",
38+
"purescript-prelude": "^4.0.0",
39+
"purescript-strings": "^4.0.0",
40+
"purescript-tailrec": "^4.0.0"
3441
},
3542
"devDependencies": {
36-
"purescript-quickcheck": "^4.6.1"
43+
"purescript-quickcheck": "^5.0.0"
3744
}
3845
}

package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
"test": "pulp test"
77
},
88
"devDependencies": {
9-
"eslint": "^3.19.0",
10-
"pulp": "^11.0.0",
11-
"purescript-psa": "^0.5.0",
12-
"purescript": "^0.11.1",
13-
"rimraf": "^2.6.1"
9+
"eslint": "^4.19.1",
10+
"pulp": "^12.2.0",
11+
"purescript-psa": "^0.6.0",
12+
"rimraf": "^2.6.2"
1413
}
1514
}

src/Data/Argonaut/Core.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ function id(x) {
44
return x;
55
}
66

7-
exports.fromNull = function () {
8-
return null;
9-
};
10-
117
exports.fromBoolean = id;
128
exports.fromNumber = id;
139
exports.fromString = id;
@@ -27,8 +23,8 @@ function isArray(a) {
2723
return objToString.call(a) === "[object Array]";
2824
}
2925

30-
exports._foldJson = function (isNull, isBool, isNum, isStr, isArr, isObj, j) {
31-
if (j == null) return isNull(null);
26+
exports._caseJson = function (isNull, isBool, isNum, isStr, isArr, isObj, j) {
27+
if (j == null) return isNull();
3228
else if (typeof j === "boolean") return isBool(j);
3329
else if (typeof j === "number") return isNum(j);
3430
else if (typeof j === "string") return isStr(j);

0 commit comments

Comments
 (0)