-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Filter(f, tuple) #32968
Filter(f, tuple) #32968
Conversation
Test failure should be fixed by #32965? To be able to filter by type, it's tempting to add something like this, working around the lack of
|
Yes, I can confirm that the doctest failure was my fault and unrelated to this :sheepish_grin: |
I'd suggest something simpler: filter(f, t::Tuple) = _filterargs(f, t...)
_filterargs(f) = ()
_filterargs(f, x, xs...) =
f(x) ? (x, _filterargs(f, xs...)...) : _filterargs(f, xs...) It can handle julia> f(::Type{T}) where T = filter(x -> x isa T, (0, 1.0, 2, 3.0))
f (generic function with 1 method)
julia> @code_typed f(Int)
CodeInfo(
1 ─ %1 = Core.tuple(0, 2)::Tuple{Int64,Int64}
└── return %1
) => Tuple{Int64,Int64} |
I played with a few variants, this one is fast on the things I was testing: @btime filter(x->x>2, (1,2,3,4,5)) # 19.337 ns this PR, 527.370 ns tkf's, 1.250 μs my most compact But @tkf's is faster on
But I don't know exactly what is fair in timing such things, |
Polite bump — what can I do to push this along? |
@JeffBezanson, please merge if this looks acceptable to you. |
Actually, here is an even simpler and better approach equivalent to #32968 (comment) (for short tuples): filter(f, xs::Tuple) = afoldl((ys, x) -> f(x) ? (ys..., x) : ys, (), xs...) @mcabbott IMHO, I think you need to re-write your code. For example, |
Thanks, that’s very neat, I did not know about |
Co-authored-by: Takafumi Arakaki <[email protected]>
Done! I also added a news item. |
Bump: @JeffBezanson can you review when you have a minute? |
Bump? Red X is from win32, "cp: cannot stat 'dist-extras/7z.*’”, surely unrelated. |
Co-authored-by: Takafumi Arakaki <[email protected]>
Closes #30418