Skip to content

Files

Latest commit

 

History

History
45 lines (22 loc) · 797 Bytes

first_where.md

File metadata and controls

45 lines (22 loc) · 797 Bytes

Pattern: Missing use of .first(where:)

Issue: -

Description

Prefer using .first(where:) over .filter { }.first in collections.

Examples of correct code:

kinds.filter(excludingKinds.contains).isEmpty && kinds.first == .identifier


myList.first(where: { $0 % 2 == 0 })


match(pattern: pattern).filter { $0.first == .identifier }

Examples of incorrect code:

myList.filter { $0 % 2 == 0 }.first


myList.filter({ $0 % 2 == 0 }).first


myList.map { $0 + 1 }.filter({ $0 % 2 == 0 }).first


myList.map { $0 + 1 }.filter({ $0 % 2 == 0 }).first?.something()


myList.filter(someFunction).first


myList.filter({ $0 % 2 == 0 })
.first

Further Reading