Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Re: Re: (OT) The Schwartzian Transform in Java

by demerphq (Chancellor)
on Jun 15, 2003 at 13:15 UTC ( #266021=note: print w/replies, xml ) Need Help??


in reply to Re: Re: (OT) The Schwartzian Transform in Java
in thread (OT) The Schwartzian Transform in Java

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...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://266021]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2023-10-04 20:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?