Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between filter(_:).first and first(where:)?

Please consider the following strings array:

let strings = ["str1", "str2", "str10", "str20"]

Let's assume that what required is to get the first element (String) which contains 5 characters, I could get it by using filter(_:) as follows:

let filterString = strings.filter { $0.count == 5 }.first
print(filterString!) // str10

but after reviewing the first(where:) method, I recognized that I will be able to get the same output:

let firstWhereString = strings.first(where: { $0.count == 5 })
print(firstWhereString!) // str10

So what is the benefit of using one instead of the other? is it only about that the filter(_:) returns a sequence and the first(where:) returns a single element?

Update:

I noticed that the filter(_:) took 5 times to do such a process, while first(where:) took 4 times:

enter image description here

like image 838
Ahmad F Avatar asked Oct 28 '25 08:10

Ahmad F


2 Answers

You are correct in observing that filter(_:) returns all elements that satisfy a predicate and that first(where:) returns the first element that satisfy a predicate.

So, that leaves us with the more interesting question of what the difference is between elements.filter(predicate).first and elements.first(where: predicate).

As you've already noticed they both end up with the same result. The difference is in their "evaluation strategy". Calling:

elements.filter(predicate).first

will "eagerly" check the predicate against all elements to filter the full list of elements, and then pick the first element from the filterer list. By comparison, calling:

elements.first(where: predicate)

will "lazily" check the predicate against the elements until it finds one that satisfies the predicate, and then return that element.

As a third alternative, you can explicitly use "a view onto [the list] that provides lazy implementations of normally eager operations, such as map and filter":

elements.lazy.filter(predicate).first

This changes the evaluation strategy to be "lazy". In fact, it's so lazy that just calling elements.lazy.filter(predicate) won't check the predicate against any elements. Only when the first element is "eagerly" evaluated on this lazy view will it evaluate enough elements to return one result.


Separately from any technical differences between these alternatives, I'd say that you should use the one that most clearly describes your intentions. If you're looking for the first element that matches a criteria/predicate then first(where:) communicates that intent best.

like image 152
David Rönnqvist Avatar answered Oct 31 '25 00:10

David Rönnqvist


I believe we should start from considering each method separately and their purpose.

filter(_:) is not purposefully designed to prepare us to obtain first element. It is about filtering and that's it. It merely returns us a sequence. first can be used after filter, but that's just an instance of usage, a possible case. first is called for a collection and if we want, of course we are free to use it directly after filter.

But first(where:) was designed to filter and return a single element and is evidently the go-to approach to accomplish that kind of a task. That said, it is easy to presume that it's also preferred from performance perspective. As mentioned, we filter entire sequence, but with first(where:) we only process the portion of a sequence until condition is met.

like image 41
Hexfire Avatar answered Oct 31 '25 00:10

Hexfire



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!