Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why lazy.compactMap.first maps 'first' element twice?

Tags:

swift

I'm testing compactMap for lazy array to find first element and map it in a few lines of code.

"abc5def".lazy
  .compactMap {
    print($0)
    return Int(String($0))
}.first as Int?

Prints

a
b
c
5
5

Why last element being mapped twice. How to avoid this behaviour?

like image 647
Dmitry Kozlov Avatar asked Jan 28 '26 01:01

Dmitry Kozlov


2 Answers

TL;DR The compactMap call returns a chain of lazy sequences LazyMapSequence<LazyFilterSequence<LazyMapSequence<..., this, combined with the fact that first needs to compute both the start index, as well as the element at that start index, results in the transform closure being called twice:

  1. when startIndex is computed
  2. when retrieving the element at the start index

This is the current implementation of compactMap over LazySequenceProtocol (a protocol that all lazy sequences conform to):

public func compactMap<ElementOfResult>(
    _ transform: @escaping (Elements.Element) -> ElementOfResult?
  ) -> LazyMapSequence<
    LazyFilterSequence<
      LazyMapSequence<Elements, ElementOfResult?>>,
    ElementOfResult
  > {
    return self.map(transform).filter { $0 != nil }.map { $0! }
}

This makes your "abc5def".lazy.compactMap { ... } to be of type LazyMapSequence<LazyFilterSequence<LazyMapSequence<String, Optional<Int>>>, Int>.

Secondly, you're asking about the first element from the lazy sequence. This resolves to the default implementation of first over the Collection protocol (all lazy sequences get automatic conformance to Collection if their base sequence is also a collection):

public var first: Element? {
    let start = startIndex
    if start != endIndex { return self[start] }
    else { return nil }
}

This means that first has to retrieve two pieces of information:

  1. the start index
  2. the value at the start index (the subscript part)

Now, it's the startIndex computation that causes the duplicate evaluation, due to this implementation over LazyFilterSequence:

public var startIndex: Index {
    var index = _base.startIndex
    while index != _base.endIndex && !_predicate(_base[index]) {
      _base.formIndex(after: &index)
    }
    return index
}

The subscript implementation over LazyMapSequence is a standard one:

public subscript(position: Base.Index) -> Element {
    return _transform(_base[position])
}

, however, as you can see, the transform is called again, resulting in the second print you see.

like image 142
Cristik Avatar answered Jan 29 '26 15:01

Cristik


A quick solution is to use the first function in place of the computed property.

Example:

"abc5def".lazy.compactMap { Int(String($0)) }.first { _ in true }
like image 31
93sauu Avatar answered Jan 29 '26 13:01

93sauu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!