You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here comes the interesting part: we check the head, if it works, we increment
130
+
the position, and left the input untouched.
131
+
132
+
We can easily unterstand the reason behind `attoparsec`'s speed: a basic error
133
+
handling, and a small state.
134
+
135
+
# Zepto
136
+
137
+
`attoparsec` comes with another parser combinators type: [`Zepto`](https://hackage.haskell.org/package/attoparsec-0.14.3/docs/Data-Attoparsec-Zepto.html):
138
+
139
+
> A tiny, highly specialized combinator parser for `ByteString` strings.
140
+
>
141
+
> While the main attoparsec module generally performs well, this module is particularly fast for simple non-recursive loops that should not normally result in failed parses.
142
+
143
+
Let's have a look:
144
+
145
+
```haskell
146
+
--| A simple parser.
147
+
--
148
+
-- This monad is strict in its state, and the monadic bind operator
149
+
-- ('>>=') evaluates each result to weak head normal form before
150
+
-- passing it along.
151
+
newtypeZeptoTma=Parser{
152
+
runParser::S->m (Resulta)
153
+
}
154
+
155
+
typeParsera=ZeptoTIdentitya
156
+
157
+
newtypeS=S{
158
+
input::ByteString
159
+
}
160
+
161
+
dataResulta=FailString
162
+
| OK!aS
163
+
```
164
+
165
+
Definitively the simplest parser combinator you can come up with.
166
+
167
+
```haskell
168
+
instance (Monadm) =>Applicative (ZeptoTm) where
169
+
pure a =Parser$\s ->return (OK a s)
170
+
171
+
instanceMonadm=>Alternative (ZeptoTm) where
172
+
empty =fail"empty"
173
+
174
+
a <|> b =Parser$\s ->do
175
+
result <- runParser a s
176
+
case result of
177
+
ok@(OK _ _) ->return ok
178
+
_ -> runParser b s
179
+
180
+
instanceMonadm=>Monad (ZeptoTm) where
181
+
m >>= k =Parser$\s ->do
182
+
result <- runParser m s
183
+
case result of
184
+
OK a s' -> runParser (k a) s'
185
+
Fail err ->return (Fail err)
186
+
```
187
+
188
+
It looks tedious because you deal with `Result`, but, in the end, you have all
189
+
the boilerplate needed to create a function stored in `Parser`.
0 commit comments