I realize this is a very old thread, but I just found it and was wondering about the efficiency of this.
I understand you could have a long, complicated expression with no room for if statements, with
[ $x => $y ] -> [ $x <= $y ]
as just a small portion of that expression and the example of my $highestvalue is just a simple example.
However if you were in a loop, and just wanted to keep overwriting $highestvalue every time you find a higher value, wouldn't it be better to write it this way?:
my $x = 0;
foreach $y (@lots_of_values){
$x = $y if $y > $x;
}
return $x;
Is that just as efficient as writing it this way?:
my $x = 0;
foreach $y (@lots_of_values){
$x = [ $x => $y ] -> [ $x <= $y ];
}
return $x;
The second way seems overly complicated in this case. | [reply] [d/l] [select] |
| [reply] |
It makes for a damn good interview question. Asking someone to explain it tells you a lot about how deeply they understand several concepts in Perl. So there's another use :D
| [reply] |
...which is same as $max = $x <= $y ? $y : $x; and w/o dereferencing. (No, i did not benchmark.) | [reply] [d/l] |
$highestvalue = [ $x => $y ] -> [ $x <= $y ]
Anybody can explanin me this code? I'm a bit confused ;-)
Uksza | [reply] [d/l] |
my $ary = ( $x, $y );
my $aryRef = \ @ary;
And $x <= $y is just a boolean expression that will give a false (0) result if $x > $y and true value (1) otherwise.
$aryRef->[ 0/1 ] will return either $ary[ 0 ] or $ary[ 1 ].
Putting it all together, you get an expression that will construct an anonymous array containing $x, $y, then dereferences that anonmous array and uses the boolean result of the comparison to select the greater of the two values before assigning it to the scalar.
"But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
"Think for yourself!" - Abigail
"Time is a poor substitute for thought"--theorbtwo
"Efficiency is intelligent laziness." -David Dunham
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
| [reply] [d/l] [select] |
Hey!
I understand!! ;-)
So, in exmaple:
my $x = 5
my $y = 10
my $highestvalue = [ $x => $y ] -> [ $x <= $y ]
#we have @aray(5,10) and because (5 <= 10) it returns 1, so
$highestvalue = aray[1]
thats mean 10 in this example, right?
Eh, nice trick... ;-)
Thanks for you reply.
Uksza
| [reply] [d/l] |