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.
If you take a look at the documentation of the following methods that we will discuss later (
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.
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:
traceI 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
Example: Everybody Even?For example, let's see if all integer in
listare even: First, we take the
evenfunction from above which takes an
int, tests if it's even and returns the corresponding
Boolean: Then, wrap
wrap— doh! — drop it into
Array.everyand 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 in
listis 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 in
list: …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
map returns an
Array. This may or may not sound confusing. However, the following examples will make the difference clear…
Example: HelloLet's say hello to all elements in
list: In this example I purposely didn't use my carefully crafted
wrapfunction 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 SquareSquare all elements in
list: …or take the square root of all elements:
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-VousLet's look at this real-world scenario: If any of the elements in
listis 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!
SourceView Source | Download Source (ZIP, 3KB)
Thank you for your attention and stay tuned for Part III of Functional ActionScript…