Questions tagged [predicate]

A Predicate is a method which represents a set of criteria and decides for a given object if these criteria are fulfilled or not. In computer languages a Predicate is expressed as a function which takes a single object as input parameter and returns a boolean value.

A Predicate is a method which represents a set of criteria and decides for a given object if these criteria are fulfilled or not. In computer languages a Predicate is expressed as a function which takes a single object as input parameter and returns a boolean value.

Example of a Predicate in .NET Framework / C#

In the .NET Framework the most general form of a Predicate is represented by a generic delegate:

public delegate bool Predicate<in T>(T obj)

An often used alternative representation is:

public delegate TResult Func<in T, out TResult>(T arg)

where TResult is of type bool.

An example of a concrete instance of such a delegate is a method which decides for a given Product by a special citerion if it is expensive or not:

public bool IsExpensive(Product product)
{
    return product.UnitPrice > 1000;
}

Predicates are often used for defining criteria to filter a subset of data out of a larger set and are often expressed as anonymous methods or by lambda expressions:

IEnumerable<Product> expensiveProducts =
    allProducts.Where(p => p.UnitPrice > 1000);
1908 questions
59
votes
5 answers

List.RemoveAll - How to create an appropriate Predicate
This is a bit of noob question - I'm still fairly new to C# and generics and completely new to predicates, delegates and lambda expressions... I have a class 'Enquiries' which contains a generic list of another class called 'Vehicles'. I'm building…
CJM
  • 11,908
  • 20
  • 77
  • 115
59
votes
3 answers

How to convert an Expression> to a Predicate

I have a method that accepts an Expression> as a parameter. I would like to use it as a predicate in the List.Find() method, but I can't seem to convert it to a Predicate which List takes. Do you know a simple way to do this? public…
Lance Fisher
  • 25,684
  • 22
  • 96
  • 122
58
votes
4 answers

How to apply multiple predicates to a java.util.Stream?

How can I apply multiple predicates to a java.util.Stream's filter() method? This is what I do now, but I don't really like it. I have a Collection of things, and I need to reduce the number of things based on the Collection of filters…
Paweł Dyda
  • 18,366
  • 7
  • 57
  • 79
54
votes
4 answers

Why do C++ classes without member variables occupy space?

I found that both MSVC and GCC compilers allocate at least one byte per each class instance even if the class is a predicate with no member variables (or with just static member variables). The following code illustrates the point. #include…
bkxp
  • 1,115
  • 1
  • 12
  • 20
53
votes
1 answer

Java interface like Predicate, but without an argument

I'm looking for a preexisting functional interface like Predicate, but one whose test method takes no arguments.
Greg Valvo
  • 1,069
  • 1
  • 9
  • 13
50
votes
4 answers

What is predicate in C++?

Can you give some example or a link to a topic.
munish
  • 4,505
  • 14
  • 53
  • 83
50
votes
4 answers

Xpath expression with multiple predicates

I am trying to build a complex xpath expression which will answer the following condition. From the XML data below, returns the User entity which: His loginname is "user1" His name is "User 1" He has 2 different profiles values which are "operator"…
user41767
  • 1,217
  • 1
  • 17
  • 26
47
votes
2 answers

C++ remove_if on a vector of objects

I have a vector (order is important) of objects (lets call them myobj class) where I'm trying to delete multiple objects at a time. class vectorList { vector<*myobj> myList; }; class myobj { char* myName; int index; bool…
Jordan
  • 9,014
  • 8
  • 37
  • 47
47
votes
1 answer

OData $filter with multiple predicates

If I have two entities in my model, "People" and "Addresses", and a particular Person has zero or more addresses, accessed via an AddressList navigation property, can I write an OData query that answers the following question: "Which people have a…
Ben Vitale
  • 1,754
  • 3
  • 17
  • 27
47
votes
1 answer

Checking that all items in a collection match a predicate in Scala

What's the most idiomatic way to test whether all items of a collection match a predicate? Any item?
ripper234
  • 222,824
  • 274
  • 634
  • 905
46
votes
3 answers

Core Data: Query objectIDs in a predicate?

I am fetching a set of objects from a Core Data persistent store using a fetch request and a predicate. My current predicate simply checks whether an attribute is >= a certain value. This all works great, except that I want to finally exclude any…
Michael Waterfall
  • 20,497
  • 27
  • 111
  • 168
44
votes
7 answers

Combine Multiple Predicates

Is there any way in c# .NET 2.0! to combine multiple Predicates? Let's say I have the following code. List names = new…
eric
  • 1,857
  • 5
  • 23
  • 33
42
votes
7 answers

lisp filter out results from list not matching predicate

I am trying to learn lisp, using emacs dialect and I have a question. let us say list has some members, for which predicate evaluates to false. how do I create a new list without those members? something like { A in L: p(A) is true }. in python…
Anycorn
  • 50,217
  • 42
  • 167
  • 261
41
votes
4 answers

Why doesn't Java 8's Predicate extend Function

If I wrote the Predicate interface, I'd want to encode in the interface the fact that it's just a function that returns a primitive boolean, like this: @FunctionalInterface public interface Predicate extends Function { boolean…
mkadunc
  • 696
  • 6
  • 12
38
votes
4 answers

Am I using the copy_if wrong?

I am using visual studio 2010 and I am trying to use std::copy_if, I want to copy all values that are satisfying a predicate. For example: struct comp { bool operator()(const int i) { return i == 5 || i == 7; } }; int main() { array
Merni
  • 2,842
  • 5
  • 36
  • 42