Skip to content

[SR-12928] .first is less efficient than .first { _ in true } on LazyMapSequence #55374

Description

@swift-ci
Previous ID SR-12928
Radar None
Original Reporter bubski (JIRA User)
Type Bug

Attachment: Download

Environment

Apple Swift version 5.2.2 (swiftlang-1103.0.32.6 clang-1103.0.32.51)
Target: x86_64-apple-darwin19.4.0

macOS 10.15.4 (19E287)

Xcode 11.4.1

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee None
Priority Medium

md5: 38b32ecf66be259b2052152057060ea8

Issue Description:

_ = (1...1)
    .lazy
    .compactMap { value -> String? in
        print("lazy compactMap a \(value)")
        return "\(value)"
}.first

_ = (1...1)
    .lazy
    .compactMap { value -> String? in
        print("lazy compactMap b \(value)")
        return "\(value)"
}.first { _ in true }

_ = (1...1)
    .compactMap { value -> String? in
        print("eager compactMap \(value)")
        return "\(value)"
}.first

// output:
// lazy compactMap a 1
// lazy compactMap a 1
// lazy compactMap b 1
// eager compactMap 1

Shouldn't .first be synonymous to .first { _ in true } in terms of not only result, but also effort?

Also, is lazy

_ = (1...1)
    .lazy
    .compactMap { value -> String? in
        print("lazy compactMap a \(value)")
        return "\(value)"
}.first

being less efficient than eager

_ = (1...1)
    .compactMap { value -> String? in
        print("eager compactMap \(value)")
        return "\(value)"
}.first

expected?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.standard libraryArea: Standard library umbrella

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions