O'Reilly logo

haXe 2 by Benjamin Dasnois

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action—Applying a function on every item

We are going to create a function that will take a function and apply it to every item in a list. We are going to use it to simply display every Int in a list.

  1. At first, let's create a function that takes an Int and displays it:
    class Main
    {
    public static function main()
    {
    }
    public static function displayInt(i : Int)
    {
    trace(i);
    }
    }
    
  2. Then in our Main class add a list of Int:
    class Main
    {
    public static var intList : List<Int>; public static function main()
    {
    intList = new List<Int>(); //Initialize the variable
    }
    }
    
  3. Note that we have to initialize the list in the main function.
  4. Now, let's create the function that will iterate over the list, as follows:
    public static function iterateAndApply(fun : Int->Void) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required