{"id":4575,"date":"2014-03-30T13:42:06","date_gmt":"2014-03-30T13:42:06","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/problem-about-predicate-collection-of-common-programming-errors\/"},"modified":"2014-03-30T13:42:06","modified_gmt":"2014-03-30T13:42:06","slug":"problem-about-predicate-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/problem-about-predicate-collection-of-common-programming-errors\/","title":{"rendered":"problem about predicate-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/e5c7e713b6f536d3fa0c9e1fbbdfb9c1?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nMPelletier<br \/>\nvb.net linq predicate<br \/>\nI need to build a dynamic linq query with or operators. I have seen PredicateBuilder but that is in C# and my project is in VB. Basically I need to build a WHERE clause similar to this:Where((this = 1 AND that = 2) OR (this = 1 AND that = 4) OR (this = 2 AND that = 4))but the problem is the number will have to be determined dynamically at runtime, and added using a loop, likefor each item in myItemsquery = query.OR (this = item.a AND this = item.b) nextHow could I go about doing that?<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/727b1b8d92be46e7269db294d06d44df?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nMaxim Gershkovich<br \/>\nc# .net vb.net predicate<br \/>\nWhen are predicates appropriate and what is the best pattern for usage? What are the advantages of predicates? It seems to me like most cases where a predicate can be employed a tight loop would accomplish the same functionality?I don&#8217;t see a reusability argument given you will probably only implement a predicate in one method right?They look and feel nice but besides that they seem like you would only employ them when you need a quick hack on the collection classes? UPDATEBut why would you be r<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/923164a150f414e076b32c4c6bbfecab?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nDan Atkinson<br \/>\nc# lambda antlr dsl predicate<br \/>\nThis is a simplified version of the original problem.I have a class called Person:public class Person {public string Name { get; set; }public int Age { get; set; }public int Weight { get; set; }public DateTime FavouriteDay { get; set; } }&#8230;and lets say an instance:var bob = new Person {Name = &#8220;Bob&#8221;,Age = 30,Weight = 213,FavouriteDay = &#8216;1\/1\/2000&#8217; }I would like to write the following as a string in my favourite text editor&#8230;.(Person.Age &gt; 3 AND Person.Weight &gt; 50) OR Person.Age &lt; 3I wou<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/592e6e89add7a3937a41c963941e10e1?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nAndy G<br \/>\nc# generics datagrid filter predicate<br \/>\nIm trying to apply a filter to a DataGrid in WPF and the filter property needs a Predicate ex:dataGrid1.Filter = p =&gt; p.A_Field_Table1.Contains(textBox.Text);But my datagrid is being filled with reflection, so I only know the type of objects inside the datagrid at runtime.Then I created a method that dynamically generates the Predicate&lt; T &gt; :public static Predicate&lt; T &gt; GetPredicate&lt; T &gt;(string column, string valueP, T objSource, string table){Type itemType = typeof(T);Paramete<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/a32206c5722f7a0bde8cc7295f998884?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nTheKido<br \/>\nc# linq lambda expression predicate<br \/>\nI have class that receives Type and an Expression (without TDeligate). I know the Expression is based on: Expression&lt;Func&lt;T, bool&gt;&gt; but I don&#8217;t have the T. Instead I have the Type. What I need is to execute the expression against a list and return the sub-list of results. Something like list.Where(Expression). Here is the method:public IEnumerable Query(Type type, Expression exp) {var list = GetListByType(type);return list.Where(exp); \/\/ &lt;&lt; How do I make this happen? }Edit #1I<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/d9e7e2aa3fbb390905e5d1c9d5ecdc79?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nMikMik<br \/>\nc++ stl set predicate<br \/>\nI was refactoring some code and found there are two places that can be written with the same code except the comparator of a set is less&lt;double&gt; in one place and greater&lt;double&gt; in the other. Something like:double MyClass::Function1(double val) {std::set&lt;double, less&lt;double&gt; &gt; s;\/\/ Do something with s }double MyClass::Function2(double val) {std::set&lt;double, greater&lt;double&gt; &gt; s;\/\/ Do the same thing with s as in Function1 }So I thought of doing:double MyClass::G<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/af92c7f9716e814795682a607057d6b3?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\natreat<br \/>\nios core-data nspredicate predicate<br \/>\nI&#8217;m trying to create the following predicate[NSPredicate predicateWithFormat:@&#8221;name != %@ AND date %@ %@&#8221;, varName, operator, varDate]Where varName and varDate are NSString and NSDate objects respectively. The operator variable is declared like so:NSString *operator = (direction == kPageDirectionForward) ? @&#8221;&gt;&#8221; : @&#8221;&lt;&#8220;;Basically I want to save a couple lines when creating the predicate. I know I can create different predicates based on the value of direction, but this use case has me thinki<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/5c4618f1ad244d69a8d72d9960136d8f?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nPiwacket<br \/>\nxcode predicate sigabrt<br \/>\nI&#8217;m a noob and trying to convert an example from a book into an app I can use. The sample app is a modified version of the contacts application and it works.I&#8217;ve done some further modification, and the search no longer works. It sigabrts on the following lineself.filteredAnswercards = [flattenedArrayfilteredArrayUsingPredicate:predicate];I&#8217;m stumped. my head is bloody from beating it against my keyboard. ANY help is massively appreciated.Thanks.<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/2ebb47a2f272e8fe85a1b9106c4c1714?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nDov<br \/>\nobjective-c ios core-data subquery predicate<br \/>\nI am working on an app, which makes use of a Core Data Model and I am encountering a problem while using an NSPredicate.My data model looks like this:Asset 1 to 1 relationship with SearchAsset many to many relationship with SearchTermsAsset has several fields like unique_ID, URL, etc.. while SearchTerms has only one field called terms. SearchAsset has no fields, it only points to an Asset.When I want to do a search I call the following code:-(void)searchBar:(UISearchBar *)searchBar textDidChange<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/36d4804e354dbbda056a3a6079867448?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\namitsbajaj<br \/>\nios uitableview core-data predicate searchbar<br \/>\nI have a simple TableViewController which has an add button which goes over to a ModalViewController; the user adds in text into a text field, presses save and that dismisses and the entry is added to the Entities and Attributes in Core Data and displayed in the TableViewController. It works well. I firstly started off with another button that pulled up a UISearchBar in a Table View and I could search for results and pull that back; that worked. I then added a new tab to the bottom of the TableV<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/7e6ad2f749efc7520f088fb4e941fceb?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nTechZen<br \/>\niphone ios core-data nspredicate predicate<br \/>\nI&#8217;m working on an iphone application and I have a simple many-to-many relationship set up with Group and Contact objects. A group can have many contacts and contacts can belong to multiple groups.I&#8217;m trying to select all groups that a particular contact does NOT already belong to using the following predicate. (Note: the uid field is a string field that I used to uniquely identify contact entities)[NSPredicate predicateWithFormat:@&#8221;ALL contacts.uid != %@&#8221;, contactUId]According to Apple&#8217;s Predica<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/77aa7ebc1b5d1f5490f1d8aa873cae56?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nSchoob<br \/>\nios exception core-data predicate nsmanagedobjectcontext<br \/>\nI&#8217;d like to execute a fetch request:NSFetchRequest *fetchRequest = [NSFetchRequest new]; [fetchRequest setIncludesPendingChanges:YES]; NSSet *set = [NSSet setWithObjects:@&#8221;TESTNAME&#8221;,@&#8221;TEST&#8221;, nil]; [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@&#8221;NONE name IN %@&#8221;, set]];when executing the request on an unsaved NSManagedObjectContext the app is terminated with exception:* Terminating app due to uncaught exception &#8216;NSInvalidArgumentException&#8217;, reason: &#8216;The left hand<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/77e34769753cf2b7a8bc4419df649320?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nlps<br \/>\niphone core-data count one-to-many predicate<br \/>\nI have an entity matches which has a related to-many entity sets. I want to get a count of how many sets have the attribute &#8216;set_finished&#8217; set to YES for a particular match. I&#8217;m trying to do this with:NSPredicate *predicate = [NSPredicate predicateWithFormat:@&#8221;ANY set_finished == YES&#8221;];NSUInteger numberOfFinishedSets = [[[match valueForKeyPath:@&#8221;sets&#8221;] filteredArrayUsingPredicate:predicate ] count];The second line crashes with this error, which I don&#8217;t understand. Can anyone shed some light on t<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/bea0c00a768f4b14eb969ba1084fd391?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nuser1121366<br \/>\nprolog swi-prolog predicate gnu-prolog<br \/>\nI wrote the following simple code, and I expect that when I write &#8216;male.&#8217;, this code ask me once &#8220;is it male?&#8221; and if i input &#8216;No&#8217; it write on screen &#8220;she is female&#8221;. male :- ( print(&#8216;is it male ? &#8216;),read(yes)) -&gt; true; asserta( not(male)),female. female:- not(male),print(&#8216;she is female&#8217;). not(P) :- (call(P) -&gt; fail ; true) .but this code has following error:uncaught exception: error(permission_error(modify,static_procedure,not\/1),asserta\/1);the error in swi-prolog is :ERROR: asserta\/1: No<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/icAID.jpg?s=32&amp;g=1\" \/><br \/>\nmbratch<br \/>\nprolog predicate discrete-mathematics<br \/>\nI am just starting to learn prolog. I am learning it for my CS 2300 class. I am trying to learn the basics right now. How do I add a predicate to my .pl file? I am just trying to add a predicate to my .pl file. I am doing this:married(X,Y) :- married(Y,X).And I get the errors:ERROR: Undefined procedure: (:-)\/2 ERROR: Rules must be loaded from a file ERROR: See FAQ at http:\/\/www.swi-prolog.org\/FAQ\/ToplevelMode.txAm I supposed to enable this somehow?This is the given .pl file:% File FAMILY.PL<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/3189f1e006358ec6c65481b9605cd0e3?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nLightness Races in Orbit<br \/>\nc++ stl predicate<br \/>\nI&#8217;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<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/8bfb71c7520e42fea3dd3505c59802db?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\ndrby<br \/>\nc++ stl functional-programming predicate<br \/>\nIs there any way that you can combine predicates? Lets say I have something like this:class MatchBeginning : public binary_function&lt;CStdString, CStdString, bool&gt; { public:bool operator()(const CStdString &amp;inputOne, const CStdString &amp;inputTwo) const{ return inputOne.substr(0, inputTwo.length()).compare(inputTwo) == 0; } };int main(int argc, char* argv[]) {CStdString myString(&#8220;foo -b ar -t az&#8221;); vector&lt;CStdString&gt; tokens;\/\/ splits the string every time it encounters a &#8220;-&#8220;spli<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/0829d483a777e60817e30cba971adccc?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\ndeworde<br \/>\nc++ vector sorting predicate user-defined<br \/>\nI&#8217;m working on a system where I need to be able to sort a vector by a given predicate, which my classes shouldn&#8217;t have control over. Basically, I pass them a derived class and they blindly sort on it.As one of the &#8220;delightful quirks&#8221;, one of the sort patterns is order of entry. Here&#8217;s what I&#8217;ve got so far.struct Strategy {virtual bool operator()(const Loan&amp; lhs, const Loan&amp; rhs) const = 0; };struct strategyA : public Strategy {bool operator()(const Loan&amp; lhs, const Loan&amp; rhs) con<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/5e86ad41964dc6fb75884c74b647149d?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nChristianLinnell<br \/>\nc# list delegates predicate<br \/>\nIt&#8217;s my understanding that if I want to get the ID of an item in a list, I can do this:private static void a() {List&lt;string&gt; list = new List&lt;string&gt; {&#8220;Box&#8221;, &#8220;Gate&#8221;, &#8220;Car&#8221;};Predicate&lt;string&gt; predicate = new Predicate&lt;string&gt;(getBoxId);int boxId = list.FindIndex(predicate); }private static bool getBoxId(string item) {return (item == &#8220;box&#8221;); }But what if I want to make the comparison dynamic? So instead of checking if item==&#8221;box&#8221;, I want to pass in a user-entered string to t<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/c411859782b186227509041f53f6daff?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\njjung<br \/>\nandroid jython predicate monkeyrunner<br \/>\nI want to use MonkeyRunner to generate MotionEvent (from screen touch). I have read the info on http:\/\/developer.android.com\/guide\/developing\/tools\/monkeyrunner_concepts.html.The problem is that for this jython program:from com.android.monkeyrunner import MonkeyRunnerif __name__ == &#8216;__main__&#8217;:# Connects to the current device, returning a MonkeyDevice objectdevice = MonkeyRunner.waitForConnection()I get the following error when executed:Traceback (most recent call last):File &#8220;C:\\Documents and Se<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/e9cba306547094e879790a0cc69f784e?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nlnafziger<br \/>\niphone objective-c core-data predicate nsfetchrequest<br \/>\nI 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 NSManagedObjectNSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@&#8221;Place&#8221;]; request.predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {Place *sortPlace = (Place *)evaluatedObject;CLLocation *place<\/li>\n<\/ul>\n<p>Web site is in building<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MPelletier vb.net linq predicate I need to build a dynamic linq query with or operators. I have seen PredicateBuilder but that is in C# and my project is in VB. Basically I need to build a WHERE clause similar to this:Where((this = 1 AND that = 2) OR (this = 1 AND that = 4) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4575","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4575","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4575"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4575\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}