Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Predicate<Bool>, pointless or useful?

I'm trying to get my head around the Predicate<T> type and I can understand it when T is anything, except for bool. If you have Predicate<bool>, I don't see how that can be used.

Can someone tell me if this is a silly thing to do or if it actually serves a purpose?

Predicate<T> already returns a bool, so then testing a condition on a bool seems a bit pointless... or have I got it wrong?

like image 209
Tony The Lion Avatar asked Jun 08 '26 18:06

Tony The Lion


1 Answers

Predicate means a function that takes a T (any type) and returns a bool.

Predicate<int> isEven = i => i % 2 == 0;

You're mostly right. For bool, there's not many uses that come to mind. I mean, there's only so much you can do with a bool.

Predicate<bool> isFalse = input => !input;
Predicate<bool> isTrue = input => input;

You might use it like this:

var listOfBools = new List<Bool>() { true, false, false, true, true };
var trues = listOfBools.FindAll(isTrue);
var falses = listOfBools.FindAll(isFalse);

It's certainly less useful, simply because there's only so many things you can do with a bool.

like image 188
Judah Gabriel Himango Avatar answered Jun 11 '26 21:06

Judah Gabriel Himango



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!