I was originally going to comment that I hated the style that you had used for the ST in your original code. But here you have it more or less as I would write it. Cool. :-) I find that emphasizing the bottom to top, or right to left effect of the chain, with as minimal obscuring parenthesis to be much more readable. And this is the point that most people get confused with about ST's and chaining list operators, the information flows right to left at first this can seem odd, but to me it makes total sense, in that the information flow involves an explicit or implicit assignment. Assignments are left associative, and flow to the left. However from the method invocation POV the right to leftness confounds their usual expectation of left to rightness. For instance in an oo language we might expect an ST to be written like (and i believe Ruby does it close to this)
@newfiles=@files.map([-s($_),$_]).sort($a->[0]<=>b->[0]).map(pop @$_);
but since we arent doing method invocation, but rather using list operators in an assignment statement we end up with the at first weird looking bottom to top, right to left. (here i was going to say "it wouldnt be hard to write an OO implementation", but I ended up writing a proof of concept instead, and I can say it wasnt that hard, except for some unexpected nonsense with sort.) Here it is:
package main;
my @x=Array->new( qw( bb cccc ddd aaaaa fffff qqq xxx iiiii ) )
->map('[length($_), $_ ]')
->sort('$b->[0] <=> $a->[0] || $a->[1] cmp $b->[1]')
->map('pop @$_');
print "@x";
So theres a perl OO implementation of the list operators in perl. :-) I kinda got carried away, sorry. :-) (The rest of this mail is what I originally intended to post, somehow this has become a major digression. My apologies.)
Also, I have a little personal rule with ST's. I always put the payload on the end. This way your code becomes
@sorted_by_size =
map { pop @$_ }
sort { $a->[0] <=> $b->[0] }
map { [-s $_ , $_ ] }
@files;
I do this because if you need to add sort criteria you can, but when you see the criteria in a list, like:
@sorted_by_ext_name_size =
map { pop @$_ }
sort {
$a->[0] cmp $b->[0] ||
$a->[1] cmp $b->[1] ||
$a->[2] <=> $b->[2] ||
0
}
map { [reverse(lc($_)=~/([^\\\/]+?)(\.[^.]+)?$/),-s($_),$_ ] }
@files=glob ('c:/winnt/*.*');
print join(",\n",@files);
print join(",\n",@sorted_by_ext_name_size);
You see all the slots. I suppose you could do it the otherway round, but then you have to say { shift @$_} or { $_->[0] } but I find { pop @$_ } is easier to type and somehow more suggestive of the underlying idea. The stylized way I wrote this is meant to make changing the criteria easier. By putting the 0 at the end, lines can be rearranged without worrying, and it has no real performance effect afaict. (For instance to remove the need for the reverse statement, all you need is a single cut and paste to reorder the sort criteria. Its to make this point that I put it in. :-)
Anyway, just thought Id throw my 2 cents in. I know other people have other preferences, and as long as they are readable I dont mind that much. :-)
---
demerphq
<Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
|