predicate

How to negate a method reference predicate

冷暖自知 提交于 2019-11-26 12:45:42
In Java 8, you can use a method reference to filter a stream, for example: Stream<String> 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. something like: long nonEmptyStrings = s.filter(not(String::isEmpty)).count(); I could create the not method like below but I was wondering if the JDK offered something similar. static <T> Predicate<T> not(Predicate<T> p) { return o -> !p.test(o); } Anton Balaniuc Predicate.not( … ) java-11 offers a new method Predicate#not So you can negate the method

Find first element in a sequence that matches a predicate [duplicate]

£可爱£侵袭症+ 提交于 2019-11-26 12:10:51
This question already has an answer here: Sequence find function in Python 4 answers 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 predicate(x), seq).next() But there must be something more elegant... And it would be nice if it returns a None value rather than raise an exception if no match is found. I know I could just define a function like: def get_first(predicate, seq): for i in seq: if

What is a predicate in c#? [duplicate]

半世苍凉 提交于 2019-11-26 11:44:34
问题 This question already has answers here : Predicate Delegates in C# (10 answers) Closed 5 years ago . I am very new to using predicates and just learned how to write: Predicate<int> pre = delegate(int a){ a %2 == 0 }; What will the predicate return, and how is it useful when programming? 回答1: Predicate<T> is a functional construct providing a convenient way of basically testing if something is true of a given T object. For example suppose I have a class: class Person { public string Name { get

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

送分小仙女□ 提交于 2019-11-26 09:26:11
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; } } ...and lets say an instance: var bob = new Person { Name = "Bob", Age = 30, Weight = 213, FavouriteDay = '1/1/2000' } I would like to write the following as a string in my favourite text editor.... (Person.Age > 3 AND Person.Weight > 50) OR Person.Age < 3 I would like to take this string and my object instance and evaluate a TRUE or FALSE - i.e.

How to apply multiple predicates to a java.util.Stream?

有些话、适合烂在心里 提交于 2019-11-26 09:18:55
问题 How can I apply multiple predicates to a java.util.Stream\'s filter() method? This is what I do now, but I don\'t really like it. I have a Collection of things and I need to reduce the number of things based on the Collection of filters (predicates): Collection<Thing> things = someGenerator.someMethod(); List<Thing> filtered = things.parallelStream().filter(p -> { for (Filter f : filtersCollection) { if (f.test(p)) return true; } return false; }).collect(Collectors.toList()); I know that if I

Predicate in Java

亡梦爱人 提交于 2019-11-26 07:53:14
问题 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? 回答1: I'm assuming you're talking about com.google.common.base.Predicate<T> from Guava. From the API: Determines a true or false value for a given input. For example, a RegexPredicate might implement Predicate<String> , and return true for any string that matches its given regular expression. This is

Predicate Delegates in C#

萝らか妹 提交于 2019-11-26 07:50:51
问题 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. 回答1: A predicate is a function that returns true or false . A predicate delegate is a reference to a predicate. So basically a predicate delegate is a reference to a function that returns true or false . Predicates are very useful for filtering a list of values - here is an example. using System; using System.Collections

How to wait until an element is present in Selenium?

两盒软妹~` 提交于 2019-11-26 07:28:11
问题 I\'m trying to make Selenium wait for an element that is dynamically added to the DOM after page load. Tried this: fluentWait.until(ExpectedConditions.presenceOfElement(By.id(\"elementId\")); In case it helps, here is fluentWait : FluentWait fluentWait = new FluentWait<>(webDriver) { .withTimeout(30, TimeUnit.SECONDS) .pollingEvery(200, TimeUnit.MILLISECONDS); } But it throws a NoSuchElementException - looks like presenceOfElement expects the element to be there so this is flawed. This must

Find first element in a sequence that matches a predicate [duplicate]

若如初见. 提交于 2019-11-26 05:53:17
问题 This question already has an answer here: Sequence find function in Python 4 answers 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 predicate(x), seq).next() But there must be something more elegant... And it would be nice if it returns a None value rather than raise an exception if no match is

Why Func<T,bool> instead of Predicate<T>?

我是研究僧i 提交于 2019-11-26 04:58:53
问题 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<TSource> Where<TSource>( this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate ) public static IEnumerable<TSource> Where<TSource>( this IEnumerable<TSource> source, Func<TSource, bool> predicate ) Why do they use Func<TSource, bool> instead of Predicate<TSource> ? Seems like the Predicate