<p dir="auto">I would like to do a binary search without having to materialize the array:<br>
I would prefer not to build the array, because my function is not i-&gt;i^2 but expensive to calculate.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="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-&gt;i^2) # does not what I had expected"><pre class="notranslate"><code class="notranslate">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-&gt;i^2) # does not what I had expected
</code></pre></div>
<p dir="auto">It looks like an implementation error, that the <code class="notranslate">by=transform</code> is applied to the value to compare with.<br>
There are at least 2 arguments against the current implementation:</p>
<ol dir="auto">
<li>It forces the user to pre-calculate an abstract vector with the transformed values</li>
<li>the transform of the comparison value is performed as many times as comparisons are made in the algorithm</li>
<li>the documentation does not suggest, that the comparison value is treated this way</li>
</ol>
<p dir="auto">Consider this:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="using Base.Dates: Second

immutable tsp # time series point
    t :: DateTime
    y :: Float64
end

t0 = now()
S = [tsp(t0+Second(10), 42), tsp(t0+Second(13), -1), tsp(t0+Second(17), 31)]

searchsortedfirst(S, t0+Second(12), by=x -&gt; x.t)
searchsortedfirst(S, tsp(t0+Second(12), NaN), by=x -&gt; x.t)"><pre class="notranslate"><code class="notranslate">using Base.Dates: Second

immutable tsp # time series point
    t :: DateTime
    y :: Float64
end

t0 = now()
S = [tsp(t0+Second(10), 42), tsp(t0+Second(13), -1), tsp(t0+Second(17), 31)]

searchsortedfirst(S, t0+Second(12), by=x -&gt; x.t)
searchsortedfirst(S, tsp(t0+Second(12), NaN), by=x -&gt; x.t)
</code></pre></div>
<p dir="auto">The first search call fails and the second is not ideal because one has to use a dummy value (NaN here) to fill in the parts of the item being searched that are not part of the key. I think that in <code class="notranslate">searchsortedfirst(S, x, by=f)</code> it would make more sense to apply f() to the items of <em>S</em> but not to the item <em>x</em>. If the searchsorted functions worked like that, then the current behavior is easily obtained by using <code class="notranslate">searchsortedfirst(S, f(x), by=f)</code>. Inversion of f() is not necessarily as easy.</p>