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.
Int
and displays it:class Main { public static function main() { } public static function displayInt(i : Int) { trace(i); } }
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 } }
public static function iterateAndApply(fun : Int->Void) : Void { for(i in intList) { fun(i); } }
iterateAndApply(displayInt)
We created an iterateAndApply
function that takes a method of type Int->Void
, that is, a method that takes an Int
and returns nothing.
displayInt
function: We have simply created a function of type Int->Void
to display an Int
. iterateAndApply
function: The most important thing in this function here is that it takes a function of type Int->Void
as an argument and applies it to every element of the List intList
. Int
to the Main
class. What you should note here is that we had to initialize it in the main
function. Also, after the initialization, you should add some elements to test your application.3.144.222.185