predicate

Why doesn't Java 8's Predicate<T> extend Function<T, Boolean>

拜拜、爱过 提交于 2019-12-02 21:44:20
If I wrote the Predicate interface, I'd want to encode in the interface the fact that it's just a function that returns a primitive boolean , like this: @FunctionalInterface public interface Predicate<T> extends Function<T, Boolean> { boolean test(T t); @Override default Boolean apply(T t) { return Boolean.valueOf(test(t)); } } I was wondering, is there a compelling reason Java 8 API designers chose to keep the Predicate completely separate from Function ? Is there some evidence that they considered doing so and decided against it? I guess similar question goes for all the other 'special'

How do I form a good predicate delegate to Find() something in my List<T>?

穿精又带淫゛_ 提交于 2019-12-02 15:37:23
After looking on MSDN, it's still unclear to me how I should form a proper predicate to use the Find() method in List using a member variable of T (where T is a class) For example: public class Car { public string Make; public string Model; public int Year; } { // somewhere in my code List<Car> carList = new List<Car>(); // ... code to add Cars ... Car myCar = new Car(); // Find the first of each car made between 1980 and 2000 for (int x = 1980; x < 2000; x++) { myCar = carList.Find(byYear(x)); Console.Writeline(myCar.Make + myCar.Model); } } What should my "byYear" predicate look like? (The

How to convert an Expression<Func<T, bool>> to a Predicate<T>

こ雲淡風輕ζ 提交于 2019-12-02 15:24:53
I have a method that accepts an Expression<Func<T, bool>> as a parameter. I would like to use it as a predicate in the List.Find() method, but I can't seem to convert it to a Predicate which List takes. Do you know a simple way to do this? public IList<T> Find<T>(Expression<Func<T, bool>> expression) where T : class, new() { var list = GetList<T>(); var predicate = [what goes here to convert expression?]; return list.Find(predicate); } Update Combining answers from tvanfosson and 280Z28, I am now using this: public IList<T> Find<T>(Expression<Func<T, bool>> expression) where T : class, new() {

Core Data: Query objectIDs in a predicate?

心不动则不痛 提交于 2019-12-02 14:14:49
I am fetching a set of objects from a Core Data persistent store using a fetch request and a predicate. My current predicate simply checks whether an attribute is >= a certain value. This all works great, except that I want to finally exclude any objects that are currently held in an array. I basically need to be able to exclude a set of objects, and the only way I think I can do this is to be able to get a list of objectID from my managed objects array, and create another expression in my predicate to ensure that any objects returned don't have the same objectID . I.E. @"ANY records.objectID

LISP: with predicate as parameter

别说谁变了你拦得住时间么 提交于 2019-12-02 08:10:40
问题 I want a predicate as a parameter of a function. (DEFUN per (F L) (cond ((F L) 'working) (T 'anything))) (per 'numberp 3) as result it raises an error: Undefined operator F in form (F L). 回答1: As explained in Technical Issues of Separation in Function Cells and Value Cells, Common Lisp is a Lisp-2, i.e., you need funcall: (defun per (F L) (if (funcall F L) 'working 'other)) (per #'numberp 3) ==> WORKING (per #'numberp "3") ==> OTHER See also apply. 回答2: Late to the party, but here's another

C++11 algorithms with multiple predicates

倖福魔咒の 提交于 2019-12-02 06:09:47
问题 Functions such as std::find_if from the algorithm header are really useful, but 1 serious limit for me is the fact that I can only use 1 predicate for each call to count_if . For example given a container like an std::vector I would like to apply, at the same time, with the same iteration of find_if , multiple predicates; there is something in the standard library that makes this possible while keeping this functional approach ? 回答1: Just combine them with a lambda: std::find_if(begin(vec),

Use of OR operator on Cloudkit predicate

孤街浪徒 提交于 2019-12-02 02:05:57
问题 I have a cloudkit record with 5 fields: Active User1 User2 User3 User4 I am trying to pick the record where one of the 4 user fields match with the userID variable. Only one other the 4 user fields will match the userID variable. This is the code I am using: var userID = "0984093843897" predicate = NSPredicate(format: "userID1 = %@ OR userID2 = %@ OR userID3 = %@ OR userID4 = %@ AND active = %@", userID, userID, userID, userID, true) Basically what I am trying to achieve is something similar

C++11 algorithms with multiple predicates

那年仲夏 提交于 2019-12-02 00:47:08
Functions such as std::find_if from the algorithm header are really useful, but 1 serious limit for me is the fact that I can only use 1 predicate for each call to count_if . For example given a container like an std::vector I would like to apply, at the same time, with the same iteration of find_if , multiple predicates; there is something in the standard library that makes this possible while keeping this functional approach ? Just combine them with a lambda: std::find_if(begin(vec), end(vec), [](elem_t val) { return f1(val) || f2(val); }); 来源: https://stackoverflow.com/questions/20754848

Use of OR operator on Cloudkit predicate

自古美人都是妖i 提交于 2019-12-02 00:32:10
I have a cloudkit record with 5 fields: Active User1 User2 User3 User4 I am trying to pick the record where one of the 4 user fields match with the userID variable. Only one other the 4 user fields will match the userID variable. This is the code I am using: var userID = "0984093843897" predicate = NSPredicate(format: "userID1 = %@ OR userID2 = %@ OR userID3 = %@ OR userID4 = %@ AND active = %@", userID, userID, userID, userID, true) Basically what I am trying to achieve is something similar to the statement bellow: if userID1 == userID || userID2 == userID || userID3 == userID || userID4 ==

How can I pass a predicate as parameter for another predicate in Prolog?

不问归期 提交于 2019-12-01 18:21:40
问题 I have these 3 predicates: times(X, Y):- Result is X * Y. minus(X, Y):- Result is X - Y. plus(X, Y):- Result is X + Y. and I want to pass for example times(2,2) in the plus(X, Y) like this plus(times(2,2), minus(X, Y)) . 回答1: The relationship between the title of your question and the text of your question is unclear to me, and I think @false is probably right that there is a more fundamental misunderstanding about Prolog here. I don't know if this really addresses your need or not, but the