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
469
votes
14 answers

How to negate a method reference predicate

In Java 8, you can use a method reference to filter a stream, for example: Stream s = ...; long emptyStrings = s.filter(String::isEmpty).count(); Is there a way to create a method reference that is the negation of an existing one, i.e.…
assylias
  • 321,522
  • 82
  • 660
  • 783
273
votes
4 answers

Find first element in a sequence that matches a predicate

I want an idiomatic way to find the first element in a list that matches a predicate. The current code is quite ugly: [x for x in seq if predicate(x)][0] I've thought about changing it to: from itertools import dropwhile dropwhile(lambda x: not…
fortran
  • 74,053
  • 25
  • 135
  • 175
270
votes
10 answers

What is a Predicate Delegate and where should it be used?

Can you explain to me: What is a Predicate Delegate? Where should we use predicates? Any best practices when using predicates? Descriptive source code will be appreciated.
Canavar
  • 47,715
  • 17
  • 91
  • 122
231
votes
4 answers

Why Func instead of Predicate?

This is just a curiosity question I was wondering if anyone had a good answer to: In the .NET Framework Class Library we have for example these two methods: public static IQueryable Where( this IQueryable source, …
Svish
  • 152,914
  • 173
  • 462
  • 620
198
votes
7 answers

How to convert a String to its equivalent LINQ Expression Tree?

This is a simplified version of the original problem. I have a class called Person: public class Person { public string Name { get; set; } public int Age { get; set; } public int Weight { get; set; } public DateTime FavouriteDay { get; set;…
Codebrain
  • 5,565
  • 4
  • 28
  • 21
186
votes
4 answers

What is a predicate in c#?

I am very new to using predicates and just learned how to write: Predicate pre = delegate(int a){ a %2 == 0 }; What will the predicate return, and how is it useful when programming?
Jebli
  • 2,775
  • 6
  • 29
  • 37
175
votes
3 answers

Built-in Java 8 predicate that always returns true?

Google Guava has a predicate that always returns true. Does Java 8 have something similar for its Predicate? I know I could use (foo)->{return true;}, but I want something pre-made, analogous to Collections.emptySet().
Garret Wilson
  • 18,219
  • 30
  • 144
  • 272
159
votes
10 answers

Delegates: Predicate vs. Action vs. Func

Can someone provide a good explanation (hopefully with examples) of these 3 most important delegates: Predicate Action Func
Sasha
145
votes
25 answers

How to wait until a predicate condition becomes true in JavaScript?

I have javascript function like this: function myFunction(number) { var x=number; ... ... more initializations //here need to wait until flag==true while(flag==false) {} ... ... do something } The problem is that…
110
votes
5 answers

What is the purpose of "!" and "?" at the end of method names?

Sometimes I see methods in Ruby that have "?" and "!" at the end of them, e.g: name = "sample_string" name.reverse name.reverse! name.is_binary_data? I was wondering what their purpose is? Are they just syntax sugarcoating?
itsaboutcode
  • 24,525
  • 45
  • 110
  • 156
103
votes
4 answers

Predicate in Java

I am going through the code which uses Predicate in Java. I have never used Predicate. Can someone guide me to any tutorial or conceptual explanation of Predicate and its implementation in Java?
srikanth
  • 1,211
  • 2
  • 9
  • 11
100
votes
7 answers

How to write a BOOL predicate in Core Data?

I have an attribute of type BOOL and I want to perform a search for all managed objects where this attribute is YES. For string attributes it is straightforward. I create a predicate like this: NSPredicate *predicate = [NSPredicate…
Proud Member
  • 40,078
  • 47
  • 146
  • 231
98
votes
8 answers

Using Predicate in Swift

I'm working through the tutorial here (learning Swift) for my first app: http://www.appcoda.com/search-bar-tutorial-ios7/ I'm stuck on this part (Objective-C code): - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope { …
levitatejay
  • 1,278
  • 1
  • 9
  • 14
70
votes
4 answers

C# Linq Where(expression).FirstorDefault() vs .FirstOrDefault(expression)

What is the difference between these two Linq queries: var result = ResultLists().Where( c=> c.code == "abc").FirstOrDefault(); // vs. var result = ResultLists().FirstOrDefault( c => c.code == "abc"); Are the semantics exactly the same? Iff…
SOFextreme
  • 731
  • 1
  • 5
  • 6
62
votes
5 answers

How to make Selenium wait until an element is present?

I'm trying to make Selenium wait for an element that is dynamically added to the DOM after page load. I tried this: fluentWait.until(ExpectedConditions.presenceOfElement(By.id("elementId")); In case it helps, here is fluentWait: FluentWait…
Steve Chambers
  • 37,270
  • 24
  • 156
  • 208
1
2 3
99 100