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
12
votes
3 answers

Spring Data + QueryDSL empty predicate + Predicate chaining

let me get straight to the point. I am using Spring Data JPA with QueryDSL in a project and I cannot figure out this myself. I have the QueryDSL predicates in static methods that can take arguments and if the argument is not correct it should return…
user1622058
  • 453
  • 3
  • 7
  • 16
12
votes
1 answer

NSInvalidArgumentException', reason: 'Unknown predicate type for predicate: BLOCKPREDICATE(0x70ad750)' Error

I have a core data database and I am trying to create a fetch request using a block predicate, but I get an Unknown Predicate error: NOTE: employeeToHouse is a property of type House that was created for me when I subclassed…
Ilya Lapan
  • 1,103
  • 2
  • 12
  • 31
11
votes
6 answers

Using Java Predicate and Lambda

Why does the below code return Predicate and not boolean? My understanding is that the !s.isEmpty() check here is going against the Predicate boolean test(T t); The return type here is boolean. So in my lambda should my…
Taobitz
  • 546
  • 3
  • 10
  • 22
11
votes
2 answers

Predicate control in Prolog

Have a curiosity related to Prolog predicate control. Supposedly I have a predicate f(A,X) and g(B). f(A,X):- a,b,c, g(X). g(B):- true. a - returns true b - returns true. c - returns false. where a,b and c are random predicates. How can I continue…
Cristina
  • 1,991
  • 3
  • 17
  • 24
11
votes
1 answer

Can you implement any pure LISP function using the ten primitives? (ie no type predicates)

This site makes the following claim: http://hyperpolyglot.wikidot.com/lisp#ten-primitives McCarthy introduced the ten primitives of lisp in 1960. All other pure lisp functions (i.e. all functions which don't do I/O or interact with the environment)…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
11
votes
3 answers

Reason not to use a guarded/constrained collection

Is there any reasons/arguments not to implement a Java collection that restricts its members based on a predicate/constraint? Given that such functionality should be necessary often, I was expecting it to be implemented already on collections…
gcscaglia
  • 289
  • 1
  • 13
11
votes
7 answers

Shorter way to order a list by boolean functions

I have a list that needs to be ordered in a specific way. I've currently solved it like this: var files = GetFiles() .OrderByDescending(x => x.Filename.StartsWith("ProjectDescription_")) .ThenByDescending(x => x.Filename.StartsWith("Budget_")) …
Svish
  • 152,914
  • 173
  • 462
  • 620
11
votes
2 answers

Conditional where clause in JPA criteria query

I am facing an issue for JPA criteria query. How can I add multiple where clause in my Criteria Query with if else... My Requirement is: CriteriaBuilder builder = getEm().getCriteriaBuilder(); CriteriaQuery query =…
11
votes
3 answers

What is the meaning of slash after the predicate name in Prolog?

I've read the SO questions what does slash(/) do in prolog? and What is the meaning of predicate “simple/1” in Prolog (SWI-Prolog), but these links don't seem to help me. I was going though some beginner tutorials on Prolog. Phrases like Solve/4 or…
Animesh Pandey
  • 5,900
  • 13
  • 64
  • 130
11
votes
5 answers

What's the difference between a Predicate and a Functor?

I just read somebody call a class with a constructor and an operator() a predicate: // Example class Foo { public: Foo(Bar); bool operator()(Baz); private: Bar bar; }; However, I haven't heard the word predicate being used in this…
bitmask
  • 32,434
  • 14
  • 99
  • 159
10
votes
1 answer

How to pass predicate as function parameter

I have a class CMyVector which holds a vector of pointers to CMyClass objects and I have several "find" functions to find elements according to differente criteria. So for example, I have: CMyClass* CMyVector::FindByX(int X); CMyClass*…
MikMik
  • 3,426
  • 2
  • 23
  • 41
10
votes
2 answers

Pass std algos predicates by reference in C++

I am trying to remove elements from a std::list and keep some stats of deleted elements. In order to do so, I use the remove_if function from the list, and I have a predicate. I would like to use this predicate to gather statistics. Here is the code…
0x26res
  • 11,925
  • 11
  • 54
  • 108
10
votes
2 answers

Java: Combining multiple predicates

In Java, is there a short elegant way to combine multiple predicates (Guava Predicate) into one? Currently, I have some list of predicates: Collection> preds = ...; And I have some code that loops through the predicates and returns…
well actually
  • 11,810
  • 19
  • 52
  • 70
10
votes
3 answers

Is there a way to modify fetched results with a predicate after they are initialized?

I am trying to build a search view for an existing CoreData app (simple logging app). I have all the data stored with CoreData and is fetched with the @FetchRequest: @State private var searchPredicate: NSPredicate? = NSPredicate(format: "title…
Jannis
  • 175
  • 10
10
votes
2 answers

Type-safe predicate functions in TypeScript

My goal is to write predicate functions (isNull and isUndefined for example) in TypeScript which fulfill the following conditions: Can be used standalone: array.filter(isNull) Can be logically combined: array.filter(and(not(isNull),…
kayahr
  • 20,913
  • 29
  • 99
  • 147