I am writing an xml-conduit parser, and I prefer applicative syntax to monadic. With lots of arguments to combine, I get somewhat lost in applicative though. My current problem is given 8 arguments, I only want to use the 4th and the 6th to construct the result.
The only way I could make it work was the following; there should be fancy arrangement of stars for a flat solution though:
import Control.Applicative
a1 :: Applicative Text
a2 :: Applicative Text
a3 :: Applicative Text
a4 :: Applicative Text
a5 :: Applicative Text
a6 :: Applicative Text
a7 :: Applicative Text
a8 :: Applicative Text
data Data = Data Text Text
f :: Text -> Text -> Data
parser :: Applicative Data
parser = a1 *> a2 *> a3 *> (f <$> a4 <* a5 <*> a6) <* a7 <* a8
Any way to do the same in the form without parentheses?
parser = f <$> a1 ?? a2 ?? a3 ?? a4 ?? a5 ?? a6 ?? a7 ?? a8
Aha, the suggested link Applicative style parser for constructor with two arguments led me to the answer: use (<$)
, don't use (*>)
.
parser = f <$ a1 <* a2 <* a3 <*> a4 <* a5 <*> a6 <* a7 <* a8
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With