predicate

Is there a Java 1.5 equivalent to the Predicate<T> methods in .Net?

落花浮王杯 提交于 2019-12-04 00:48:34
问题 Specifically, I'm looking for similarly clean notation to the Collection<T>.TrueForAll / Exists , etc. It feels smelly to have to write a foreach loop to inspect the return of a method on each object, so I'm hoping there's a better Java idiom for it. 回答1: Predicates are provided in the Google Collections library. 回答2: Functional Java provides first-class functions. A predicate is expressed as F<T, Boolean> . For example, here's a program that tests an array for the existence of a string that

Java join collections using functor

为君一笑 提交于 2019-12-03 18:55:40
2 collections are given with the same number of elements, say List<String> . What are elegant ways in JAVA to apply a functor on each 2 elements of collections with corresponding indexes? Say, one example could be: List<String> = { "APPLE", "PEAR" }; List<String> = { "BANANA", "ORANGE" }; A predicate that joins string together will result in the following List<String> : List<String> = { "APPLEBANANA", "PEARORANGE" }; Akin to the functors found in Apache Commons Collections, I have created binary equivalents in the past. For your situation, a binary transformer type object, which takes to two

Predicate control in Prolog

拜拜、爱过 提交于 2019-12-03 18:03:31
问题 Have a curiosity related to Prolog predicate control. Supposedly I have a predicate f(A,X) and g(B). f(A,X):- a,b,c, g(X). g(B):- true. a - returns true b - returns true. c - returns false. where a,b and c are random predicates. How can I continue to evaluate g(X) in the predicate f(A,X) if c returns false? 回答1: If your intention is to define f(A,X) such that g(X) should be evaluated whether or not c fails, then either: You could encode this using implication ( -> ) and/or disjunction ( ; ),

Convert func to predicate using reflection in C#

ⅰ亾dé卋堺 提交于 2019-12-03 17:03:20
I'm basically trying to do this , but I don't know what T will be, so I'm building things up using Reflection and Expression trees. // Input (I don't know about "Book") Type itemType = typeof(Book); // Actual Code // Build up func p => p.AuthorName == "Jon Skeet" ParameterExpression predParam = Expression.Parameter(itemType, "p"); Expression left = Expression.Field(predParam, itemType.GetField("AuthorName")); Expression right = Expression.Constant("Jon Skeet", typeof(string)); Expression equality = Expression.Equal(left, right); Delegate myDelegate = Expression.Lambda(equality, new

Why do C++ classes without member variables occupy space?

ⅰ亾dé卋堺 提交于 2019-12-03 05:12:57
I found that both MSVC and GCC compilers allocate at least one byte per each class instance even if the class is a predicate with no member variables (or with just static member variables). The following code illustrates the point. #include <iostream> class A { public: bool operator()(int x) const { return x>0; } }; class B { public: static int v; static bool check(int x) { return x>0; } }; int B::v = 0; void test() { A a; B b; std::cout << "sizeof(A)=" << sizeof(A) << "\n" << "sizeof(a)=" << sizeof(a) << "\n" << "sizeof(B)=" << sizeof(B) << "\n" << "sizeof(b)=" << sizeof(b) << "\n"; } int

How can I implement the unification algorithm in a language like Java or C#?

◇◆丶佛笑我妖孽 提交于 2019-12-03 03:52:54
问题 I'm working through my AI textbook I got and I've come to the last homework problem for my section: "Implement the Unification Algorithm outlined on page 69 in any language of your choice." On page 69, you have the following pseudo-code for the unification algorithm: function unify(E1, E2); begin case both E1 and E2 are constants or the empty list: if E1 = E2 then return {} else return FAIL; E1 is a variable: if E1 occurs in E2 then return FAIL else return {E2/E1} E2 is a variable if E2

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

荒凉一梦 提交于 2019-12-03 02:57:23
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 chain "primitive" comparisons. A trivial example would be if you were sorting a collection of objects that represent contact data. Sometimes you will want to sort by last name, first name, area code . Other times first name, last name - yet other times age, first name, area code ... etc Now, you can certainly write an additional function object for

Core Data: Query objectIDs in a predicate?

余生颓废 提交于 2019-12-03 02:16:24
问题 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

Create predicate with nested classes with Expression

匆匆过客 提交于 2019-12-03 00:37:50
I have this : public class Company { public int Id { get; set; } public string Name { get; set; } } public class City { public int Id { get; set; } public string Name { get; set; } public int ZipCode { get; set; } } public class Person { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public int? Age { get; set; } public City City { get; set; } public Company Company { get; set; } } I'd like a some case generate the predicate like this : var result = listPerson.Where(x => x.Age == 10).ToList<>(); Or this : var result = listPerson.Where( x

Can you pass an additional parameter to a predicate?

我是研究僧i 提交于 2019-12-02 23:09:56
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 predicate when using one of std's algorithms? std::vector<std::string> first, second; first.push_back("abc"); first.push_back("abc"); first.push_back("def"); first.push_back("abd"); first.push_back("cde"); first.push_back("def"); std::remove_copy_if(first.begin(), first.end(), second.begin(), is_invalid); I'm hoping to pass the following function