Skip to content
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

searchsorted "by" method should not be applied to key #33997

Closed
StephenVavasis opened this issue Dec 1, 2019 · 1 comment
Closed

searchsorted "by" method should not be applied to key #33997

StephenVavasis opened this issue Dec 1, 2019 · 1 comment

Comments

@StephenVavasis
Copy link
Contributor

StephenVavasis commented Dec 1, 2019

This would be a breaking change, so probably it will be rejected, but anyway:

The function call searchsorted(v, key, by=mytransform) applies mytransform to key, which isn't helpful. It would be more useful if the searchsorted function assumes that the key is already of the type equal to the output of mytransform. The common use for searchsorted is to search the sorted array v for a particular key that is a field or is computed from the fields of the array entries. Typically, the user would know the key but would not want to construct a dummy record whose type is eltype(v) just to hold that key.

Just to be clear, here is a trace of what I mean. I think the first invocation sequence (that gives the error) is more useful than the second.

julia> v = [(2,5), (3,9), (12,15)]
3-element Array{Tuple{Int64,Int64},1}:
 (2, 5)
 (3, 9)
 (12, 15)

julia> searchsortedfirst(v, 7, by = a-> a[2])
ERROR: BoundsError
Stacktrace:
 [1] getindex at .\number.jl:78 [inlined]
 [2] #5 at .\REPL[4]:1 [inlined]
 [3] lt at .\ordering.jl:51 [inlined]
 [4] searchsortedfirst at .\sort.jl:178 [inlined]
 [5] searchsortedfirst at .\sort.jl:291 [inlined]
 [6] #searchsortedfirst#4 at .\sort.jl:293 [inlined]
 [7] (::getfield(Base.Sort, Symbol("#kw##searchsortedfirst")))(::NamedTuple{(:by,),Tuple{getfield(Main, Symbol("##5#6"))}}, ::typeof(searchsortedfirst), ::Array{Tuple{Int64,Int64},1}, ::Int64) at .\none:0
 [8] top-level scope at REPL[4]:1

julia> searchsortedfirst(v, (99999,7), by = a-> a[2])
2
@JeffBezanson
Copy link
Member

Duplicate of #9429. I do think this would be useful. It would be nice to add it as a new function or keyword argument of some sort.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants