Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
haXe 2 Beginner's Guide

You're reading from  haXe 2 Beginner's Guide

Product type Book
Published in Jul 2011
Publisher
ISBN-13 9781849512565
Pages 288 pages
Edition 1st Edition
Languages

Table of Contents (21) Chapters

haxe 2
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Getting to know haXe 2. Basic Syntax and Branching 3. Being Cross-platform with haXe 4. Understanding Types 5. The Dynamic Type and Properties 6. Using and Writing Interfaces, Typedefs, and Enums 7. Communication Between haXe Programs 8. Accessing Databases 9. Templating 10. Interfacing with the Target Platform 11. A Dynamic Website Using JavaScript 12. Creating a Game with haXe and Flash Pop Quiz Answers Index

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) : Void
       {
          for(i in intList)
          {
             fun(i);
          }
       }
  5. Now, we can simply call this function, as follows:

    iterateAndApply(displayInt)

What just happened?

We created an iterateAndApply...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}