# Why doesn't first(Array, Int64) work?

**URL:** <https://discourse.julialang.org/t/why-doesnt-first-array-int64-work/49355>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 31, 2020, 6:12am UTC](https://discourse.julialang.org/t/why-doesnt-first-array-int64-work/49355 "2020-10-31T06:12:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [October 31, 2020, 6:12am UTC](https://discourse.julialang.org/t/why-doesnt-first-array-int64-work/49355/1 "2020-10-31T06:12:33Z")

</div>

Why don’t these do the same thing?

```julia
julia> [1,2,3][1:2]
2-element Array{Int64,1}:
 1
 2

julia> first([1,2,3], 2)
ERROR: MethodError: no method matching first(::Array{Int64,1}, ::Int64)
Closest candidates are:
  first(::AbstractArray) at abstractarray.jl:270
  first(::AbstractString, ::Integer) at strings/basic.jl:606
  first(::Any) at abstractarray.jl:288
Stacktrace:
 [1] top-level scope at none:0

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 31, 2020, 7:59am UTC](https://discourse.julialang.org/t/why-doesnt-first-array-int64-work/49355/2 "2020-10-31T07:59:04Z")

</div>

Because there is no such function. Maybe you want `Iterators.take`.

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [October 31, 2020, 8:14am UTC](https://discourse.julialang.org/t/why-doesnt-first-array-int64-work/49355/3 "2020-10-31T08:14:03Z")

</div>

I was surprised to find that function didn’t exist because

```julia
julia> first([1,2,3])
1

```

does exist. I’m curious if there is a reason why it shouldn’t exist?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 31, 2020, 8:16am UTC](https://discourse.julialang.org/t/why-doesnt-first-array-int64-work/49355/4 "2020-10-31T08:16:17Z")

</div>

See my reply to your other, related question, you can do:

```julia
julia> getindex([1,2,3], 1:2)
2-element Array{Int64,1}:
 1
 2

```

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [October 31, 2020, 8:44am UTC](https://discourse.julialang.org/t/why-doesnt-first-array-int64-work/49355/5 "2020-10-31T08:44:51Z")

</div>

This is actually defined on master, so it will be in the upcoming Julia 1.6. See [https://github.com/JuliaLang/julia/pull/34868](https://github.com/JuliaLang/julia/pull/34868) for reference.

---

<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:** [October 31, 2020, 9:24am UTC](https://discourse.julialang.org/t/why-doesnt-first-array-int64-work/49355/6 "2020-10-31T09:24:15Z")

</div>

> [@nilshg](#):
>
> `getindex([1,2,3], 1:2)`

This would only work for arrays with one-based indexing, and anyway has no advantage over `[1,2,3][1,2]`. I believe the point of `first` is exactly to work generically for all indexing types and iterables.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 31, 2020, 10:08am UTC](https://discourse.julialang.org/t/why-doesnt-first-array-int64-work/49355/7 "2020-10-31T10:08:26Z")

</div>

Agreed - I guess I fused this question with another of OP’s question that he asked around the same time, which is why you can’t do `[[1,2,3], [2,3,4]].[1:2]` - that’s where `getindex` comes in handy.
