Applicative style for infix operators?
Asked Answered
H

3

8

Is there a way to make applicative uses of <$> and <*> look nice when dealing with infix operators? I think that

((++) <$> a <*> ((++) <$> b <*> c ))

looks much more cluttered then

a ++ b ++ c

so I wonder if there is a nicer way.

Howard answered 2/10, 2012 at 2:35 Comment(1)
If they are all the same operator, you could use foldr1 (liftA2 (++)) [a, b, c].Immunoreaction
O
13
(<++>) = liftA2 (++)
a <++> b <++> c

or

liftA2 (++) a $ liftA2 (++) b c
Overscrupulous answered 2/10, 2012 at 2:44 Comment(1)
yup - add another pair of angle brackets.Instrumentation
H
6

See Thomas Davie's InfixApplicative package, which provides a general syntactic trick, rather than having to define a new operator for each lifting.

Hugues answered 2/10, 2012 at 6:19 Comment(1)
The documentation is a bit mangled. I think the OP's example would translate as a <^(++)^> (b <^(++)^> c) --- can you confirm/deny?Mikaela
S
3

SHE lets you write

(|a ++ (|b ++ c|)|)

if that's any use. Of course, there's some overhead to introducing a preprocessing layer.

Selfconsistent answered 2/10, 2012 at 7:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.