[Haskell-cafe] avoiding parens in postfix applicative notation
Jason Shipman
jasonpshipman at gmail.com
Wed Feb 7 03:39:54 UTC 2018
Hi Johannes,
The lens library defines (<&>) with very low precedence (1), whereas (<$>) has precedence 4. If you define (<&>) yourself and specify a precedence higher than 4, or just don’t specify a precedence at all, your example will work fine:
(<&>) :: Functor f => f a -> (a -> b) -> f b
(<&>) = flip fmap
"foo" <**> "bar" <&> (,)
You can do hanging style too, with no dollar sign:
"foo" <**> "bar" <&> \q r ->
..(q, r)
If you don’t like defining ad-hoc versions of things like (<&>), you might find the ‘overhang’ library useful: https://hackage.haskell.org/package/overhang-1.0.0/docs/Overhang.html#v:onMap <https://hackage.haskell.org/package/overhang-1.0.0/docs/Overhang.html#v:onMap>
The overhang equivalent of (<&>) is ‘onMap’ and it can be used in the same way:
import Overhang (onMap)
"foo" <**> "bar" `onMap` (,)
The code aesthetics around writing a “final" lambda that spans several lines was the driver for creating that library!
Jason
> On Feb 6, 2018, at 12:37 PM, Neil Mayhew <neil_mayhew at users.sourceforge.net> wrote:
>
> On 2018-02-06 07:59 AM, MarLinn wrote:
>> I've been bitten multiple times because of my own invented operators. What was (>>?!) again? Or (^>>>&)? The more I use Haskell the more I tend to solutions like that first dead-simple one.
>
> I agree.
>
> Also, since
>
> func <$> "foo" <*> "bar"
>
> is the lifted equivalent of
>
> func "foo" "bar"
>
> I find it unintuitive to read or write the logic in the opposite order.
>
> Whether we like it or not, Haskell is fundamentally a right-to-left language. Or, to look at it another way, top-down corresponds to left-to-right, and bottom-up corresponds to right-to-left. Perhaps it depends on whether you're a top-down thinker (like me) or a bottom-up thinker. I much prefer `where` to `let`, for example.
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20180206/21fae036/attachment.html>
More information about the Haskell-Cafe
mailing list