Arrow Operators
[# Currently borken! # # a -> a + 1 # incrementor # # (a, b) -> a + b # adder # # users.each( # u -> "<b>$u.surname</b>, $u.forename" # ) # # users.sort( # (a, b) -> a.surname cmp b.surname # ) #] --EOF--look inside...
Thus Spake Andy:
Thin arrows provide a lightweight syntax for creating simple functions
(lambda functions for those in the know). The first example creates an
incrementing function that takes an a
and returns a + 1
. The
second example takes an a
and a b
and returns their sum. The third
example passes a function to the each
list virtual method that we call
for a list of users. The function is called once for each user in the list,
passed to the function as u
. The function formats the user's name as HTML
and returns it. The fourth example shows how a list can be sorted by
providing a comparison function.
TT2 offered a number of different virtual data methods for doing different kinds of sort, etc. TT3 provides a couple of basic ones that accept functions as arguments and provides you with the syntax to create functions. This gives you more flexibility/power and makes the underlying implementation (and concept) that little bit simpler.
Alas, I broke the arrow operator last week (before I wrote these slides). One side-effect of using TT3 to syntax highlight the examples is that it refuses to parse code that is broken - so I had to comment it out just to be able to show you the example! This should be working again Real Soon Now.