Prev | Current Page 205 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"


Using Functions in Collection Objects
Earlier, when you looked at looping through an Array, you saw how, in order to directly modify the
items stored in an Array while looping, the Array had to be indexed using an iterated integer value.
However, what about List objects? The List collection cannot be indexed as the Array can, so how can
the values contained in a List be modified in a loop? The answer is through the use of the List ??™ s map
method.
The List map Method
The map method of the List collection applies a function to each and every item in its collection and
returns a new List with the modified values. It is quite unlike any of the class methods you have looked
at so far, as it takes a function as a parameter. This function must follow a set structure, because of strict
typing, but can be created by using local functions. The type of the function, then, is:
var fun : X - > T;
The parameter, or X , is the type of the object contained in the original list, while the return value, or T , is
the type of the object to be contained in the new List.


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