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
16
votes
2 answers

PredicateBuilder.New vs PredicateBuilder.True

I am using PredicateBuilder to create a search/filter section in my action. Here it is: [HttpPost] public ActionResult Test(int? cty, string inumber, int? page) { var lstValues = db.TableName.Include(x => x.Table1) …
Grizzly
  • 5,873
  • 8
  • 56
  • 109
16
votes
4 answers

Java 8 - Filter with BiPredicate

I have a Stream of Integer and I would like to find the two numbers whose sum is equals to another number. So I came up with the following solution: BiPredicate p = (price1, price2) -> price1.intValue() + price2.intValue() ==…
user2919910
  • 575
  • 1
  • 7
  • 17
16
votes
2 answers

Problems understanding lower bounds when used with lambda and Functional Interface

While studying up on Java8 Streams, I came across the following code snippet: Predicate predicate = s -> s.startsWith("g"); Since the generic parameter is a lower bound, I figured this would not compile. The way I see it, if an…
piper1970
  • 518
  • 1
  • 3
  • 14
16
votes
5 answers

Core Data Predicate Date Comparison

Im trying to fetch all the objects in an entity matching a user selectedDate (it's an NSDate). The Core Data code is fine but my predicate keeps returning 0 results, the dates are the same in the database as the user is selecting. How should the…
user2228755
  • 397
  • 1
  • 2
  • 10
16
votes
4 answers

How can I implement the unification algorithm in a language like Java or C#?

I'm working through my AI textbook I got and I've come to the last homework problem for my section: "Implement the Unification Algorithm outlined on page 69 in any language of your choice." On page 69, you have the following pseudo-code for the…
Mithrax
  • 7,603
  • 18
  • 55
  • 60
15
votes
4 answers

What does it mean "Predicates should not modify their state due to a function call"?

I was reading online about C++ and came across this statement: Predicates should not modify their state due to a function call. I did not understand what does "state" mean here. Can someone please elaborate with an example?
sonu gupta
  • 473
  • 8
  • 21
15
votes
1 answer

Join on different elements using Spring JPA Specifications and Hibernate

I'm trying to build a JPA specification for a I18N implementation to be able to filter on a name. In the database I have translations for multiple languages. The thing is that most of the times only the default language is specified. So when someone…
Robin Hermans
  • 1,579
  • 1
  • 24
  • 52
14
votes
1 answer

How do I match contents of an element in XPath (lxml)?

I want to parse HTML with lxml using XPath expressions. My problem is matching for the contents of a tag: For example given the Example element I can match the href attribute using…
akosch
  • 4,326
  • 7
  • 59
  • 80
14
votes
1 answer

JPA: Predicate and expression both in QueryCriteria where clause

I have a situation where in my where clause I have single predicate and expression. And both needs to be ANDed in where clause: Expression col1 = tableEntity.get("col1"); Expression regExpr =…
user613114
  • 2,731
  • 11
  • 47
  • 73
14
votes
3 answers

What is a pure function?

I found this quote: Make predicts pure functions. Predicate purity: A predicate is a function object that returns a yes/no answer, typically as a bool value. A function is pure in the mathematical sense if its results depend only on its…
user3395918
  • 195
  • 5
13
votes
1 answer

What is the VB.NET syntax for using List.FindAll() with a lambda?

In C# I have been performing a FindAll in a generic list as follows: List tlist = list.FindAll(p => p.parid == titem.catid); Two questions, is this the appropriate way of performing such a thing and how do I convert this to VB.Net
mattgcon
  • 4,768
  • 19
  • 69
  • 117
13
votes
2 answers

Difference between std::greater() and std::greater?

This code works: #include #include #include #include using namespace std; int main(){ priority_queue,greater > pq; pq.push(1); cout<
Manohar
  • 165
  • 1
  • 1
  • 10
13
votes
7 answers

Scheme and Clojure don't have the atom type predicate - is this by design?

Common LISP and Emacs LISP have the atom type predicate. Scheme and Clojure don't have it. http://hyperpolyglot.wikidot.com/lisp Is there a design reason for this - or is it just not an essential function to include in the API?
hawkeye
  • 34,745
  • 30
  • 150
  • 304
12
votes
2 answers

IllegalStateException using CriteriaBuilder for getting a List in Java

Summarizing, I Have 3 entities, the main one is the entity named "Rac". It contains a List of "RacNatureza", which contain a attribute "Natureza". Rac @Entity @Table(name = "rac") public class Rac { @Id @GeneratedValue(strategy…
Murilo Góes de Almeida
  • 1,588
  • 5
  • 20
  • 42
12
votes
1 answer

Return first result matching predicate in a Java stream or all non-matching results

I have a Validator interface which provides a isValid(Thing) method, returning a ValidationResult which contains a boolean and a reason message. I want to create a ValidatorAggregator implementation of this interface which performs an OR across…
Rik
  • 790
  • 8
  • 11