Thanks. That’s more compact than my custom function.
Now that makes me wonder why there’s a first(s::AbstractString, n::Integer) helper function for strings if the same can be accomplished with the technique you described.
julia> s = "foo"
"foo"
julia> s[1:min(5,end)]
"foo"
This mismatch may continue to be confusing for new users who expect first to work the same on Array as it it does on other Array-like types.
I didn’t know that first did that on strings. But note that they aren’t all that array-like, for instance first("abc′dêf",5) == "abc′dêf"[1:7] but [1:5] gives an error. Perhaps that’s why they got special commands?
Another way to write this, BTW, is f(n) = a[intersect(eachindex(a), 1:n)].
absolutely nothing.
In fact, I didn’t know at the time that the first function also had a second parameter - I learned this later in other discussions.
When I read (quickly, perhaps too much) the post I only saw that the chosen solution was:
f(n) = a[intersect(eachindex(a), 1:n)].
I came up with the one I sent and after quickly trying it out and also surprised by the fact that it just worked, I posted it straight away.
Without even seeing that the same idea had already been proposed in previous messages.