Actually I’ll be working with
Vector but since I’m passing theoretical knowledge – that won’t be a problem.
Say we want to play my favorite game: Fizz-Buzz, first I’ll create two helper methods that will do most list creation and number checking, to remove details from our way:
* Utility factory method, creates simple ranges of numbers.
* @param from Initial number.
* @param to Target number
Vector. of number in range
protected function range(from:int, to:int):Vector.
var result:Vector. = new Vector.;
var current:int = from;
while(current != to)
if(from < to)
result.fixed = true;
* Simple helper method, emulates "Fizz-Buzz" game, returns "Fizz" if number dividable by 3, "Buzz" if by 5, and "Fizz-Buzz" if by both.
* @param number Number to check.
* @return "Fizz" if number is dividable by 3, "Buzz" if by 5, and "Fizz-Buzz" if by both.
protected function fizzBuzz(number:int):String
if(number % 3 == 0 && number % 5 == 0)
else if(number % 3 == 0)
else if(number % 5 == 0)
First method as you already guessed simply creates ranges of number, while second checks individual number on it's fizz-buzz-ines. Structural approach would be to create a loop:
protected function structural():void
for each(var number:int in range(1, 101))
No surprise here I guess, using
for instead of
for each could be smarter but I was going for shortness here. But there is a functional approach too:
protected function test():void
* Used as a callback in
* @param element Current element.
* @param index Current index.
* @param vec Reference to the Vector.
protected function loog(element:int, index:int, vec:Vector.):void
trace("[" + index + "] " + fizzBuzz(element));
This approach does not look short*, but it gives us some flexibility because we've just reached new level of abstraction and went beyond Object Oriented. Here is better example:
* Higher order function that creates a loop closure.
* @param output Reference to the desired output method, such as "trace".
* @return Function with selected output method.
protected function looper(output:Function):Function
return function(element:int, index:int, vec:Vector.):void
output("[" + index + "] " + fizzBuzz(element));
looper is a Higher Order Function (hof) instead of doing anything - it creates and returns another function, we also pass closure/function as argument, this gives us ability to change way we want to output information, we can easily switch between
ILogger.log or any custom debug method
myDebugger.debug, and we can do it fluently:
range(1, 101).forEach(looper(trace)); //using trace()
range(1, 101).forEach(looper(myLogger.log)); //using Flex Logger
range(1, 101).forEach(looper(console)); //my custom debug method that traces items to Firebug Console
No ActionScript IDE support functional approach to programming, so I wouldn't recommend heavy use of fp, but there is places where FP (Functional Programming) can be very helpful, such as Game AIs, which might need to easily discard unworking tactics and try new ones, without storing whole classes/modules of tactics and plans in memory. I also found that it's quite usable to use FP in small utility and helper functions, just make sure that your team understands what it is.
* At least it does not look that short in ActionScript, but you might be surprised by it's elegance in languages like Ruby, Scala, Erlang and Haskell.
 Coding Horror: FizzBuzz