caedes has asked for the wisdom of the Perl Monks concerning the following question:
sub intersect { my ($a,$b,$c,$d) = @_; my ($n1,$n2) = ([$a,$b],[$c,$d]); if($a > $c){ my $t = $n1; $n1 = $n2; $n2 = $t; } ($a,$b,$c,$d) = (@n1,@n2); return ($b-$c)*($b>$c)-($b-$d)*($b>$d); }
which I later compressed into this 2-line obfuscation:
sub intersect3 { my ($a,$b,$c,$d) = map {($_->[0],$_->[1])} sort { $a->[0] <=> $b-> +[0] } ([@_[0,1]],[@_[2,3]]); return ($b-$c)*($b>$c)-($b-$d)*($b>$d); }
I kept looking for a more elegant/shorter/cooler way to do this but have gotten no farther after many hours of meditation. I wonder if some of the more enlightened among us can offer further ideas?
-caedes
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Finding the intersect of two ranges
by BrowserUk (Patriarch) on Feb 03, 2003 at 19:04 UTC | |
by caedes (Pilgrim) on Feb 04, 2003 at 02:21 UTC | |
by BrowserUk (Patriarch) on Feb 04, 2003 at 09:40 UTC | |
|
Re: Finding the intersect of two ranges
by broquaint (Abbot) on Feb 03, 2003 at 18:04 UTC | |
by caedes (Pilgrim) on Feb 04, 2003 at 02:14 UTC |