Range consolidation: Difference between revisions

m
(→‎{{header|Wren}}: Now uses new core library method.)
Line 711:
 
<lang dyalect>type Pt(s, e)
 
func Pt.minMin() => min(this::.s, this::.e)
func Pt.maxMax() => max(this::.s, this::.e)
func Pt.toStringToString() => "(\(this::.s), \(this::.e))"
 
let rng = [
[ Pt(1.1, 2.2) ],
Line 723:
[ Pt(1.0, 3.0), Pt(-6, -1), Pt(-4, -5), Pt(8, 2), Pt(-6, -6) ]
]
 
func overlap(left, right) =>
right.maxMax() >= left.minMin()
when left.maxMax() > right.maxMax()
else left.maxMax() >= right.minMin()
func consolidate(left, right) => Pt(min(left.minMin(), right.minMin()), max(left.maxMax(), right.maxMax()))
func normalize(range) => Pt(range.minMin(), range.maxMax())
for list in rng {
var z = list.lenLength() - 1
while z >= 1 {
for y in (z - 1)^-1..0 when overlap(list[z], list[y]) {
list[y] = consolidate(list[z], list[y])
break list.removeAtRemoveAt(z)
}
z -= 1
}
for i in list.indicesIndices() {
list[i] = normalize(list[i])
}
list.sortSort((x,y) => x::.s - y::.s)
print(list)
}</lang>
Anonymous user