Skip to content

Commit

Permalink
Use reduceLeft for union on Interval
Browse files Browse the repository at this point in the history
  • Loading branch information
kwalcock committed Mar 17, 2023
1 parent b7c0b70 commit 2174bb2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/scala/ai/lum/common/Interval.scala
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ object Interval {
def union(col: Seq[Interval]): Interval = {
val sorted = col.sorted
try {
sorted.reduceRight(_ union _)
sorted.reduceLeft(_ union _)
} catch {
case _: IllegalArgumentException =>
throw new IllegalArgumentException("gap in intervals: " + sorted)
Expand Down
17 changes: 17 additions & 0 deletions src/test/scala/ai/lum/common/TestInterval.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ai.lum.common

import org.scalatest._

class TestInterval extends FlatSpec with Matchers {

behavior of "Interval"

it should "perform unions in large quantities" in {
val count = 1000000
val intervals = 1.to(count).map { length => Interval.ofLength(length - 1, length) }.toList
val expectedInterval = Interval.open(0, count + count - 1)
val actualInterval = Interval.union(intervals)

actualInterval should be (expectedInterval)
}
}

0 comments on commit 2174bb2

Please sign in to comment.