Easy way to negate a predicate (e.g. (String) -> Boolean)) in Kotlin
Clash Royale CLAN TAG#URR8PPP
Easy way to negate a predicate (e.g. (String) -> Boolean)) in Kotlin
Given a predicate (String) -> Boolean
I wondered whether there is an easy way to negate the outcome of that predicate.
As long as I use a list, I can simply switch from filter
to filterNot
, but what if I have, lets say... a Map
and use filterKeys
?
(String) -> Boolean
filter
filterNot
Map
filterKeys
What I used so far is:
val myPredicate : (String) -> Boolean = TODO()
val map : Map<String, String> = TODO()
map.filterKeys !myPredicate(it)
But I wonder why there is an overloaded function for List
, but not for Map
. Moreover I also wonder, why there isn't something similar to what we have in Java, i.e. Predicate.negate()
and since Java 11 Predicate.not(..)
.
List
Map
Predicate.negate()
Predicate.not(..)
Or does it exist and I just haven't found it?
filterNot
Map
ok... I specifically meant
filterKeys
and filterValues
but also any other function accepting a ~predicate.. Do there exist any easy convenience methods as the ones shown in the question? If not would be interesting why...– Roland
yesterday
filterKeys
filterValues
Actually this is a really interesting question. It might be a performance issue (or I was just not able to figure it out): stackoverflow.com/q/52057967/3755692
– msrd0
yesterday
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
There is
filterNot
forMap
as well: kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/…– msrd0
yesterday