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

**URL:** https://discourse.julialang.org/t/tying-a-function-to-an-array-when-i-get-the-value-of-the-array/94354
**Category:** Performance
**Tags:** question, broadcast, broadcasting
**Created:** [February 9, 2023, 4:02pm UTC](https://discourse.julialang.org/t/tying-a-function-to-an-array-when-i-get-the-value-of-the-array/94354 "2023-02-09T16:02:25Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![LolianSh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/loliansh/32/10834_2.png) [@LolianSh](https://discourse.julialang.org/u/LolianSh)
#### Post date: [February 9, 2023, 4:02pm UTC](https://discourse.julialang.org/t/tying-a-function-to-an-array-when-i-get-the-value-of-the-array/94354/1 "2023-02-09T16:02:25Z")

</div>

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

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [February 9, 2023, 4:20pm UTC](https://discourse.julialang.org/t/tying-a-function-to-an-array-when-i-get-the-value-of-the-array/94354/2 "2023-02-09T16:20:49Z")

</div>

This should apply the function lazily, only when needed:

```julia
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.

---

<div class="post-metadata">

### Author: ![LolianSh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/loliansh/32/10834_2.png) [@LolianSh](https://discourse.julialang.org/u/LolianSh)
#### Post date: [February 9, 2023, 4:28pm UTC](https://discourse.julialang.org/t/tying-a-function-to-an-array-when-i-get-the-value-of-the-array/94354/3 "2023-02-09T16:28:55Z")

</div>

Thanks!

I also found [GitHub - JuliaArrays/MappedArrays.jl: Lazy in-place transformations of arrays](https://github.com/JuliaArrays/MappedArrays.jl)

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

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [February 9, 2023, 5:28pm UTC](https://discourse.julialang.org/t/tying-a-function-to-an-array-when-i-get-the-value-of-the-array/94354/4 "2023-02-09T17:28:54Z")

</div>

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`.

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [February 9, 2023, 6:00pm UTC](https://discourse.julialang.org/t/tying-a-function-to-an-array-when-i-get-the-value-of-the-array/94354/5 "2023-02-09T18:00:28Z")

</div>

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!`](https://docs.julialang.org/en/v1/base/sort/#Base.sort!) and [`Base.Order.ord`](https://docs.julialang.org/en/v1/base/sort/#Base.Order.ord) for more on sorting options.
