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

How to use Dapper with Linq

I'm trying to convert from Entity Framework to Dapper to hopefully improve data access performance. The queries I use are in the form of predicates like so Expression>. To give an example: I have the following code which I need to…
Rian Mostert
  • 714
  • 1
  • 7
  • 19
20
votes
2 answers

Can you pass an additional parameter to a predicate?

I'm trying to filter a vector so it contains only a specific value. e.g. Make sure the vector only contains elements of the value "abc." Right now, I'm trying to achieve this with remove_copy_if. Is there any way to pass an additional parameter to a…
noko
  • 1,129
  • 2
  • 14
  • 25
19
votes
1 answer

Is there a convenience method to create a Predicate that tests if a field equals a given value?

I often find myself in the need to filter a Stream or to use a predicate that checks if a given field has a given value. Say for example I have this POJO: public class A { private Integer field; public A(final Integer field) { …
Didier L
  • 18,905
  • 10
  • 61
  • 103
19
votes
2 answers

What is the difference between a lambda expression and a predicate in .NET?

What is the difference between a lambda expression and a predicate in .NET?
Nathan Noble
  • 806
  • 7
  • 14
18
votes
5 answers

In Raku, how does one write the equivalent of Haskell's span function?

In Raku, how does one write the equivalent of Haskell's span function? In Haskell, given a predicate and a list, one can split the list into two parts: the longest prefix of elements satisfying the predicate the remainder of the list For example,…
user3134725
  • 1,003
  • 6
  • 12
18
votes
5 answers

What Javascript library can evaluate MongoDB-like query predicates against an object?

Is there a javascript library that will allow me to express object predicates in a DSL similar to MongoDB's query language? For the sake of clarity in a large program, I'd like to be able to say: var obj = { a: 1, b: 'abcdefg' }, qry = {…
RubyTuesdayDONO
  • 2,350
  • 2
  • 25
  • 37
18
votes
2 answers

using std::find with a predicate

I want to use std::find function along with a predicate (not sure if I use the correct word). Here is the code #include #include #include using namespace std; class foo { public: typedef pair< int, vector >…
mahmood
  • 23,197
  • 49
  • 147
  • 242
17
votes
5 answers

Guava - How to remove from a list, based on a predicate, keeping track of what was removed?

I have an ArrayList to be filtered, and various Guava Predicates to filter it with. This list will have only 50-100 elements. I was planning on Iterables.removeIf using each predicate in turn. It is perhaps not maximally efficient but never mind (at…
Iain
  • 1,797
  • 1
  • 20
  • 38
17
votes
5 answers

Why is Predicate not applicable to Object?

Assume we have a predicate declared as Predicate. I would naively expect it to be applicable to any superclass of SomeClass up the hierarchy, including Object. However this predicate is not applicable to Object. I get the…
lexicore
  • 42,748
  • 17
  • 132
  • 221
17
votes
3 answers

What is the difference between using a Predicate or a function as a Java stream filter?

So assuming I use some random filter on a stream, the most straightforward way is to directly input the Predicate: x.stream().filter(e -> e % 2 == 0) As well I can simply make a reference and define the Predicate in advance: Predicate
AdHominem
  • 1,204
  • 3
  • 13
  • 32
17
votes
4 answers

Why a `Predicate` doesn't match a `Func`?

I try to compile the following code in C#: public static T FirstEffective(IEnumerable list) { Predicate pred = x => x != null; return Enumerable.FirstOrDefault(list, pred); } The compiler (Mono/.NET 4.0) gives the following…
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
17
votes
3 answers

Becoming operational in Prolog quickly

My company has a project running in Prolog and I want to clarify few things about how to go about learning it. I know Prolog is different. It should not be learnt just like any other language. Having said that, and considering the fact that I did…
JPro
  • 6,292
  • 13
  • 57
  • 83
16
votes
3 answers

How to obtain index of element from predicate passed to some STL algorithm?

Say, I have vector of elements and a mask array, and I want to extract elements from vector with true corresponding mask value to separate vector. Is there a way to use std::copy_if for this purpose? The problem is, I only have value of element…
Mikhail
  • 20,685
  • 7
  • 70
  • 146
16
votes
4 answers

XPath/XSLT nested predicates: how to get the context of outer predicate?

It seems that this question was not discussed on stackoverflow before, save for Working With Nested XPath Predicates ... Refined where the solution not involving nested predicates was offered. So I tried to write the oversimplified sample of what…
penartur
  • 9,792
  • 5
  • 39
  • 50
16
votes
4 answers

string.Contains as a predicate not a function call?

I found this sample of code on SO (can't remember from where :/) that allowed me to check for line code arguments when launching my application : if (e.Args.Length == 0 || e.Args.Any("-show".Contains)) { //show interface... } I just can't seem to…
Phoque
  • 217
  • 3
  • 14