Manuel Dossinger
11/16/2021, 2:00 PMmyList.filterNotNull().filter { f(it) }
, but that would essentially loop through the list twice, correct?Joffrey
11/16/2021, 2:01 PMList
after the filterNotNull
.Joffrey
11/16/2021, 2:03 PMfilter
but that will not make the type of the elements non-null, which is annoyingManuel Dossinger
11/16/2021, 2:03 PMfilter { it != null && predicate(it) }
but I need to cast that to List<T>, because the compiler thinks it is still a List<T?>Joffrey
11/16/2021, 2:05 PMmyList.asSequence().filterNotNull().filter { predicate(it) }.toList()
Joffrey
11/16/2021, 2:06 PMManuel Dossinger
11/16/2021, 2:09 PMManuel Dossinger
11/16/2021, 2:09 PMJoffrey
11/16/2021, 2:11 PMManuel Dossinger
11/16/2021, 2:12 PMmkrussel
11/16/2021, 2:23 PMfilter
and filterNotNull
, I don't think they are complicated.Joffrey
11/16/2021, 2:25 PMJacob
11/16/2021, 3:22 PMJoffrey
11/16/2021, 3:26 PMfilterNotNull
might also be a significant difference. It's all about context / use case.Manuel Dossinger
11/16/2021, 5:06 PMephemient
11/16/2021, 9:45 PMmyList.mapNotNull { if (it != null && f(it)) it else null }
a little contrivance results in a List<T>
in a single pass, thanks to smart castingmkrussel
11/16/2021, 9:49 PMmyList.mapNotNull { it?.takeIf(f) }
mkrussel
11/16/2021, 9:50 PMephemient
11/16/2021, 9:52 PMf
or ::f
depending on context, but yes, that's equivalent