# Return the minimum size of an array needed for a function to work

**URL:** <https://discourse.julialang.org/t/return-the-minimum-size-of-an-array-needed-for-a-function-to-work/97591>\
**Category:** General Usage\
**Tags:** array, function\
**Created:** [April 17, 2023, 9:13pm UTC](https://discourse.julialang.org/t/return-the-minimum-size-of-an-array-needed-for-a-function-to-work/97591 "2023-04-17T21:13:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jarroyoe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jarroyoe/32/42482_2.png) [@jarroyoe](https://discourse.julialang.org/u/jarroyoe)\
**Post date:** [April 17, 2023, 9:13pm UTC](https://discourse.julialang.org/t/return-the-minimum-size-of-an-array-needed-for-a-function-to-work/97591/1 "2023-04-17T21:13:45Z")

</div>

Hi everyone, I want to create a function that receives another function as an input and returns the size of the array this. As an example, let’s say I have a simple function that takes a vector as an input:

```julia
f(x::Vector{Number}) = x[1]+x[2]

```

and I want a function that would give

```julia
g(f) = 2

```

because f is expecting `length(x)>=2`

Is there any way to make this?

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [April 17, 2023, 9:24pm UTC](https://discourse.julialang.org/t/return-the-minimum-size-of-an-array-needed-for-a-function-to-work/97591/2 "2023-04-17T21:24:32Z")

</div>

Automatically? No. Manually? Yes.

```julia
julia> f(x::Vector{<:Number}) = x[1]+x[2]
f (generic function with 1 method)

julia> g(::typeof(f)) = 2
g (generic function with 1 method)

julia> g(f)
2

```

Note that you probably want `<:Number`, not `Vector{Number}`.

---

<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 17, 2023, 9:27pm UTC](https://discourse.julialang.org/t/return-the-minimum-size-of-an-array-needed-for-a-function-to-work/97591/3 "2023-04-17T21:27:22Z")

</div>

I guess one could also test arrays of all sizes until the function errors. But what exactly do you need this for?

---

<div class="post-metadata">

**Author:** ![jarroyoe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jarroyoe/32/42482_2.png) [@jarroyoe](https://discourse.julialang.org/u/jarroyoe)\
**Post date:** [April 17, 2023, 9:48pm UTC](https://discourse.julialang.org/t/return-the-minimum-size-of-an-array-needed-for-a-function-to-work/97591/4 "2023-04-17T21:48:50Z")

</div>

I want my users to input an user-defined function `f(x,p,q)` where the sizes of `x` and `q` are not relevant, but the size of `p` is, because it is later given to another function as an input.  
I can always ask the user to specify the size of `p` a priori and throw warnings/errors later in the code if the specified size is not correct, but I was wondering if there was a way to prevent these types of user error automatically in Julia.

---

<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 17, 2023, 10:00pm UTC](https://discourse.julialang.org/t/return-the-minimum-size-of-an-array-needed-for-a-function-to-work/97591/5 "2023-04-17T22:00:17Z")

</div>

The only thing that comes to mind is StaticArrays.jl if said arrays are small enough, but honestly I think you should go for the easy way of throwing errors

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [April 17, 2023, 10:15pm UTC](https://discourse.julialang.org/t/return-the-minimum-size-of-an-array-needed-for-a-function-to-work/97591/6 "2023-04-17T22:15:04Z")

</div>

They don’t need to be small enough. We could used a `SizedArray`. We just need to move the size into the type domain.

```julia
julia> g(f::Function) = only(methods(f)).sig.parameters[2].body.parameters[1].parameters[1]
g (generic function with 1 method)

julia> f(x::StaticVector{2}) = x[1]+x[2]
f (generic function with 1 method)

julia> g(f)
2

julia> h(x::StaticVector{3}) = x[1]+x[2]+x[3]
h (generic function with 1 method)

julia> g(h)
3

julia> x = SizedVector{2}(rand(2))
2-element SizedVector{2, Float64, Vector{Float64}} with indices SOneTo(2):
 0.5036256944197668
 0.3502881692558516

julia> f(x)
0.8539138636756184

julia> y = SizedVector{3}(rand(3))
3-element SizedVector{3, Float64, Vector{Float64}} with indices SOneTo(3):
 0.5070385956063957
 0.8204921722209075
 0.6512150701603181

julia> h(y)
1.9787458379876215

```

We could then create an accessory function that helps us call the arbitrary functions.

```julia
julia> function call_with_sized_view(foo, vec::AbstractVector)
           foo(SizedVector{g(foo)}(@view(vec[firstindex(vec):(firstindex(vec) + g(foo) -1)])))
       end
call_with_sized_view (generic function with 1 method)

julia> call_with_sized_view(f, 1:5)
3

julia> call_with_sized_view(h, 1:5)
6

```

---

<div class="post-metadata">

**Author:** ![jarroyoe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jarroyoe/32/42482_2.png) [@jarroyoe](https://discourse.julialang.org/u/jarroyoe)\
**Post date:** [April 17, 2023, 10:41pm UTC](https://discourse.julialang.org/t/return-the-minimum-size-of-an-array-needed-for-a-function-to-work/97591/7 "2023-04-17T22:41:16Z")

</div>

Thank you, that seems to do exactly what I need! It seems that the user would still need to specify the size of the vector though, right?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [April 17, 2023, 10:44pm UTC](https://discourse.julialang.org/t/return-the-minimum-size-of-an-array-needed-for-a-function-to-work/97591/8 "2023-04-17T22:44:34Z")

</div>

Yes. Otherwise, we would need to parse the abstract syntax tree.

```julia
julia> ci = @code_lowered f(x)
CodeInfo(
1 ─ %1 = Base.getindex(x, 1)
│ %2 = Base.getindex(x, 2)
│ %3 = %1 + %2
└── return %3
)

julia> ast.code[1].args[3]
1

julia> ast.code[2].args[3]
2

```
