# Getindex for AbstractArray with Function

**URL:** <https://discourse.julialang.org/t/getindex-for-abstractarray-with-function/79810>\
**Category:** New to Julia\
**Created:** [April 22, 2022, 6:53am UTC](https://discourse.julialang.org/t/getindex-for-abstractarray-with-function/79810 "2022-04-22T06:53:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [April 22, 2022, 6:53am UTC](https://discourse.julialang.org/t/getindex-for-abstractarray-with-function/79810/1 "2022-04-22T06:53:05Z")

</div>

There is a AbstractArray:

```julia
m=[[1,2,3] [2,3,4]]

"""
3×2 Matrix{Int64}:
 1 2
 2 3
 3 4

"""

```

How can I dispath on `Function` to `Base.getindex` to have this custom method?

```julia
m[>=(2),:]

"""
1×2 Matrix{Int64}:

 2 3
 3 4

"""

m[:,>=(1)]

m[[1,2], >=(1)]

etc.....

```

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [April 22, 2022, 7:12am UTC](https://discourse.julialang.org/t/getindex-for-abstractarray-with-function/79810/2 "2022-04-22T07:12:47Z")

</div>

Hi! It seems to me that you want to do something like

```julia
m[2:end, :]

```

is that correct?

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [April 22, 2022, 7:18am UTC](https://discourse.julialang.org/t/getindex-for-abstractarray-with-function/79810/3 "2022-04-22T07:18:22Z")

</div>

yes, this can get the same result.

> [@gdalle](#):
>
> `m[2:end, :]`

But above is just a simple example, I have many more compcated situation, which have the similar style to that.

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [April 22, 2022, 7:20am UTC](https://discourse.julialang.org/t/getindex-for-abstractarray-with-function/79810/4 "2022-04-22T07:20:09Z")

</div>

Maybe you could engineer something with [`findall`](https://docs.julialang.org/en/v1/base/arrays/#Base.findall-Tuple%7BFunction,%20Any%7D) instead of overloading `getindex`?

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [April 22, 2022, 7:29am UTC](https://discourse.julialang.org/t/getindex-for-abstractarray-with-function/79810/5 "2022-04-22T07:29:48Z")

</div>

`findall` is good, but not elegent. With methods mentioned above, I can:

```julia
Base.getindex(x::M,i::F) where T=x[i.(@view(x[:,1])),:]

Base.getindex(x::V,i::F)=x[i.(x)]

Base.getindex(x::M,i::Va{Pair})=begin
    mask=trues(size(x,1))
    for (k,v) in i
        mask = mask .&& (@view(x[:,k]) .|> v)
    end
    x[mask,:]

end

m[>=(2)]

"""
2×2 Matrix{Int64}:
 2 3
 3 4

"""

m[1=>>=(2),2=>==(3)]

"""
1×2 Matrix{Int64}:
 2 3
"""

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 22, 2022, 7:36am UTC](https://discourse.julialang.org/t/getindex-for-abstractarray-with-function/79810/6 "2022-04-22T07:36:08Z")

</div>

> [@Brian1](#):
>
> ```julia
> m[>=(2),:]
> 
> """
> 1×2 Matrix{Int64}:
> 
> 2 3
> 
> """
> 
> ```

It’s hard to get a grip on what you are trying to do. For example,

```julia
julia> m[2:end, :]
2×2 Matrix{Int64}:
 2 3
 3 4

julia> m[m[:, 1].>=2, :]
2×2 Matrix{Int64}:
 2 3
 3 4

```

neither of which are the same. Can you explain what `x[>=(2), :]` is supposed to do? Why does it return `[2 3]`?

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [April 22, 2022, 7:43am UTC](https://discourse.julialang.org/t/getindex-for-abstractarray-with-function/79810/7 "2022-04-22T07:43:20Z")

</div>

Sorry for that, I made a mistake, It is supposed to

> [@DNF](#):
>
> ```julia
> julia> m[2:end, :]
> 2×2 Matrix{Int64}:
> 2 3
> 3 4
> 
> ```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 22, 2022, 8:32am UTC](https://discourse.julialang.org/t/getindex-for-abstractarray-with-function/79810/8 "2022-04-22T08:32:01Z")

</div>

So you are looking for calculations on the _indices_, not on the values when selecting rows?

Maybe something like

```julia
m[foo.(axes(m, 1)), :]

```

for a function `foo` that returns a `Bool`. If you are working on the values, not the indices, then

```julia
m[foo.(m[:, 1]), :]

```

maybe with a `@view` in front.

I would _not_ recommend overloading `getindex` for `Array` and `Function`, since that would be [type piracy](https://docs.julialang.org/en/v1/manual/style-guide/#Avoid-type-piracy). Instead, you could make your own function, maybe

```julia
selectrows(f::Function, m::AbstractMatrix) = m[f.(axes(m, 1)), :]

```

and then, `selectrows(>=(2), m)`, etc.
