Range consolidation: Difference between revisions
Rename Perl 6 -> Raku, alphabetize, minor clean-up
m (→{{header|REXX}}: simplified code.) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 914:
[4, 3], [2, 1], [-1, -2], [3.9, 10] => -2..-1 1..2 3..10
[1, 3], [-6, -1], [-4, -5], [8, 2], [-6, -6] => -6..-1 1..8</pre>
=={{header|Perl 6}}==▼
{{works with|Rakudo|2018.12}}▼
In Perl 6, a Range is a first class object with its own specialized notation. Perl 6 Ranges allow for exclusion of the boundary numbers. This example doesn't since it isn't a requirement in this task. Much of the logic is lifted from the [[Set_of_real_numbers#Perl_6|Set_of_real_numbers]] task with simplified logic for the much simpler requirements.▼
Note: the output is in standard [https://docs.perl6.org/type/Range Perl 6 notation for Ranges].▼
<lang perl6># Union▼
sub infix:<∪> (Range $a, Range $b) { Range.new($a.min,max($a.max,$b.max)) }▼
# Intersection▼
sub infix:<∩> (Range $a, Range $b) { so $a.max >= $b.min }▼
multi consolidate() { () }▼
multi consolidate($this is copy, **@those) {▼
gather {▼
for consolidate |@those -> $that {▼
if $this ∩ $that { $this ∪= $that }▼
else { take $that }▼
}▼
take $this;▼
}▼
}▼
for [[1.1, 2.2],],▼
[[6.1, 7.2], [7.2, 8.3]],▼
[[4, 3], [2, 1]],▼
[[4, 3], [2, 1], [-1, -2], [3.9, 10]],▼
[[1, 3], [-6, -1], [-4, -5], [8, 2], [-6, -6]]▼
-> @intervals {▼
printf "%46s => ", @intervals.perl;▼
say reverse consolidate |@intervals.grep(*.elems)».sort.sort({ [.[0], .[*-1]] }).map: { Range.new(.[0], .[*-1]) }▼
}</lang>▼
{{out}}▼
<pre> [[1.1, 2.2],] => (1.1..2.2)▼
[[6.1, 7.2], [7.2, 8.3]] => (6.1..8.3)▼
[[4, 3], [2, 1]] => (1..2 3..4)▼
[[4, 3], [2, 1], [-1, -2], [3.9, 10]] => (-2..-1 1..2 3..10)▼
[[1, 3], [-6, -1], [-4, -5], [8, 2], [-6, -6]] => (-6..-1 1..8)▼
</pre>▼
=={{header|Phix}}==
Line 1,164 ⟶ 1,123:
((4 3) (2 1) (-1 -2) (3.9 10)) => ((-2 -1) (1 2) (3 10))
((1 3) (-6 -1) (-4 -5) (8 2) (-6 -6)) => ((-6 -1) (1 8))
▲</pre>
(formerly Perl 6)
▲{{works with|Rakudo|2018.12}}
▲In Perl 6, a Range is a first class object with its own specialized notation. Perl 6 Ranges allow for exclusion of the boundary numbers. This example doesn't since it isn't a requirement in this task. Much of the logic is lifted from the [[Set_of_real_numbers#Perl_6|Set_of_real_numbers]] task with simplified logic for the much simpler requirements.
▲Note: the output is in standard [https://docs.perl6.org/type/Range Perl 6 notation for Ranges].
▲<lang perl6># Union
▲sub infix:<∪> (Range $a, Range $b) { Range.new($a.min,max($a.max,$b.max)) }
▲# Intersection
▲sub infix:<∩> (Range $a, Range $b) { so $a.max >= $b.min }
▲multi consolidate() { () }
▲multi consolidate($this is copy, **@those) {
▲ gather {
▲ for consolidate |@those -> $that {
▲ if $this ∩ $that { $this ∪= $that }
▲ else { take $that }
▲ }
▲ take $this;
▲ }
▲}
▲for [[1.1, 2.2],],
▲ [[6.1, 7.2], [7.2, 8.3]],
▲ [[4, 3], [2, 1]],
▲ [[4, 3], [2, 1], [-1, -2], [3.9, 10]],
▲ [[1, 3], [-6, -1], [-4, -5], [8, 2], [-6, -6]]
▲-> @intervals {
▲ printf "%46s => ", @intervals.perl;
▲ say reverse consolidate |@intervals.grep(*.elems)».sort.sort({ [.[0], .[*-1]] }).map: { Range.new(.[0], .[*-1]) }
▲}</lang>
▲{{out}}
▲<pre> [[1.1, 2.2],] => (1.1..2.2)
▲ [[6.1, 7.2], [7.2, 8.3]] => (6.1..8.3)
▲ [[4, 3], [2, 1]] => (1..2 3..4)
▲ [[4, 3], [2, 1], [-1, -2], [3.9, 10]] => (-2..-1 1..2 3..10)
▲[[1, 3], [-6, -1], [-4, -5], [8, 2], [-6, -6]] => (-6..-1 1..8)
</pre>
|