What's more "purescriptonic": parentheses or '$'?

When I want to get the tail array of a non-empty array data, what is more idiomatic in PureScript:

Maybe.fromMaybe [] (Array.tail data)

or

Maybe.fromMaybe [] $ Array.tail data

The former feels more “sensible” to me, the PureScript newbie. However I can see that the “flatness” of the second way is attractive, too.

3 Likes

By and large, whichever style reads best to you and the people who write code with you is correct.

Two thoughts that might be worth considering: As a functional language, PureScript tends to encourage nesting a lot of function calls; the $ style saves you from having to stack close parentheses at the end of an expression, which can save you (a tiny amount, but multiplied over many occurrences, of) time and lead to cleaner diffs when adding applications to the front. On the other hand, there are occasionally type inference issues caused or exacerbated by using $, and for that reason using parentheses exclusively as a beginner may help you learn the language. Experienced PureScript developers may favor one or the other style or use a mix, for these or for other reasons; none of these choices are generally considered unidiomatic.

7 Likes

In Haskell I could also write fromMaybe [] . tail $ arr, but I’m not writing this way in PS due to verbose <<< category composition. But if you can eta reduce, fromMaybe [] <<< tail is also ok

verbose <<< category composition

Unicode, single chars, and math symbols to the rescue!

infixr 9 compose as ∘

foo = fromMaybe [] ∘ tail

-- or just

bar = fold ∘ tail

But I agree with the sibling comment that whatever subjectively reads better is the actual practice. It seems most libraries don’t like the idea of vehemently sticking to one or the other and that’s probably for the best.

1 Like

Also have

infixr 9 compose as ⋘

bar = fold ⋘ tail
1 Like

How do you write unicode chars in your editor? Do you have some latex plugin or something?

1 Like

Vim digraphs: digraph - Vim Documentation

ctrl+k then FA and I get a
ctrl+k then Ob and I get a

Although I prefer the compose operator from CS to whatever this is :stuck_out_tongue: (Function composition (computer science) - Wikipedia)

Do note that while PureScript does support Unicode, a feature I’m grateful for, it is by no means neither necessary nor recommended to create such operators unless they spark joy in you personally. To some it looks nice and others it matches closer to the papers they are reading. Non-Unicoders say it scares users unfamiliar, is harder to type (though ask some people how easy $ is to type), yak-shaves, and goes from 1 way to use an operator to 2 (though all infixes require a named variation as well so technically 3 ways).

1 Like

If you use VS Code, I like the Insert Unicode plugin plus a few keyboard shortcuts. I mostly use it for the symbol in place of forall.

2 Likes

$ causing inference problems definitely made me skew more towards just parentheses, though I find myself using $ from time to time for trivial stuff.

3 Likes

Thanks for pointing the inference problems out. For a newbie definitely good to know about!

Also, I think I have noticed in case I make syntactical mistakes, the error messagse seem to be less cryptic when using parentheses…

1 Like

shameless plug of point-free, which gives you <. and .>

Regarding unicode in my editor, I just use abbreviations

iabbrev <buffer> forall ∀

I have this in my puresript.vim ftplugin, so when I type forall in a purescript file, it converts it to

2 Likes

I prefer the “flat” style with $.