Prev | Current Page 211 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

map accepts an iterator as its first parameter and returns a new iterator,
and Lambda.mapi performs the same feat, while passing an index to the processing function. Here is an
example of these two functions in use:
class Mapping
{
public static function main()
{
var myArr : Array < Int > = [1, 2, 3, 4];
var newIter : Iterator < Int > ;
var funIter = function( x : Int ) : Int
(continued)
98
Part I: The Core Language
{
return x * 2;
}
newIter = Lambda.map( myArr, funIter );
// Outputs: 2, 4, 6, 8
for ( i in newIter )
trace( i );
}
}
array() And list()
Creating an iterator from an Array or List is easy, thanks to the iterator() method they both expose.
However, what about creating a List or an Array from an iterator? Luckily, this is exactly what Lambda.
array() and Lambda.list() does. One of the great benefits to these methods is that it is quite quick
and simple to convert an Array to a List and vice versa by using the methods as a proxy:
var myArr : Array < Int > = [ 1, 2, 3, 4 ];
// Creates the List from the Array
var myLst : List < Int > = Lambda.


Pages:
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223