in reply to Re (tilly) 3: built-ins sometimes iterating over a list, sometimes not
in thread built-ins sometimes iterating over a list, sometimes not
I do this as well, my only issue with it is where to put the listor array, on the same line as the last transformation or the line below? Also I can never quite decide how to deal with 'blockless' transforms such as sort.
About your example from Ruby, are you allowed arbitray whitespace in between the transforms? ie could you write that as the following?@list=map {substr($_,1)} sort map {pack("CA*",length($_),$_)} @words; #or @list=map {substr($_,1)} sort map {pack("CA*",length($_),$_)} @words;
...even has the benefit of reading more naturally left to right!sorted = my_list.map {|i| [ i.split(/\|/)[2,4],i ]}. sort. map { |i| i[-1] }
Well, while it wouldnt be difficult to write a class that supported this type of syntax, I have to say that i'm not so sure that this is more natural or not. In fact, while I dont think it would be that hard to get used to or even that it would bother me, I would actually argue that it is less natural than the perl notation, which I hasten to add does seem a little odd at first, seemingly going against the convention in a program that lines are executed in order from top to bottom. This I believe is because it is easy to think in terms of statement execution rather than what is really happening, list assignment, or function chaining.
For the sake of clarity in my argument Id like to point out that normally values move from the right to left in statements. $x=$y; for instance involves evaluating the right side and placing its value in the container on the left side. With lists we extend the idea to that of 'pouring' values from the right side into the left side.
Now when I see @greets=map{"Hello $_!"}@names; the idea is extended so each value from the list at the right goes left, into the block and then left again into the array. And so on when we have longer chains. This also matches the way it would be written if these were all functions.
Whereas the method form would translate into a multistatement equivelent@list=map(substr($_,1),sort(map(pack("CA*",length($_),$_),@words)));
Which makes sense when written as multiple statements, but when written as one statement as you showed in ruby it actually seems to be unnatural. The result gets taken from the extreme right hand side and stuck in the container on the left side, which hardly seems more natural than the values flowing consistantly from right to left.my @tmp = map { pack("CA*",length($_),$_) } @words; my @tmp2= sort @tmp; my @list= map { substr($_,1)} @tmp2;
Just my $0.02 :-)
Yves / DeMerphq
--
Have you registered your Name Space?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re (tilly) 5: built-ins sometimes iterating over a list, sometimes not
by tilly (Archbishop) on Nov 09, 2001 at 01:49 UTC |