maxlength(l) = findmax(map(length, l))
When I look at the compiled code, I see a reference to
collect_similar, so it is materializing the map result. I don’t want that inefficiency.
I know I can write the equivalent using
foldl, and get the call to
length inside the loop for
foldl. But that is not very expressive. I see that many functions over iterables, such as
maximum, take a function argument for this purpose. So it would be okay with me if there were a
findmax method that took a function as first argument. If that’s the Julia way of dealing with this sort of issue, then I’m happy to submit a PR to implement it.
But the language designer in me cringes at the proliferation of such methods. I would prefer that the code I wrote above, would optimize the function call into the loop, so the result of the
map does not get materialized. Then I would consider deprecating methods like the
sum methods that take a function argument, since
maximum(f, i) === maximum(map(f, i))
I will understand if having methods that take a function argument are preferred, since they work well with
If there is agreement that optimizing
map in this way is desirable, I might consider implementing it. Many years ago I was a compiler writer. I have not looked at the Julia compiler. I would need someone to point me in the right direction. I would try to generalize this optimization, but I’d need guidance with that.
Anyway – I want to be able to write either the code at the top, or
findmax(length, l). Enough that I’m willing to do the work, if you tell me which way is preferable for Julia.