Destructuring extravaganza

A few months back I added support for destructuring assignment and tuples to Ioke. Since Ioke’s assignment is just a regular method call, this was actually fairly easy to do. The end result is that you can do things like (x, y) = (13, 14). You can also do more interesting things, such as ((x, y), (x2, y2)) = [[1,2],[3,4]]. Notice that the right hand side is not a tuple anymore, but a list. Anything that can be turned into a tuple using the asTuple method can be on the right hand side, or an item in a recursive destructuring.

All this functionality makes code slightly more readable. But last week I decided to add support for eachCons and eachSlice, and suddenly I realized that destructuring would be very nice to have not only in the explicit assignment case, but also in cases where you want to pick apart the arguments to an enumerable or sequence method. So I added those, which means that suddenly lots of code becomes much more simple.

Short story, in all Sequence and Enumerable methods, at every place where you could put an argument name, you can now put a destructuring statement instead. Let’s take a look at an example:

Point = Origin with(asTuple: method((x, y, z)))

points = [
Point with(x: 42, y: 14, z: -1),
Point with(x: 20, y: 0, z: 444),
Point with(x: 31, y: 646, z: 3),
Point with(x: 456, y: 14, z: 12)
]

distances1 = points consed map(obj,
((obj x) * (obj x) +
(obj y) * (obj y) +
(obj z) * (obj z)) sqrt)

distances2 = points consed map(
((x1,y1,z1), (x2,y2,z2)),
(x1*x2 + y1*y2 + z1*z2) sqrt)

distances1 inspect println
distances2 inspect println

This code first creates a Point that can be coerced into a tuple of x, y and z coordinates. We then create a list of Points with different coordinates. We then want to calculate the three distances between the four points. We do this in two ways, using the old method and then using destructuring. The method consed is a sequence version of eachCons. The default cons length is 2, so this will yield three entries with two points in each. We then call map on the sequence. We will get a List of two entries, where each entry is a point. Finally we use Pythagoras to calculate the distance.

The second version is very similar – the only difference is that instead of using the square brackets to index into the lists, we instead give a pattern. This pattern contains two patterns, and the variable names inside of it will be bound to the right parts of each point.

At least in my mind, the destructured syntax is much more readable than the original one. And remember, this works for anything that can be turned into a tuple, which means you can use it on any Enumerable – you can use it on a Pair (such as what a Dict will yield) or any thing you would want to add asTuple to on your own.

1. Sam Aaron

Great stuff!

BTW, how would I go about destructuring each? Consider:

[(0, 0), (0, 1), (0, 2)] each((x,y), x println) ;=> attempts to fetch x from outer scope

March 3rd, 2010

2. Idran

As far as I could tell there’s not support for “variadic destructing” or what you’d might call it. I’m thinking along the lines of e.g. Perl’s wantarray (only useful for single vs many) or Perl’s Want modules that can tell you the number of wanted return values. Matlab makes heavy use of variables number of return values, and Perl’s wantarray can be pretty useful at times. I’m thinking maybe it can be useful in a more general case applying to any destructable data structure. Are there any plans/ideas for this in Ioke/Seph?

March 2nd, 2012