It’s not part of
elm-review-simplify because this is not a simplification 100% of the time in terms of readability.
|> List.map toX
|> List.map toY
may be more readable than
|> List.map (toX >> toY)
especially for people who dislike or have trouble reading
>>. Also, in some cases, things get pretty
|> List.map toX
(if condition then
>> (if condition then
which I believe is worse (not that the former was that much better though).
The Elm compiler actually does very little work transforming the code. As far as I can tell, it transforms things like
+into real additions (instead of function calls) and
|> into regular function calls, and that’s the extent to which the compiler optimizes things (and it’s already really fast ).
I am currently looking into merging map functions and the sorts together through
elm-optimize-level-2. If it works out well, then hopefully it will make it into the compiler at one point. From preliminary benchmarks, it does look like it improves performance.
That said, there is a problem with optimizing Elm core code like this, which is that custom code won’t receive the same optimizations. Say you wrote a thin wrapper around List named
MyList to enforce some constraints. If the compiler were to optimize
a |> List.map f |> List.map g, it wouldn’t do the same thing for
a |> MyList.map f |> MyList.map g, because the compiler doesn’t know how this
map function works.
And the same thing applies for other kinds of optimizations. If you want to benefit from the same optimizations because
MyList is used in performance-critical code, you’d need to remove the wrapper and start using
List, which is not an incentive for good and maintainable code.
It’s like having regular-speed car lanes on a highway for almost everything in Elm, and then a fast lane for core custom types. Once you have a fast lane, the other lanes become slow lanes.
Does this mean we shouldn’t apply this transformation? Not necessarily, but it’s something to keep in mind. I believe we should try and find ways to make all lanes fast, but that will probably be hard.