# First n elements of array with Base.first

**URL:** <https://discourse.julialang.org/t/first-n-elements-of-array-with-base-first/28512>\
**Category:** General Usage\
**Created:** [September 7, 2019, 8:02pm UTC](https://discourse.julialang.org/t/first-n-elements-of-array-with-base-first/28512 "2019-09-07T20:02:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![milesf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milesf/32/9289_2.png) [@milesf](https://discourse.julialang.org/u/milesf)\
**Post date:** [September 7, 2019, 8:02pm UTC](https://discourse.julialang.org/t/first-n-elements-of-array-with-base-first/28512/1 "2019-09-07T20:02:00Z")

</div>

Is there a built-in way to get the first `n` elements of an array, even if `n` exceeds the length of the array?

This function demonstrates the desired behavior:

```julia
function first_n(a, n)
    n = min(n, length(a))
    a[1:n]
end

```

Just `a[1:n]` may result in a bounds error if `n > length(a)`.

Could (or should) `Base.first` be used for this?

Seems like most `Base.first({T})` methods could be extended to support `Base.first({T}, ::Integer)`.

This is already supported for `DataFrame` and `String`:

```julia
first(df::AbstractDataFrame, n::Integer)
first(s::AbstractString, n::Integer)

```

Example of missing support for `Array`/`Range` (versus `String`).

```julia
julia> s = "foo"
"foo"

julia> a = 1:3
1:3

julia> first(s)
'f': ASCII/Unicode U+0066 (category Ll: Letter, lowercase)

julia> first(s, 5)
"foo"

julia> first(a)
1

julia> first(a, 5)
ERROR: MethodError: no method matching first(::UnitRange{Int64}, ::Int64)

```

---

<div class="post-metadata">

**Author:** ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)\
**Post date:** [September 7, 2019, 8:31pm UTC](https://discourse.julialang.org/t/first-n-elements-of-array-with-base-first/28512/2 "2019-09-07T20:31:09Z")

</div>

You can use `end` anywhere inside `[…]`, so perhaps you want `a[1:min(n,end)]`?

```julia
julia> a = 1:3

julia> f(n) = a[1:min(n,end)]

julia> f.(1:5)
5-element Array{UnitRange{Int64},1}:
 1:1
 1:2
 1:3
 1:3
 1:3

```

---

<div class="post-metadata">

**Author:** ![milesf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milesf/32/9289_2.png) [@milesf](https://discourse.julialang.org/u/milesf)\
**Post date:** [September 7, 2019, 8:40pm UTC](https://discourse.julialang.org/t/first-n-elements-of-array-with-base-first/28512/3 "2019-09-07T20:40:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)\
**Post date:** [September 7, 2019, 8:46pm UTC](https://discourse.julialang.org/t/first-n-elements-of-array-with-base-first/28512/4 "2019-09-07T20:46:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [August 24, 2021, 10:00am UTC](https://discourse.julialang.org/t/first-n-elements-of-array-with-base-first/28512/5 "2021-08-24T10:00:15Z")

</div>

```julia
f(arr,n)= arr[1:min(end,n)]

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 24, 2021, 2:13pm UTC](https://discourse.julialang.org/t/first-n-elements-of-array-with-base-first/28512/6 "2021-08-24T14:13:33Z")

</div>

@rocco_sprmnt21, what would be the added value of your function compared to base: `first(arr,n)` ?

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [August 28, 2021, 4:35am UTC](https://discourse.julialang.org/t/first-n-elements-of-array-with-base-first/28512/7 "2021-08-28T04:35:02Z")

</div>

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:

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