predicate

How to filter child entities collections with predicate?

二次信任 提交于 2019-12-10 13:37:10
问题 I have an entity service on which I need to filter a collection of child entity, based on a list of id's. My service have a public method which receive the id of the parent entity and a list of id's of some of his children entities. By default, I know that JPA will fetch all related entities and this his the actual behavior. But we need to work on the performance of the service. So instead of getting all related entities and filter them with many loop (filter on id's and also on other

How to define a predicate as a function argument

谁说胖子不能爱 提交于 2019-12-09 16:39:14
问题 I want to be able to write something as void Start(some condition that might evaluate to either true or false) { //function will only really start if the predicate evaluates to true } I'd guess it must be something of the form: void Start(Predicate predicate) { } How can I check inside my Start function whenever the predicate evaluated to true or false? Is my use of a predicate correct? Thanks 回答1: Here's a trivial example of using a predicate in a function. static void

How to call Dictionary<K, V>.TryGetValue() where K : Predicate<T>, V : enum

假装没事ソ 提交于 2019-12-08 22:10:30
I have Dictionary<Predicate<double>, SomeEnum> : var dic = new Dictionary<Predicate<double>, SomeEnum> { { (d) => d < 10, SomeEnum.Foo }, { (d) => d > 90, SomeEnum.Bar } }; I want to call TryGetValue(K, out V) against it like this: dic.TryGetValue(99) and receive SomeStruct.Bar But first param for TryGetValue() is Predicate<T> , not just T . How can I do what I want? I found only a dirty workaround: var kpv = dic.FirstOrDefault(p => p.Key(99)); if (kpv.Key != null) var result = kpv.Value; Are there other ways? Or how to implement my idea properly? - declare a key not as a constant but like a

Conditional where clause in JPA criteria query

天涯浪子 提交于 2019-12-08 16:27:24
问题 I am facing an issue for JPA criteria query. How can I add multiple where clause in my Criteria Query with if else... My Requirement is: CriteriaBuilder builder = getEm().getCriteriaBuilder(); CriteriaQuery<Object> query = builder.createQuery(Object.class); // From Root<EntityClass> entity= query.from(EntityClass.class); // Select query.multiselect(entity.get("id").get("col1"),entity.get("id").get("col2")); // Where Predicate p1 = builder.and(builder.equal(entity.get("col3"), value3));

Find whether a tree is a binary search tree in Haskell

扶醉桌前 提交于 2019-12-08 16:19:36
问题 type BSTree a = BinaryTree a data BinaryTree a = Null | Node (BinaryTree a) a (BinaryTree a) deriving Show flattenTree :: BinaryTree a -> [a] flattenTree tree = case tree of Null -> [] Node left val right -> (flattenTree left) ++ [val] ++ (flattenTree right) isBSTree :: (Ord a) => BinaryTree a -> Bool isBSTree btree = case btree of Null -> False tree -> (flattenTree tree) == sort (flattenTree tree) What I want to do is to write a function to determine whether the given tree is a binary search

NSInvalidArgumentException', reason: 'Unknown predicate type for predicate: BLOCKPREDICATE(0x70ad750)' Error

陌路散爱 提交于 2019-12-08 15:50:40
问题 I have a core data database and I am trying to create a fetch request using a block predicate, but I get an Unknown Predicate error: NOTE: employeeToHouse is a property of type House that was created for me when I subclassed NSManagedObject NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Place"]; request.predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) { Place *sortPlace = (Place *)evaluatedObject; CLLocation *placeLocation =

IEnumerable<T>.Contains with predicate

五迷三道 提交于 2019-12-08 14:26:28
问题 I need just to clarify that given collection contains an element. I can do that via collection.Count(foo => foo.Bar == "Bar") > 0) but it will do the unnecessary job - iterate the whole collection while I need to stop on the first occurrence. But I want to try to use Contains() with a predicate, e.g. foo => foo.Bar == "Bar" . Currently IEnumerable<T>.Contains has two signatures: IEnumerable<T>.Contains(T) IEnumerable<T>.Contains(T, IEqualityComparer<T>) So I have to specify some variable to

Associative Lists in Prolog

自闭症网瘾萝莉.ら 提交于 2019-12-08 07:38:59
问题 my task is to implement maps with lists. We defined associative lists as follows: [] is the list, k is a key, v is a value and a is an associative list, then [[k, v] | a] is an associative list. so now ive got to write a predicate, in which it checks if the given argument is a associative list. for example: ?- test([[a,5]]). -> true., ?- test([[1],[2]]). -> false. im really in despair, i hope someone can help me there greetings 回答1: I may say that associative lists in SWI-Prolog are

how to make search bar as iPhone contact app Or search bar which handles more than one incomplete kewords

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 07:33:28
问题 i creating a search bar which should behave like iPhones contact search bar.currently it does partially. here is my code -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { NSRange whiteSpaceRange = [result rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]]; if (whiteSpaceRange.location != NSNotFound) { NSString * stringAfterSpace = [result substringFromIndex:whiteSpaceRange.location]; NSString *

Prolog: check transitivity for simple facts

£可爱£侵袭症+ 提交于 2019-12-08 07:26:43
问题 My intention was to implement a simple example (just for myself) of transitivity in Prolog. These are my facts: trust_direct(p1, p2). trust_direct(p1, p3). trust_direct(p2, p4). trust_direct(p2, p5). trust_direct(p5, p6). trust_direct(p6, p7). trust_direct(p7, p8). trust_direct(p100, p200). I've written this predicate to check whether A trusts C , which is true whenever there is a B that trusts C and A trusts this B : trusts(A, B) :- trust_direct(A, B). trusts(A, C) :- trusts(A, B), trusts(B,