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

How to add a list of Predicates to CriteriaBuilder.or

I have a List to append in an or condition The Issue I am facing is when I am iterating over the List and adding it to the CategoryBuilder then it takes the last Predicate Following is the example: public static Specification
Kalyan Pradhan
  • 1,415
  • 3
  • 19
  • 34
10
votes
2 answers

How to combine all predicates from List>

I have one problem and I'm sure you help me with my wrinkle. I have List> predicates and I want to use these predicates in taskFxList.stream().filter(predicates).collect(Collectors.toList()); as a one predicate merged like:…
Matley
  • 1,953
  • 4
  • 35
  • 73
10
votes
0 answers

Share extension only with specific URL

Is there a way to filter Share extension to show only when, for example, the URL domain is a specific one? For example, I want to show my app extension only when the user is sharing a google link: http://www.google.com/?someQuery If I'm filtering…
FormigaNinja
  • 1,571
  • 1
  • 24
  • 36
10
votes
2 answers

Java 8 - How to use predicate that has a function with parameter?

I have the following code: public boolean isImageSrcExists(String imageSrc) { int resultsNum = 0; List blogImagesList = driver.findElements(blogImageLocator); for (WebElement thisImage : blogImagesList) { if…
Nimrod
  • 147
  • 1
  • 1
  • 6
10
votes
6 answers

Chaining of ordering predicates (e.g. for std::sort)

You can pass a function pointer, function object (or boost lambda) to std::sort to define a strict weak ordering of the elements of the container you want sorted. However, sometimes (enough that I've hit this several times), you want to be able to…
philsquared
  • 22,403
  • 12
  • 69
  • 98
10
votes
3 answers

Prolog: How to tell if a predicate is deterministic or not

So from what I understand about deterministic predicates: Deterministic predicate = 1 solution Non-deterministic predicate = multiple solutions Are there any type of rules as to how you can detect if the predicate is one or the other? Like looking…
John Smith
  • 181
  • 1
  • 7
10
votes
1 answer

compound predicate in JPA Criteria Query - Use both and, or methods

Requirement: There are some initial conditions.lets say P1 is the predicate. There is a date field in database table which could be null. If it is null, I can proceed with initial predicate itself. If it is not null, the value of that field has to…
tortoise
  • 613
  • 1
  • 7
  • 19
10
votes
3 answers

What am I missing in this chain of predicates?

NOTE: Right before posting this question it occurred to me there's a better way of doing what I was trying to accomplish (and I feel pretty stupid about it): IEnumerable checkedItems = ProductTypesList.CheckedItems.Cast(); filter = p…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
10
votes
3 answers

Finding elements in a scala list and also know which predicate has been satisfied

I have the following problem in scala. I have to find the first element in al list which satisfies a predicate function with two conditions in OR. The problem is that I would like to get the element but also know which of the two conditions has been…
Filippo Tabusso
  • 601
  • 3
  • 10
  • 16
9
votes
6 answers

Pointfree function combination in Python

I have some predicates, e.g.: is_divisible_by_13 = lambda i: i % 13 == 0 is_palindrome = lambda x: str(x) == str(x)[::-1] and want to logically combine them as in: filter(lambda x: is_divisible_by_13(x) and is_palindrome(x), range(1000,10000)) The…
Frank S. Thomas
  • 4,725
  • 2
  • 28
  • 47
9
votes
4 answers

In Java, can I make a predicate that applies a filter on more than one object?

I have a predicate that I use to filter a list of the same Entity Object: Predicate companyFilter = i -> i.getCompany().equals(company); I also have to apply the same filter, with the exact same condition on the exact same field, on a list…
R.S.
  • 367
  • 2
  • 14
9
votes
3 answers

Predicates Common Lisp

Do you know where can I get the source of a predicate in Common Lisp? (The content of the predicate, if you prefer.) For example, source code of setq, string= etc. Thanks !
lilawood
  • 333
  • 2
  • 4
  • 10
9
votes
5 answers

Find whether a tree is a binary search tree in Haskell

type BSTree a = BinaryTree a data BinaryTree a = Null | Node (BinaryTree a) a (BinaryTree a) deriving Show flattenTree :: BinaryTree a -> [a] flattenTree tree = case tree of Null -> [] Node left val right…
Jayyyyyy
  • 197
  • 1
  • 10
9
votes
3 answers

Create custom Predicate with Set and String as parameter

I have a String as "ishant" and a Set as ["Ishant", "Gaurav", "sdnj"] . I need to write the Predicate for this. I have tried as below code, but it is not working Predicate,String> checkIfCurrencyPresent = (currencyList,currency)…
Ishant Gaurav
  • 1,183
  • 2
  • 13
  • 32
9
votes
3 answers

Predicate from Function Reference (of boolean type)

I need to compose a stream operation with a predicate based on a boolean function. Found a workaround via rethrowing a method's argument as a predicate, as shown: public Predicate pred(final Predicate aLambda) { return…
Tomas F.
  • 610
  • 1
  • 6
  • 13