Functional ActionScript – Part II
Welcome to the second part of my series on Functional ActionScript. Part I was a brief introduction to some concepts of functional programming in ActionScript. In this second part, I will present you some examples to ActionScript's built-in functional APIs on Array
. However, first I would like to introduce you to a neat little trick that will save us some typing and make our code more clear.
Foreplay
If you take a look at the documentation of the following methods that we will discuss later (every
, some
, filter
, forEach
, and map
) you will notice that they all take a callback that, apart from the return type maybe, has a signature that looks like this:
Basically, it takes a simple function like:
…and returns a function which conforms to the callback signature shown above. Another great example for the power of higher-order functions.
The Party
After having been introduced to friend number one, namely map
, in Part I, I suggest we get to know some new friends but first a small convention:
trace
I will use the following convention to denote trace output:
Friend Number Two: every
If you want to check if all the elements of an Array
satisfy a certain condition, just write a test function and drop it into Array.every
.
Example: Everybody Even?
For example, let's see if all integer inlist
are even: First, we take theeven
function from above which takes anint
, tests if it's even and returns the correspondingBoolean
: Then, wrapeven
withwrap
— doh! — drop it intoArray.every
and see what happens:
Friend Number Three: some
Array.some
works along the lines of every
but returns true as soon as one of the elements passes the supplied test.
Example: Anybody Odd?
In the following example, we check if any (meaning: one or more) of the elements inlist
is odd: Our test function: The test:
Friend Number Four: filter
Array.filter
is really handy. Pass it a test function and it returns you an Array
with all the elements that passed the test.
Example: Who's Even, Who's Odd?
Get all even elements inlist
: …and all odd elements:
Friend Number Five: forEach
Array.forEach
is pretty much the same as Array.map
with a subtle but important difference: forEach
executes a function on each element in an Array
but unlike map
has not the purpose to modify the elements. Therefore forEach
returns void
and map
returns an Array
. This may or may not sound confusing. However, the following examples will make the difference clear…
Example: Hello
Let's say hello to all elements inlist
: In this example I purposely didn't use my carefully craftedwrap
function from above to show you how ugly the callback function can end up (line 3–6).
Old Friend: map
We've already met map
in the first part on Functional ActionScript but I allow myself to introduce her here once again. Array.map
takes a function, applies it to all elements in an Array
and returns an Array
with all modified elements.
Example: We're Square
Square all elements inlist
: …or take the square root of all elements:
Friends Forever
When I'm talking about friends, I actually mean friends. Not only will the functions above be nice to you but they also get along very well with each other. Let's see how…
Example: Rendez-Vous
Let's look at this real-world scenario: If any of the elements inlist
is odd, you want to pick out the even elements, square them and then say hello to them. No sooner said than done: Isn't the expressivess of this code just beautiful? Finding a more useless example is left as an exercise to the reader.
Doggy Bag (a.k.a Source Code)
Like what you saw? Have look at it, download it, and play with it!
Source
View Source | Download Source (ZIP, 3KB)
Thank you for your attention and stay tuned for Part III of Functional ActionScript…