Proposal: Add (&) to Data.Function

Edward Kmett ekmett at gmail.com
Tue Nov 20 21:44:24 CET 2012


On Tue, Nov 20, 2012 at 3:32 PM, Dan Burton <danburton.email at gmail.com>wrote:

> Functional metapost called it (#). But for me (>>>) is ok. It is even more
>> descriptive than (&).
>
>
> The reason I feel that >>> is inadequate is because it requires that you
> use either parens or $ in order to finally apply the chain of functions to
> a value. Also, being part of a typeclass can scare newcomers from
> using/understanding it.
>
> (foo >>> bar >>> baz) val
> foo >>> bar >>> baz $ val
>
> Blegh.
>
> Part of the reason I like |> is because it *is* a little more clutter-y
> than &. This forces you to use it prudently, with adequate whitespace. For
> example, when I was writing a StackOverflow answer[1] a few days ago, I
> found that using this operator was unreadable unless I used additional
> newlines. Lining up the |> operators actually produces a rather nice visual
> effect, as if you were entering each transformation at a command prompt.
> The vertical bars lining up is also nice. Consider the "butt-ugly" code
> (according to Thomas):
>
>  ("hello","world")  &  _1.traverse %~ toUpper  & _2 .~ 42
>


> Rewritten with whitespace, it can look much prettier:
>
> ("Hello", "world")
>   |> _1.traverse %~ toUpper
>   |> _2          .~ 42
>

You can make pretty much any mishmash of operators more palatable by
creative spacing and lots of newlines. If you dig around in the lens code,
many of the uses of (&) are spaced exactly this way.

The case is perhaps made better by DSLs that themselves aren't operator
ridden, so we don't get muddled in precedence issues:

>>> def & forward 10 & down & turn (pi/2) & forward 5
Turtle {_tPoint = Point {_x = 5.0, _y = 10.0}, _tColor = Color {_r = 0, _g
= 0, _b = 0}, _heading = 1.5707963267948966, _penDown = True}

When the operations are as individually small as I sketched above though,
the overhead of that operator accretes pretty fast, and the overhead of all
those newlines gets silly. =) Admittedly, I perhaps should have chosen an
example where the (|>) looks less like the turtle in question. ;)

-Edward
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/libraries/attachments/20121120/f57c3828/attachment.htm>


More information about the Libraries mailing list