Easy way to negate a predicate (e.g. (String) -> Boolean)) in Kotlin

The name of the pictureThe name of the pictureThe name of the pictureClash 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?





There is filterNot for Map as well: kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/…
– msrd0
yesterday


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.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard