rikhuijzer/ShowLint

`findall(x -> !x, Y)` -> `findall(.!Y)`

rikhuijzer opened this issue · 3 comments

`findall(x -> !x, Y)` -> `findall(.!Y)`

Better, you could map x -> !x to !. But why isn't this caught by the general x -> f(x) rule?

Because I'm not sure whether and how I can make the match on the brackets after f optional.

I'll make a separate pattern to match those 👍

EDIT. For later reference. From Discourse:

julia> @btime findfirst(x->!x, r) setup=(r=rand(Bool, 10^6)); # same as findfirst(!, r)
  2.283 ns (0 allocations: 0 bytes)

julia> @btime findfirst(.!r) setup=(r=rand(Bool, 10^6));
  137.891 μs (4 allocations: 126.42 KiB)

julia> @btime count(x->!x, r) setup=(r=rand(Bool, 10^6));
  80.810 μs (0 allocations: 0 bytes)

julia> @btime count(.!r) setup=(r=rand(Bool, 10^6));
  142.010 μs (4 allocations: 126.42 KiB)

julia> @btime findall(x->!x, r) setup=(r=rand(Bool, 10^6));
  671.744 μs (8 allocations: 3.92 MiB)

julia> @btime findall(.!r) setup=(r=rand(Bool, 10^6));
  668.053 μs (6 allocations: 3.93 MiB)

To save myself some work, I did not use the broadcasting. The reason is that not all functions which take two parameters, like map(x -> !x, A) (where ! can be any unary operator) can be rewritten in the broadcast form and take one parameter map(!.A). So, then I would need a whitelist of all functions which work on two and one function, which is too laborious.

In at most 20 minutes, pattern 8 will be online which checks for !, ~, +, and -.

Thanks for thinking about this problem 😄