Skip to content

Files

Latest commit

 

History

History
54 lines (25 loc) · 932 Bytes

last_where.md

File metadata and controls

54 lines (25 loc) · 932 Bytes

Pattern: Use of .filter { }.last instead of .last(where:)

Issue: -

Description

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

Examples of correct code:

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


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


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


(myList.filter { $0 == 1 }.suffix(2)).last


collection.filter("stringCol = '3'").last

Examples of incorrect code:

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


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


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


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


myList.filter(someFunction).last


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


(myList.filter { $0 == 1 }).last

Further Reading