Esta página ainda não foi traduzida, por isso é exibida em inglês. Ajude a traduzir

negate

Returns a predicate that's the logical negation of the one you pass in.

bool Function(T) negate<T>(bool Function(T) f)

Lecture

negate(f) gives you back a new predicate that returns !f(x) for any x. It's most useful when you already have a named predicate (isEven, isEmpty, a matches(pattern) result) and want its opposite as a function value — for filter, reject, or anywhere else a predicate is expected — without redefining the logic.

Don't confuse it with not: not flips a single bool value, negate flips a whole predicate function. negate(f) is roughly (x) => not(f(x)).

Demo 1 · Basics

Demo 2 · Negating a library predicate

negate composes with any predicate already in the library, like the value-based isEmpty:

Try it yourself

Exercise: use negate(isVowel) to keep only the consonants.

Related: not — negate a single boolean value · filter / reject — the usual home for a negated predicate · isEmpty — a value-based predicate to combine with negate · when — apply a callback only when a predicate holds