Swift lazy collection evaluated multiple times

Originator:keithbsmiley
Number:rdar://23697280 Date Originated:30-Nov-2015 04:33 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Swift 2.1
Classification:Other Bug Reproducible:Always
 
Summary:

Sometimes when accessing elements from a lazy collection, the element is
evaluated multiples times, resulting in unexpected function calls.

Steps to reproduce:

1. Run the attached example with
  `swift LazyCollectionEvaluatedMultipleTimes.swift`

Expected results:

The output is:

```
Called with 'a' returning ''
Called with 'b' returning 'hi'
```

Where each of these entries is evaluated only a single time.

Actual results:

The output is:

```
Called with 'a' returning ''
Called with 'b' returning 'hi'
Called with 'b' returning 'hi'
```

Where `Called with 'b' returning 'hi'` is because the element is
evaluated twice.

Notes:

- This seems to be a side effect of accessing `self[index]` in the given
  example.
- This behavior might be intended, although the expensive function we
  were attempting to call lazily being called multiples times seemed
  unexpected.

Comments

https://github.com/keith/radars/tree/master/LazyCollectionEvaluatedMultipleTimes

By keithbsmiley at Dec. 1, 2015, 12:34 a.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!