Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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)[-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. :-)

<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

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (1)
As of 2023-10-04 23:00 GMT
Find Nodes?
    Voting Booth?

    No recent polls found