Why no single argument `filter(f::function)`?

Is there a good reason that there is no single-argument version of filter that returns a filter-function? I.e. why is the following (or something similar) not in Base

filter(f::Function) = Base.Fix1(filter, f)

?

This would be in line with the existence of <(x) = (y -> y < x) or occursin(haystack) = (needle -> (occusin(needle, haystack))) and very useful for data processing pipelines using |>.

I found this thread which disusses alternatives, but gives no reason why this shouldn’t exist in Base.

It’s included in Julia 1.9 :slight_smile:

8 Likes

Nice, looking forward to the release then!

The next question will of course be why map doesn’t have such a partially-applied form :wink:

Because map is variadic, so it needs to work on 0 data arguments. I’m not sure this was the best option, but it’s what we have.

Oh wow, I was expecting it to throw an error :sweat_smile:

julia> f() = 5
f (generic function with 1 method)

julia> map(f)
5

This behavior seems to violate any meaningful notion of mapping a collection[s] to another, and it’s not in the docstring. Under what circumstance is this useful?

For comparison:

>>> def f():
...     return 5
...
>>> list(map(f))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: map() must have at least two arguments.

Good point. Change map(f) behavior · Issue #48372 · JuliaLang/julia · GitHub

1 Like

Ah, a fun reminder of my favorite Julia code:

for zip! in zip()
    zip, zip, zip
    zip!, zip!, zip!
end
2 Likes

It’s entertaining how the issue that it’s a duplicate of, rapidly devolves into a discussion of PR#24990 :sweat_smile:. Which would have it written, map(f, _...).

1 Like