Skip to content

Commit 269ace0

Browse files
Merge pull request #63 from tesujimath/guide-operator-precedence
Fix the operator precedence examples.
2 parents 629e99b + b8c731d commit 269ace0

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

GUIDE.md

+17-13
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,24 @@ post =
9494
9595
postEdit :: Match MyRoute
9696
postEdit =
97-
PostEdit <$> (lit "posts" *> int <* lit "edit")
97+
PostEdit <$> (lit "posts" *> int) <* lit "edit"
9898
```
9999

100100
Note the use of the `*>` and `<*` operators. These let us direct the focus of
101101
the value we want to consume. In `postEdit`, we want to consume the `int`,
102102
but we also need to match the "edit" suffix. The arrows point to the value we
103103
want.
104104

105+
Note that in general parentheses are required when using `*>` since the
106+
operator precedence is not what is required (resulting in type errors
107+
otherwise.)
108+
105109
And now finally, we need to extract multiple segments for `PostBrowse`.
106110

107111
```purescript
108112
postBrowse :: Match MyRoute
109113
postBrowse =
110-
PostBrowse <$> (lit "posts" *> str <*> str)
114+
PostBrowse <$> (lit "posts" *> str) <*> str
111115
```
112116

113117
The `<*>` combinator has arrows on both sides because we want both values.
@@ -145,8 +149,8 @@ myRoute :: Match MyRoute
145149
myRoute = oneOf
146150
[ PostIndex <$ lit "posts"
147151
, Post <$> (lit "posts" *> int)
148-
, PostEdit <$> (lit "posts" *> int <* lit "edit")
149-
, PostBrowse <$> (lit "posts" *> str <*> str)
152+
, PostEdit <$> (lit "posts" *> int) <* lit "edit"
153+
, PostBrowse <$> (lit "posts" *> str) <*> str
150154
]
151155
```
152156

@@ -161,8 +165,8 @@ myRoute =
161165
lit "posts" *> oneOf
162166
[ pure PostIndex
163167
, Post <$> int
164-
, PostEdit <$> (int <* lit "edit")
165-
, PostBrowse <$> (str <*> str)
168+
, PostEdit <$> int <* lit "edit"
169+
, PostBrowse <$> str <*> str
166170
]
167171
```
168172

@@ -179,9 +183,9 @@ myRoute :: Match MyRoute
179183
myRoute =
180184
lit "posts" *> oneOf
181185
[ PostIndex <$ end
182-
, Post <$> (int <* end)
183-
, PostEdit <$> (int <* lit "edit" <* end)
184-
, PostBrowse <$> (str <*> str <* end)
186+
, Post <$> int <* end
187+
, PostEdit <$> int <* lit "edit" <* end
188+
, PostBrowse <$> str <*> str <* end
185189
]
186190
```
187191

@@ -193,9 +197,9 @@ the routes followed by an `end`, so we would still have to rearrange them.
193197
myRoute :: Match MyRoute
194198
myRoute =
195199
lit "posts" *> oneOf
196-
[ PostEdit <$> (int <* lit "edit")
200+
[ PostEdit <$> int <* lit "edit"
197201
, Post <$> int
198-
, PostBrowse <$> (str <*> str)
202+
, PostBrowse <$> str <*> str
199203
, pure PostIndex
200204
] <* end
201205
```
@@ -211,9 +215,9 @@ with the `root` combinator.
211215
myRoute :: Match MyRoute
212216
myRoute =
213217
root *> lit "posts" *> oneOf
214-
[ PostEdit <$> (int <* lit "edit")
218+
[ PostEdit <$> int <* lit "edit"
215219
, Post <$> int
216-
, PostBrowse <$> (str <*> str)
220+
, PostBrowse <$> str <*> str
217221
, pure PostIndex
218222
] <* end
219223
```

0 commit comments

Comments
 (0)