Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring arguments in Control.Applicative

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
like image 437
Skirmantas Kligys Avatar asked Apr 17 '13 03:04

Skirmantas Kligys


1 Answers

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
like image 109
Skirmantas Kligys Avatar answered Nov 07 '22 17:11

Skirmantas Kligys