mirror of
https://github.com/digital-asset/daml.git
synced 2024-09-20 09:17:43 +03:00
Add lazy flattening function to Relation (#4922)
* Add lazy flattening function to Relation CHANGELOG_BEGIN CHANGELOG_END * Restrict tests to non-empty relations and address https://github.com/digital-asset/daml/pull/4922#pullrequestreview-372050511
This commit is contained in:
parent
50a0e2eda3
commit
6f59832e45
@ -35,6 +35,12 @@ object Relation {
|
||||
|
||||
mutMap.toMap
|
||||
}
|
||||
|
||||
def flatten[A, B](relation: Relation[A, B]): Iterator[(A, B)] =
|
||||
for {
|
||||
kvs <- relation.iterator
|
||||
value <- kvs._2
|
||||
} yield (kvs._1, value)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -44,4 +44,14 @@ class RelationTest extends PropSpec with Matchers with PropertyChecks {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
property("flattening is the inverse of grouping for non empty relations") {
|
||||
forAll { m: Map[Int, Set[Char]] =>
|
||||
// an empty map and a map with exclusively empty values represent
|
||||
// the same relationship but the underlying structure is different
|
||||
whenever(m.values.forall(_.nonEmpty)) {
|
||||
flatten(m).toSeq.groupBy(_._1).mapValues(_.map(_._2).toSet) shouldEqual m
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user