Using the end keyword as a function argument

Hi,

Is there any way to pass the end keyword as an argument in a function or another index (i.e., -1 in python) to point to the last position of an array?

a = [1 2 3 4 5]

function f1(a, n)
    return a[n]
end

f1(a, end)
f1(a, -1)

Regards,
Santiago

2 Likes

I don’t think so, but I’d approach this a little differently

a = [1 2 3 4 5]

function f1(a, n)
    return a[n]
end
function f1(a, n::function)
    return n(a)
end

f1(a, last)
# 5
f1(a, 2)
# 2
1 Like

just f1(a, lastindex(a))

2 Likes

This package has values ibegin and iend that you can pass around.

2 Likes