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:

use strict; use warnings; package Array; use Data::Dumper; sub new { my $class=shift; my $self=bless \@_,$class; $self }; sub grep { my ($self,$code,$pack)=@_; my $ret=defined wantarray ? ref($self)->new() : $self; $pack||=caller; my $sub=ref $code ? $code : eval"package $pack; sub{$code}" or die "grep failed eval: '$code'\n$@"; @$ret=grep($sub->(),@$self); return wantarray ? @$ret : $ret } sub map { my ($self,$code,$pack)=@_; $pack||=caller; my $ret=defined wantarray ? ref($self)->new() : $self; my $sub=ref $code ? $code : eval"package $pack; sub{$code}" or die "map failed eval: '$code'\n$@"; @$ret=map($sub->(),@$self); wantarray ? @$ret : $ret } sub sort { my ($self,$code,$pack)=@_; my $ret=defined wantarray ? ref($self)->new() : $self; $pack||=caller; if ($code) { # we play games with namespaces here because $a and $b + are package # variables and we need to do the sort in the same pac +kage as the # $a and $b are used in. @$ret=eval qq({ package $pack; local *__sorter=*__sorter=ref \$code ? \$code : eval "sub{\$code}" or die "Sort failed eval: '\$code'\n\$@"; sort __sorter \@\$self; }) or die $@; } else { @$ret=sort @$self; } wantarray ? @$ret : $ret } sub reverse { my $self=shift; my $ret=defined wantarray ? ref($self)->new() : $self; @$ret=reverse @$self; wantarray ? @$ret : $ret }
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...


In reply to Re: Re: Re: (OT) The Schwartzian Transform in Java by demerphq
in thread (OT) The Schwartzian Transform in Java by djantzen

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.