Skip to content

Commit bfaec59

Browse files
committed
Rename fold functions to case #24
1 parent 83dd71f commit bfaec59

File tree

3 files changed

+58
-58
lines changed

3 files changed

+58
-58
lines changed

README.md

Lines changed: 16 additions & 16 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

@@ -94,16 +94,16 @@ someObject = A.fromObject (StrMap.fromFoldable [
9494

9595
### Eliminating/matching on `Json` values
9696

97-
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.
9898
This function is necessary because `Json` is not an algebraic data type. If
9999
`Json` were an algebraic data type, we would not have as much need for this
100100
function, because we could perform pattern matching with a `case ... of`
101101
expression instead.
102102

103-
The type of `foldJson` is:
103+
The type of `caseJson` is:
104104

105105
```purescript
106-
foldJson
106+
caseJson
107107
:: forall a
108108
. (Unit -> a)
109109
-> (Boolean -> a)
@@ -115,11 +115,11 @@ foldJson
115115
-> a
116116
```
117117

118-
That is, `foldJson` takes six functions, which all must return values of some
119-
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
120120
returns a value of the same type `a`.
121121

122-
A use of `foldJson` is very similar to a `case ... of` expression, as it allows
122+
A use of `caseJson` is very similar to a `case ... of` expression, as it allows
123123
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
124124
case alternatives.
125125

@@ -128,7 +128,7 @@ The function that takes `Unit` as an argument is for matching `null` values. As
128128
Just like in a `case ... of` expression, the final value
129129
that the whole expression evaluates to comes from evaluating exactly one of the
130130
'alternatives' (functions) that you pass in. In fact, you can tell that this
131-
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
132132
property called *parametricity* (although a deeper explanation of parametricity
133133
is outside the scope of this tutorial).
134134

@@ -141,15 +141,15 @@ exports.anotherArray = [0.0, {foo: 'bar'}, false];
141141
exports.anotherObject = {foo: 1, bar: [2,2]};
142142
```
143143

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

146146
```purescript
147147
foreign import anotherNumber :: Json
148148
foreign import anotherArray :: Json
149149
foreign import anotherObject :: Json
150150
151151
basicInfo :: Json -> String
152-
basicInfo = foldJson
152+
basicInfo = caseJson
153153
(const "It was null")
154154
(\b -> "Got a boolean: " <>
155155
if b then "it was true!" else "It was false.")
@@ -168,20 +168,20 @@ basicInfo anotherArray -- => "Got an array, which had 3 items."
168168
basicInfo anotherObject -- => "Got an object, which had 2 items."
169169
```
170170

171-
`foldJson` 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 `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`.
173173

174-
However, `foldJson` is not always comfortable to use, so Argonaut provides a
175-
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`
176176
functions can be used to match on a specific type. The first argument acts as a
177177
default value, to be used if the `Json` value turned out not to be that type.
178178
For example, we can write a function which tests whether a JSON value is the
179179
string "lol" like this:
180180

181181
```purescript
182-
foldJsonString :: forall a. a -> (String -> a) -> Json -> a
182+
caseJsonString :: forall a. a -> (String -> a) -> Json -> a
183183
184-
isJsonLol = foldJsonString false (_ == "lol")
184+
isJsonLol = caseJsonString false (_ == "lol")
185185
```
186186

187187
If the `Json` value is not a string, the default `false` is used. Otherwise,

src/Data/Argonaut/Core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function isArray(a) {
2323
return objToString.call(a) === "[object Array]";
2424
}
2525

26-
exports._foldJson = function (isNull, isBool, isNum, isStr, isArr, isObj, j) {
26+
exports._caseJson = function (isNull, isBool, isNum, isStr, isArr, isObj, j) {
2727
if (j == null) return isNull();
2828
else if (typeof j === "boolean") return isBool(j);
2929
else if (typeof j === "number") return isNum(j);

src/Data/Argonaut/Core.purs

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
-- | for this module.
44
module Data.Argonaut.Core
55
( Json
6-
, foldJson
7-
, foldJsonNull
8-
, foldJsonBoolean
9-
, foldJsonNumber
10-
, foldJsonString
11-
, foldJsonArray
12-
, foldJsonObject
6+
, caseJson
7+
, caseJsonNull
8+
, caseJsonBoolean
9+
, caseJsonNumber
10+
, caseJsonString
11+
, caseJsonArray
12+
, caseJsonObject
1313
, isNull
1414
, isBoolean
1515
, isNumber
@@ -69,7 +69,7 @@ instance ordJNull :: Ord JNull where
6969
compare _ _ = EQ
7070

7171
-- | Case analysis for `Json` values. See the README for more information.
72-
foldJson
72+
caseJson
7373
:: forall a
7474
. (Unit -> a)
7575
-> (Boolean -> a)
@@ -78,62 +78,62 @@ foldJson
7878
-> (Array Json -> a)
7979
-> (Object Json -> a)
8080
-> Json -> a
81-
foldJson a b c d e f json = runFn7 _foldJson a b c d e f json
81+
caseJson a b c d e f json = runFn7 _caseJson a b c d e f json
8282

83-
-- | A simpler version of `foldJson` which accepts a callback for when the
83+
-- | A simpler version of `caseJson` which accepts a callback for when the
8484
-- | `Json` argument was null, and a default value for all other cases.
85-
foldJsonNull :: forall a. a -> (Unit -> a) -> Json -> a
86-
foldJsonNull d f j = runFn7 _foldJson f (const d) (const d) (const d) (const d) (const d) j
85+
caseJsonNull :: forall a. a -> (Unit -> a) -> Json -> a
86+
caseJsonNull d f j = runFn7 _caseJson f (const d) (const d) (const d) (const d) (const d) j
8787

88-
-- | A simpler version of `foldJson` which accepts a callback for when the
88+
-- | A simpler version of `caseJson` which accepts a callback for when the
8989
-- | `Json` argument was a `Boolean`, and a default value for all other cases.
90-
foldJsonBoolean :: forall a. a -> (Boolean -> a) -> Json -> a
91-
foldJsonBoolean d f j = runFn7 _foldJson (const d) f (const d) (const d) (const d) (const d) j
90+
caseJsonBoolean :: forall a. a -> (Boolean -> a) -> Json -> a
91+
caseJsonBoolean d f j = runFn7 _caseJson (const d) f (const d) (const d) (const d) (const d) j
9292

93-
-- | A simpler version of `foldJson` which accepts a callback for when the
93+
-- | A simpler version of `caseJson` which accepts a callback for when the
9494
-- | `Json` argument was a `Number`, and a default value for all other cases.
95-
foldJsonNumber :: forall a. a -> (Number -> a) -> Json -> a
96-
foldJsonNumber d f j = runFn7 _foldJson (const d) (const d) f (const d) (const d) (const d) j
95+
caseJsonNumber :: forall a. a -> (Number -> a) -> Json -> a
96+
caseJsonNumber d f j = runFn7 _caseJson (const d) (const d) f (const d) (const d) (const d) j
9797

98-
-- | A simpler version of `foldJson` which accepts a callback for when the
98+
-- | A simpler version of `caseJson` which accepts a callback for when the
9999
-- | `Json` argument was a `String`, and a default value for all other cases.
100-
foldJsonString :: forall a. a -> (String -> a) -> Json -> a
101-
foldJsonString d f j = runFn7 _foldJson (const d) (const d) (const d) f (const d) (const d) j
100+
caseJsonString :: forall a. a -> (String -> a) -> Json -> a
101+
caseJsonString d f j = runFn7 _caseJson (const d) (const d) (const d) f (const d) (const d) j
102102

103-
-- | A simpler version of `foldJson` which accepts a callback for when the
103+
-- | A simpler version of `caseJson` which accepts a callback for when the
104104
-- | `Json` argument was a `Array Json`, and a default value for all other cases.
105-
foldJsonArray :: forall a. a -> (Array Json -> a) -> Json -> a
106-
foldJsonArray d f j = runFn7 _foldJson (const d) (const d) (const d) (const d) f (const d) j
105+
caseJsonArray :: forall a. a -> (Array Json -> a) -> Json -> a
106+
caseJsonArray d f j = runFn7 _caseJson (const d) (const d) (const d) (const d) f (const d) j
107107

108-
-- | A simpler version of `foldJson` which accepts a callback for when the
108+
-- | A simpler version of `caseJson` which accepts a callback for when the
109109
-- | `Json` argument was an `Object`, and a default value for all other cases.
110-
foldJsonObject :: forall a. a -> (Object Json -> a) -> Json -> a
111-
foldJsonObject d f j = runFn7 _foldJson (const d) (const d) (const d) (const d) (const d) f j
110+
caseJsonObject :: forall a. a -> (Object Json -> a) -> Json -> a
111+
caseJsonObject d f j = runFn7 _caseJson (const d) (const d) (const d) (const d) (const d) f j
112112

113113
verbJsonType :: forall a b. b -> (a -> b) -> (b -> (a -> b) -> Json -> b) -> Json -> b
114-
verbJsonType def f fold = fold def f
114+
verbJsonType def f g = g def f
115115

116116
-- Tests
117117
isJsonType :: forall a. (Boolean -> (a -> Boolean) -> Json -> Boolean) -> Json -> Boolean
118118
isJsonType = verbJsonType false (const true)
119119

120120
isNull :: Json -> Boolean
121-
isNull = isJsonType foldJsonNull
121+
isNull = isJsonType caseJsonNull
122122

123123
isBoolean :: Json -> Boolean
124-
isBoolean = isJsonType foldJsonBoolean
124+
isBoolean = isJsonType caseJsonBoolean
125125

126126
isNumber :: Json -> Boolean
127-
isNumber = isJsonType foldJsonNumber
127+
isNumber = isJsonType caseJsonNumber
128128

129129
isString :: Json -> Boolean
130-
isString = isJsonType foldJsonString
130+
isString = isJsonType caseJsonString
131131

132132
isArray :: Json -> Boolean
133-
isArray = isJsonType foldJsonArray
133+
isArray = isJsonType caseJsonArray
134134

135135
isObject :: Json -> Boolean
136-
isObject = isJsonType foldJsonObject
136+
isObject = isJsonType caseJsonObject
137137

138138
-- Decoding
139139

@@ -145,22 +145,22 @@ toJsonType
145145
toJsonType = verbJsonType Nothing Just
146146

147147
toNull :: Json -> Maybe Unit
148-
toNull = toJsonType foldJsonNull
148+
toNull = toJsonType caseJsonNull
149149

150150
toBoolean :: Json -> Maybe Boolean
151-
toBoolean = toJsonType foldJsonBoolean
151+
toBoolean = toJsonType caseJsonBoolean
152152

153153
toNumber :: Json -> Maybe Number
154-
toNumber = toJsonType foldJsonNumber
154+
toNumber = toJsonType caseJsonNumber
155155

156156
toString :: Json -> Maybe String
157-
toString = toJsonType foldJsonString
157+
toString = toJsonType caseJsonString
158158

159159
toArray :: Json -> Maybe (Array Json)
160-
toArray = toJsonType foldJsonArray
160+
toArray = toJsonType caseJsonArray
161161

162162
toObject :: Json -> Maybe (Object Json)
163-
toObject = toJsonType foldJsonObject
163+
toObject = toJsonType caseJsonObject
164164

165165
-- Encoding
166166

@@ -200,7 +200,7 @@ jsonSingletonObject key val = fromObject (Obj.singleton key val)
200200

201201
foreign import stringify :: Json -> String
202202

203-
foreign import _foldJson
203+
foreign import _caseJson
204204
:: forall z
205205
. Fn7
206206
(Unit -> z)

0 commit comments

Comments
 (0)