Hlint suggestion: use uncurry

230 Views Asked by At

I have this line of code:

map (\(u,v) -> flatTorus n u v) gridUV

Hlint suggests me to replace it with

map (uncurry (flatTorus n)) gridUV

What is the motivation of this suggestion ? Is it for shortness only, or something else (performance) ? Because though it is longer, I find the first code more easy to read.

In fact my question is more general,, because this is just an example among others: does Hlint suggestions are generally based on a shortness motivation only or are there other improvements behind the suggestions ?

2

There are 2 best solutions below

0
Fried Brice On

I think Hlint prefers using uncurry because it gives you an invariant representation of the callback. Lambda expressions are inherently sensitive to representation, since

\(u, v) -> flatTorus n u v

is equivalent to

\(x, y) -> flatTorus n x y

even though they are textually different.

Using uncurry frees readers of the cognitive load of doing alpha equivalence in their head (e.g., recognizing that the above two expressions are the same), but then saddles them with the cognitive load of having to remember a vocabulary of combinators. Ultimately, it's a matter of taste.

1
dfeuer On

These are not actually quite equivalent.

(\(x, y) -> (,) x y) undefined = undefined
uncurry (,) undefined = (undefined, undefined)

Do you should take any suggestion to use uncurry with a grain of salt. Think about whether that extra laziness will help, hurt, or make no difference.