Flattening and such

A quick blog post to supplement discussion on IRC and (presumably) at the #APW hackathon.

I just did a couple of quick acks on the ABC codebase, and this is what I found:

Wynne:ABC colomon$ ack -Q .list
lib/ABC/Actions.pm
187:            for $bar.list {
202:                for $_.value.ast.list {

lib/ABC/KeyInfo.pm
72:                for $match<key-def><global-accidental>.list -> $ga {

The first one here I totally understand why I have to use .list. The remaining two, though, are classic examples of where my intuition says “These should return a list I can loop on” but instead I get an item. I guarantee that both times I only added the .list after minutes of head-scratching about my code failing in mysterious ways.

Wynne:ABC colomon$ ack -Q .flat
lib/ABC/Actions.pm
147:        make (@chords, @texts).flat;

t/01-regexes.t
421:    is $match<header_field>.flat.map({ .<header_field_name> }), "X T S M L K", "Got the right field names";
442:        is .<header_field>.flat.map({ .<header_field_name> }), "X T S M L K", "Got the right field names";

Here the first one really is flattening, but I think is a prime example of why we need an explicit list concatenation operation.

The remaining two … I’m not sure if I really meant .flat or .list there. Let me go look at the code…

Updated: Yes, .list gives me the same results on those last two. So in practice they are probably another example where I just tacked something randomish on to get things to behave the way I thought they should have by default.

Update to the update: Actually, for those last two, it works fine without .list or .flat. So that call was necessary at some point (or I wouldn’t have added it) but is no longer.

Another update: Okay, I tried removing the second two .lists from the first batch. Actions.pm line 202 can be removed without breaking any tests. KeyInfo.pm line 72 breaks loads of tests if the .list is removed.

Also, it appears my code is using @( $match ) and similar constructions much more often than it is using .list or .flat. Again the standard usage is somewhere I would have expected it to not be needed.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: