Skip to content

serachsortedfirst, searchsortedlast, searchsorted unexpected behavior #35381

@KlausC

Description

@KlausC

I would like to do a binary search without having to materialize the array:
I would prefer not to build the array, because my function is not i->i^2 but expensive to calculate.

searchsortedfirst([i^2 for i in 1:10], 25) # is working
searchsortedfirst((i^2 for i in 1:10), 25) # no method
searchsortedfirst(1:10, 25, by=i->i^2) # does not what I had expected

It looks like an implementation error, that the by=transform is applied to the value to compare with.
There are at least 2 arguments against the current implementation:

  1. It forces the user to pre-calculate an abstract vector with the transformed values
  2. the transform of the comparison value is performed as many times as comparisons are made in the algorithm
  3. the documentation does not suggest, that the comparison value is treated this way

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions