Prev | Current Page 207 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"


The List filter Method
The List collections filter method works in a similar way to the map method, with the exception that,
instead of applying a function to affect the values of the items contained in the List, the function passed
to the method compares each items value and returns a Boolean result. If the result of the function is
true, then the item is returned in a new List object, otherwise it is excluded. This gives the effect, as
you ??™ ve probably already guessed, of filtering your values.
class ListFiltering
{
public static function main()
{
var strLst = new List < List > ();
strLst.add(???Bob???);
strLst.add(???Burt???);
strLst.add(???James???);
strLst.add(???Rupert???);
var newLst : List < String > = new List();
var fun1 : String - > Bool = function( x : String ) : Bool
{
return ( x.length % 2 != 0 );
}
var fun2 : String - > Bool = function( x : String ) : Bool
{
return StringTools.startsWith( x, ???B??? );
}
// Outputs: [???Bob???, ???James???]
newLst = strLst.filter( fun1 );
trace(newLst);
// Outputs: [???Bob???, ???Burt???]
newLst = strLst.


Pages:
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219