predicate

Filter NSArray based on another array using predicate

流过昼夜 提交于 2019-11-29 11:21:02
Consider the arrays below. The arrays contain objects of type 'Alpha'. We only care about the property username which is of type NSString . NSArray *some_usernames = @[ <multiple values of type Alpha> ] NSArray *all_usernames = @[ <multiple values of type Alpha> ] I basically want a list of all the usernames that are not in the array some_usernames , i.e. NSArray *remaining_usernames = @[ <all_usernames but not in some_usernames> ]; The way I would intend to do is: NSPredicates *predicates; for (Alpha *alpha in some_usernames) { predicate = [predicate with @"username != %@", alpha.username];

Java 8 - How to use predicate that has a function with parameter?

梦想的初衷 提交于 2019-11-29 09:50:40
I have the following code: public boolean isImageSrcExists(String imageSrc) { int resultsNum = 0; List<WebElement> blogImagesList = driver.findElements(blogImageLocator); for (WebElement thisImage : blogImagesList) { if (thisImage.getAttribute("style").contains(imageSrc)) { resultsNum++; } } if (resultsNum == 2) { return true; } else { return false; } } What is the proper way of converting it to use Java 8 Stream s? When I'm trying to use map() , I get an error since getAttribute isn't a Function . int a = (int) blogImagesList.stream() .map(WebElement::getAttribute("style")) .filter(s -> s

Is there any way to negate a Predicate?

不打扰是莪最后的温柔 提交于 2019-11-29 09:06:17
I want to do something like this: List<SomeClass> list1 = ... List<SomeClass> list2 = ... Predicate<SomeClass> condition = ... ... list2.RemoveAll (!condition); ... list2.AddRange (list1.FindAll (condition)); However, this results in a compiler error, as ! can't be applied to Predicate<SomeClass> . Is there any way to do this? You could use a lambda expression to define an anonymous delegate inplace that is the result of negating the result of the predicate: list.RemoveAll(x => !condition(x)); Another option: static Predicate<T> Negate<T>(Predicate<T> predicate) { return x => !predicate(x); }

How to test for a Match with FakeItEasy on a predicate call?

梦想与她 提交于 2019-11-29 07:42:48
I have the following call in my code: var dbResults = new List<CrossReferenceRelationshipEF>(); dbResults = dateTimeFilter == null ? new List<CrossReferenceRelationshipEF>( CrossReferenceRelationshipRepository.GetAll() .ToList().OrderBy(crr => crr.ToPartner)) : new List<CrossReferenceRelationshipEF>( CrossReferenceRelationshipRepository.SearchFor( crr => crr.HistoricEntries .Any(he => he.ModifiedDatetime > dateTimeFilter)) .ToList().OrderBy(crr => crr.ToPartner)); and I am trying to use FakeItEasy to verify that when the dateTimeFilter has a value, the SearchFor(…) is being called within my

Predicate control in Prolog

梦想与她 提交于 2019-11-29 06:45:36
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? 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 ( ; ), or f(A,X) doesn't need to be defined in terms of c . This assumes c has no side-effects (e.g., asserting

List<object>.RemoveAll - How to create an appropriate Predicate

拥有回忆 提交于 2019-11-29 01:26:22
问题 This is a bit of noob question - I'm still fairly new to C# and generics and completely new to predicates, delegates and lambda expressions... I have a class 'Enquiries' which contains a generic list of another class called 'Vehicles'. I'm building up the code to add/edit/delete Vehicles from the parent Enquiry. And at the moment, I'm specifically looking at deletions. From what I've read so far, it appears that I can use Vehicles.RemoveAll() to delete an item with a particular VehicleID or

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

二次信任 提交于 2019-11-28 23:22:46
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 = { a: { $gt: 0 }, b: /^abc/ }; if(query(qry).matches(obj)) { // do something appropriate since } instead of: var obj = { a: 1, b: 'abcdefg' }; if(obj.a>0 && qry.b.test(obj.b)) { // do something appropriate } I'm using Node.js, so anything on NPM would be great. It would be an added bonus if the library can select objects out of an array as well as just matching

Correct Way to Define a Predicate Function in C++

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 23:12:58
问题 I'm trying to write predicate function for use with STL algorithms. I see that they are two ways to define a predicate: (1) Use a simple function as below: bool isEven(unsigned int i) { return (i%2 == 0); } std::find_if(itBegin, itEnd, isEven); (2) Use the operator() function as below: class checker { public: bool operator()(unsigned int i) { return (i%2 == 0); } }; std::find_if(itBegin, itEnd, checker); I have more use for the second type as I usually would like to create a predicate object

MOQ - LINQ Predicates in Setup Method

耗尽温柔 提交于 2019-11-28 22:33:29
问题 In my method, I have my repository doing this: bool isConditionMet = MyRepository.Any(x => x.Condition == true); I am attempting to mock this using MOQ like so: MyMockedRepository.Setup(x => x.Any(y => y.Condition == true)).Returns(true); However, when the code executes, the repository call always returns false. Is there a way to do this using MOQ? ** EDIT - Adding code per request ** I am using NHibernate so my Any method is in my base repository and implemented as such: public virtual bool

Xpath expression with multiple predicates

自作多情 提交于 2019-11-28 19:06:20
I am trying to build a complex xpath expression which will answer the following condition. From the XML data below, returns the User entity which: His loginname is " user1 " His name is " User 1 " He has 2 different profiles values which are " operator " and " admin " (I don't know the exact order ahead) <user> <login>user1</login> <name>User 1</name> <profile> <value>admin</value> <id>2</id> <description>admin users</description> </profile> <profile> <value>operator</value> <id>1</id> <description>Operator</description> </profile> </user> <user> <login>user2</login> <name>User 2</name>