predicate

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

一曲冷凌霜 提交于 2019-12-08 06:02:15
问题 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

Antlr superfluous Predicate required?

我的梦境 提交于 2019-12-08 04:50:45
问题 I have a file where I want to ignore parts of it. In the Lexer I use gated semantic predicates to avoid creating tokens for the uninteresting part of the file. My rules are similar to the following. A : {!ignore}?=> 'A' ; START_IGNORE : 'foo' {ignore = true; skip();} ; END_IGNORE : 'oof' {ignore = false; skip();} ; IGNORE : {ignore}?=> . {skip();} ; However unless I change START and END to also use semantic predicates (as below) it does not work.. A : {!ignore}?=> 'A' ; START_IGNORE : {true}?

NSPredicate not working with calculated field

寵の児 提交于 2019-12-08 02:17:15
问题 I have a Core Data project and am having difficulty searching the data with a simple calculated field and have no idea why it's not working. I have a Tutor entity, with core data string attributes "tutorFirstName" and "tutorLastName". I've created an additional string attribute "tutorFullName" which is populated in a Category as such: NSString *fullName = [@[self.tutorFirstName, self.tutorLastName] componentsJoinedByString:@" "]; The data is populated fine, but when I perform the following

How to implement a Predicate in Java used for cross-checking a String against an arbitrary amount of rules?

青春壹個敷衍的年華 提交于 2019-12-08 00:12:23
问题 This question is a continuation of: How to check whether an input conforms to an arbitrary amount of rules in Java? I'm trying to make use of Predicates to cross-check a string/word against a set of rules/methods that return a boolean value. However I'm having difficulties implementing it in my code. public class CapValidator { /** PRIMARY METHODS **/ private boolean capitalize(String word) { // boolean valid = true; // for (Predicate<> rule : rules) { // valid = valid && rule.test(word); //

Passing a parameter to a linq predicate

别来无恙 提交于 2019-12-07 17:10:53
问题 I want to write code like the following - public IQueryable<Invoice> InvoiceFilterForMonthAndYear(DateTime? monthAndYear = null) { var invoices = _invoices.Where(MonthAndYearPredicate(monthAndYear); return invoices; } private bool MonthAndYearPredicate(Invoice invoice, DateTime? monthAndYear) { //code to check if the invoice start or end dates is from the falls in the month I am checking for } But I can't use a predicate like that because the predicate expects just one parameter. I know I

Prolog: check transitivity for simple facts

天大地大妈咪最大 提交于 2019-12-07 15:08:30
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, C). The predicate returns true for trusts(p1, p2) or trusts(p1, p5) for example, but trusts(p5, p6)

How to implement a not_all_equal/1 predicate

拜拜、爱过 提交于 2019-12-07 05:17:58
问题 How would one implement a not_all_equal/1 predicate, which succeeds if the given list contains at least 2 different elements and fails otherwise? Here is my attempt (a not very pure one): not_all_equal(L) :- ( member(H1, L), member(H2, L), H1 \= H2 -> true ; list_to_set(L, S), not_all_equal_(S) ). not_all_equal_([H|T]) :- ( member(H1, T), dif(H, H1) ; not_all_equal_(T) ). This however does not always have the best behaviour: ?- not_all_equal([A,B,C]), A = a, B = b. A = a, B = b ; A = a, B = b

Java split stream by predicate into stream of streams

天大地大妈咪最大 提交于 2019-12-07 05:00:45
问题 I have hundreds of large (6GB) gziped log files that I'm reading using GZIPInputStream s that I wish to parse. Suppose each one has the format: Start of log entry 1 ...some log details ...some log details ...some log details Start of log entry 2 ...some log details ...some log details ...some log details Start of log entry 3 ...some log details ...some log details ...some log details I'm streaming the gziped file contents line by line through BufferedReader.lines() . The stream looks like: [

Adding a parameter to a FindAll for a Generic List in C#

时间秒杀一切 提交于 2019-12-07 04:29:26
问题 I have a list of objects that I want to filter by an integer parameter List<testObject> objectList = new List<testObject>(); // populate objectList with testObjects objectList.FindAll(GroupLevel0); private static bool GroupLevel0(testObject item) { return item._groupLevel == 0; } private class testObject { public string _FieldSQL = null; public int _groupLevel; } What I'm looking to do is to make GroupLevel0 take in an integer as a parameter instead of hardcoding to 0. I'm working in .NET 2.0

Is there a way to drain parts of a vector based on a predicate?

寵の児 提交于 2019-12-06 17:52:53
问题 I'm trying to remove some elements from a vector, based on a predicate, and collecting the result. Here's a (not working) example with an expected result: let mut v: Vec<i32> = vec![1, 2, 3, 4, 5, 6]; let drained: Vec<i32> = v.iter().filter(|e| (*e) % 2 == 0).drain(..).collect(); assert_eq!(v, vec![1, 3, 5]); assert_eq!(drained, vec![2, 4, 6]); This results in the error error[E0599]: no method named `drain` found for type `std::iter::Filter<std::slice::Iter<'_, i32>, [closure@src/main.rs:4:45