Tying a function to an array when I get the value of the array

Hi everyone, I have question. Maybe someone has encountered this before and can help me out.

I would like to do the following:
searchsortedfirst(ntoh.(v), x)
Assuming v is a memory mapped vector on disk.

Using ntoh.() would load the whole vector and perform the bitflip and then pass it to searchsortedfirst

Is there a way to broadcast a function on an array so that it is executed only if an element of said array needs to be accessed?I want ntoh(v[k]) to be performed when I ask to get v[k] , only then

This should apply the function lazily, only when needed:

using FlexiMaps

searchsorted(mapview(ntoh, v), x)

mapview(f, A) is like regular map(f, A), but presents a view of the original array instead of materializing the result.


I also found GitHub - JuliaArrays/MappedArrays.jl: Lazy in-place transformations of arrays

Does searchsorted(mappedarray(ntoh, v), x) do anythin different as far as you know (meaning is it at the same cost)?

I think for simple cases (arrays, read-only view) all these implementations should behave the same. As yet another alternative, there’s mapview in SplitApplyCombine.jl.

I believe that all you need for this specific case is searshsortedfirst(v, hton(x); by=ntoh). In more general cases, you can try some of the lazy evaluation techniques proposed by others.

It was necessary to apply hton(x) since by applies to both the search value and the target array. See the docs for sort! and Base.Order.ord for more on sorting options.