Hey guys,
I just wanted to show off our support for predicates that we've got
running with ActionStep.
A predicate is what you'll find in the contents of an "if" statement, or
the "WHERE" clause of a SQL statement. They always evaluates to TRUE or
FALSE based on the input.
ActionStep has a string-based language for building predicates and
provides hooks to use the predicates for filtering NSArrays
(ActionStep's array class). I'm just going to jump right into code to
show you what I'm talking about.
Let's say we have an NSArray containing "person" objects structured as
follows:
{firstName:String, lastName:String, age:Number, canDrive:Boolean}
Now, let's say in your application, you allow the users to filter by all
sorts of criteria. Suppose you wanted to display all drivers over 60.
The predicate string to describe this criteria would be as follows:
"canDrive == true AND age >= 60"
To actually filter the array, here's what you'd do:
var pred:NSPredicate = NSPredicate.predicateWithFormat("canDrive == true
AND age >= 60"); var driversOver60:NSArray =
people.filteredArrayUsingPredicate(pred);
And that's it. Pretty cool, huh?
And this is only the tip of the iceburg. We support nested predicates
(the use of parenthesis), OR and NOT operators, a bunch of useful
functions (min, max, stddev, mean just to name a few), the ability to
add your own functions, lots of operators (BEGINSWITH, ENDSWITH, LIKE,
IN and all the standard ones).
Also, the properties that are accessed can be chained together. Let's
say each person has "mother" and "father" properties referencing other
person objects. If I wanted to see if both parents could drive, I could
do this.
"mother.canDrive == true and father.canDrive == true"
These properties can be getters (like "getMother()", implicit getters or
methods named like "mother()") or regular fields.
These predicates can do so much I can't even mention it all here. I'm
going to be writing a tutorial in the next few days to go over more of
what they can do, and I'll post back when I have something up.
Scott
_______________________________________________
osflash mailing list
[email protected]
http://osflash.org/mailman/listinfo/osflash_osflash.org