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:
Stefano Baghino 2020-03-10 18:39:45 +01:00 committed by GitHub
parent 50a0e2eda3
commit 6f59832e45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View File

@ -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)
}
}

View File

@ -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
}
}
}
}