# The missing \`Sequence.first\`

**URL:** <https://forums.swift.org/t/the-missing-sequence-first/42476>\
**Category:** Discussion\
**Created:** [December 3, 2020, 8:58pm UTC](https://forums.swift.org/t/the-missing-sequence-first/42476 "2020-12-03T20:58:57Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![nixberg](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nixberg/32/7961_2.png) [@nixberg](https://forums.swift.org/u/nixberg)\
**Post date:** [December 3, 2020, 8:58pm UTC](https://forums.swift.org/t/the-missing-sequence-first/42476/1 "2020-12-03T20:58:57Z")

</div>

Quite frequently I find myself stumbling over the fact that there is no

```swift
extension Sequence {
    var first: Element? {
        var iterator = self.makeIterator()
        return iterator.next()
    }
}

```

which I’d consider a small and obvious addition to the language. Is there a particular reason for this omission?

Relatedly, [[Concurrency] AsyncSequence](https://forums.swift.org/t/concurrency-asyncsequence/42417) seeks to introduce the analogous

```swift
extension AsyncSequence {
  public func first() async rethrows -> AsyncIterator.Element?
}

```

---

<div class="post-metadata">

**Author:** ![AlexanderM](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexanderm/32/9781_2.png) [@AlexanderM](https://forums.swift.org/u/AlexanderM)\
**Post date:** [December 3, 2020, 9:05pm UTC](https://forums.swift.org/t/the-missing-sequence-first/42476/2 "2020-12-03T21:05:42Z")

</div>

I think that before this is/isn't added, we should clarify whether Sequence is expected to be repeatedly iterable or not.

A `Sequence.first` like this would consume the first entry, which might be forever gone, depending on the particular sequence's implementation.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [December 3, 2020, 9:16pm UTC](https://forums.swift.org/t/the-missing-sequence-first/42476/3 "2020-12-03T21:16:58Z")

</div>

> [@AlexanderM](#):
>
> I think that before this is/isn't added, we should clarify whether Sequence is expected to be repeatedly iterable or not.

`Sequence` does _not_ guarantee that conforming types are multi-pass; `first`, if added, would be allowed to consume the first element.

What types are you using that conform to `Sequence` but not `Collection`?

(It should also be noted that there is `Sequence.first(where:)`. You can write `first { _ in true }`. Part of the thinking reflected in this design is that it is more surprising for a _property_ to potentially consume elements of a sequence when invoked.)

---

<div class="post-metadata">

**Author:** ![nixberg](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nixberg/32/7961_2.png) [@nixberg](https://forums.swift.org/u/nixberg)\
**Post date:** [December 3, 2020, 9:47pm UTC](https://forums.swift.org/t/the-missing-sequence-first/42476/4 "2020-12-03T21:47:59Z")

</div>

> [@xwu](#):
>
> You can write `first { _ in true }` .

Which is what I end up writing usually.

> [@xwu](#):
>
> Part of the thinking reflected in this design is that it is more surprising for a _property_ to potentially consume elements of a sequence when invoked.)

Well, I’ve personally never had an issue with that. But I find it quite surprising that something as innocuous as

```swift
.compactMap { $0.cycled().dropFirst(n).first }

```

puts my laptop in leaf blower mode. The fact that code in this functional style _sometimes_ works (consuming a `Collection`; without `.cycled()`) and _sometimes_ doesn’t (consuming a `Sequence`).

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [December 3, 2020, 10:09pm UTC](https://forums.swift.org/t/the-missing-sequence-first/42476/5 "2020-12-03T22:09:00Z")

</div>

> [@xwu](#):
>
> `Sequence` does _not_ guarantee that conforming types are multi-pass; `first` , if added, would be allowed to consume the first element.

Not just the first element; the whole `Sequence`.

Once you call `.first`, any other attempt to inspect the sequence's elements is allowed to produce arbitrary results.

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [December 4, 2020, 4:17am UTC](https://forums.swift.org/t/the-missing-sequence-first/42476/6 "2020-12-04T04:17:35Z")

</div>

> [@nixberg](#):
>
> Quite frequently I find myself stumbling over the fact that there is no
> 
> ```swift
> extension Sequence {
> var first: Element? {
> var iterator = self.makeIterator()
> return iterator.next()
> }
> }
> 
> ```

My personal pet peeve is that the language won't accept:

```swift
let first = seq.makeIterator().next()

```

And generally won't compile mutating methods on ephemeral temporary values:

```swift
let target = makeTemp().mutatingMethod()

```

I don't know what's the reason why it is disallowed to create and immediately discard mutating values. It just forces to declare a temp `var` without clear benefit.

If this were allowed, this thread would not exist in the first place, answers about the fundamental nature of sequences would be spared to the OP. This is an ergonomic problem with the language, not a problem with Sequence, or the OP's goals.

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [December 4, 2020, 4:38am UTC](https://forums.swift.org/t/the-missing-sequence-first/42476/7 "2020-12-04T04:38:26Z")

</div>

> [@gwendal.roue](#):
>
> I don't know what's the reason why it is disallowed to create and immediately discard mutating values.

Consider code like

```swift
let w = foo.words.remove(at: 0)

```

You can’t tell from this call site, but `words` is read-only! If mutating methods were allowed on temporaries, this call would be accepted by the compiler, even though it’s clearly wrong.

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [December 4, 2020, 4:46am UTC](https://forums.swift.org/t/the-missing-sequence-first/42476/8 "2020-12-04T04:46:42Z")

</div>

In all scenario this problem happens, people just do introduce the missing `var`, and there lies the ergonomic problem:

```swift
var words = foo.words
let w = words.remove(at: 0)

```

I'm sure the compiler could help. I think it should.

---

<div class="post-metadata">

**Author:** ![anon9791410](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@anon9791410](https://forums.swift.org/u/anon9791410)\
**Post date:** [December 4, 2020, 4:50am UTC](https://forums.swift.org/t/the-missing-sequence-first/42476/9 "2020-12-04T04:50:26Z")

</div>

> [@gwendal.roue](#):
>
> My personal pet peeve is that the language won't accept:
> 
> ```swift
> let first = seq.makeIterator().next()
> 
> ```

Well, that's only because we have the extremely concise, and completely mutationally consistent

```swift
AnyIterator(seq.makeIterator()).next()

```

and

```swift
AnySequence(seq).makeIterator().next()

```

available to us! 😺

…🤔

(I use a `Sequence.first` extension all the time.)

---

<div class="post-metadata">

**Author:** ![nixberg](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nixberg/32/7961_2.png) [@nixberg](https://forums.swift.org/u/nixberg)\
**Post date:** [December 9, 2020, 1:27pm UTC](https://forums.swift.org/t/the-missing-sequence-first/42476/10 "2020-12-09T13:27:17Z")

</div>

More confusing behavior caused by the absence of `Sequence.first`:

Obviously slow:

```swift
(0...10_000_000).map { $0 }.first

```

Fast, as one would expect:

```swift
(0...10_000_000).lazy.map { $0 }.first
(0...10_000_000).lazy.map { $0 }.first { _ in true }

```

Expected infinite loop:

```swift
(1...).map { $0 }

```

Unexpectedly:

```swift
(1...).lazy.map { $0 }.first // (Function)
(1...).lazy.map { $0 }.first! // Infinite loop!

```

(Which, of course, can be fixed by defining your own `Sequence.first`.)

Not an infinite loop, as one would expect:

```swift
(1...).lazy.map { $0 }.first { _ in true }

```

---

<div class="post-metadata">

**Author:** ![benrimmington](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/benrimmington/32/695_2.png) [@benrimmington](https://forums.swift.org/u/benrimmington)\
**Post date:** [December 9, 2020, 4:12pm UTC](https://forums.swift.org/t/the-missing-sequence-first/42476/11 "2020-12-09T16:12:10Z")

</div>

> [@nixberg](#):
>
> Unexpectedly:
> 
> ```swift
> (1...).lazy.map { $0 }.first // (Function)
> (1...).lazy.map { $0 }.first! // Infinite loop!
> 
> ```
> 
> (Which, of course, can be fixed by defining your own `Sequence.first`.)

This part could also be "fixed" by requiring `.first(where:)` instead of `.first`

> [@Require parameter names when referencing to functions](https://forums.swift.org/t/require-parameter-names-when-referencing-to-functions/27048):
>
> Hi everyone, Some of you may remember [SE-0220](https://github.com/apple/swift-evolution/blob/master/proposals/0220-count-where.md), my proposal that added the count(where:) function to sequences. This proposal was ultimately accepted in time for Swift 5, but sadly [had to be reverted](https://github.com/apple/swift/pull/22289) because it was causing issues with the type checker. The issue was that when you reference count, in an expression like myArray.count \* 5, you could be referring to the count property, with type Int, or the count(where:) function, which has type ((Element) -\> Bool) -\> Int, which you can refer to in…

---

<div class="post-metadata">

**Author:** ![nixberg](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nixberg/32/7961_2.png) [@nixberg](https://forums.swift.org/u/nixberg)\
**Post date:** [December 9, 2020, 4:23pm UTC](https://forums.swift.org/t/the-missing-sequence-first/42476/12 "2020-12-09T16:23:35Z")

</div>

> [@benrimmington](#):
>
> This part could also be "fixed" by requiring `.first(where:)` instead of `.first`

The first line yes, but not the infinite loop.
