Using Predicate in Swift

This is really just a syntax switch. OK, so we have this method call: [NSPredicate predicateWithFormat:@”name contains[c] %@”, searchText]; In Swift, constructors skip the “blahWith…” part and just use the class name as a function and then go straight to the arguments, so [NSPredicate predicateWithFormat: …] would become NSPredicate(format: …). (For another example, [NSArray arrayWithObject: … Read more

How to write a BOOL predicate in Core Data?

From Predicate Programming Guide: You specify and test for equality of Boolean values as illustrated in the following examples: NSPredicate *newPredicate = [NSPredicate predicateWithFormat:@”anAttribute == %@”, [NSNumber numberWithBool:aBool]]; NSPredicate *testForTrue = [NSPredicate predicateWithFormat:@”anAttribute == YES”]; You can also check out the Predicate Format String Syntax.

Predicate in Java

I’m assuming you’re talking about com.google.common.base.Predicate<T> from Guava. From the API: Determines a true or false value for a given input. For example, a RegexPredicate might implement Predicate<String>, and return true for any string that matches its given regular expression. This is essentially an OOP abstraction for a boolean test. For example, you may have … Read more

What is the purpose of “!” and “?” at the end of method names?

It’s “just sugarcoating” for readability, but they do have common meanings: Methods ending in ! perform some permanent or potentially dangerous change; for example: Enumerable#sort returns a sorted version of the object while Enumerable#sort! sorts it in place. In Rails, ActiveRecord::Base#save returns false if saving failed, while ActiveRecord::Base#save! raises an exception. Kernel::exit causes a script … Read more

How to wait until a predicate condition becomes true in JavaScript?

Javascript is single threaded, hence the page blocking behaviour. You can use the deferred/promise approach suggested by others. The most basic way would be to use window.setTimeout. E.g. function checkFlag() { if(flag === false) { window.setTimeout(checkFlag, 100); /* this checks the flag every 100 milliseconds*/ } else { /* do something*/ } } checkFlag(); Here … Read more

Delegates: Predicate vs. Action vs. Func

Predicate: essentially Func<T, bool>; asks the question “does the specified argument satisfy the condition represented by the delegate?” Used in things like List.FindAll. Action: Perform an action given the arguments. Very general purpose. Not used much in LINQ as it implies side-effects, basically. Func: Used extensively in LINQ, usually to transform the argument, e.g. by … Read more

How to convert a String to its equivalent LINQ Expression Tree?

Would the dynamic linq library help here? In particular, I’m thinking as a Where clause. If necessary, put it inside a list/array just to call .Where(string) on it! i.e. var people = new List<Person> { person }; int match = people.Where(filter).Any(); If not, writing a parser (using Expression under the hood) isn’t hugely taxing – … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)