Given a sparse mapping represented as a function (sparseMapping) from the input domain A to a range of optional values (Option[B]), convert to a dense format of the appropriate container type.
Given a sparse mapping represented as a function (sparseMapping) from the input domain A to a range of optional values (Option[B]), convert to a dense format of the appropriate container type. This is done by composing f = g ∘ sparseMapping, where g is defined as:
⎧ y if x = Some(y) g(x) = ⎨ ⎩ whenMissing otherwise
and mapping f over denseDomain.
An example:
val denseDomain = 3 to 6 val sparseKeys = Array(4, 6) val sparseVals = Iterable(1, 2) val sparseMapping = (sparseKeys zip sparseVals).toMap.get _ // Int => Option[Int] val whenMissing = 0 val result = densify(denseDomain, sparseMapping, whenMissing) val expected = Vector(0, 1, 0, 2) // NOTE: The resulting container type is a Vector because of the type of denseDomain. assert(result.getClass.getCanonicalName == "scala.collection.immutable.Vector") assert(result == expected)
type of the dense domain
type of the dense range
the container type of the input. An attempt is made to make the output container type as close as possible to the input container type. While this is a FilterMonadic, it really only needs to be a functor (because we only care about the map function. Flatmap doesn't matter).
the resulting type implementation.
the domain of dense values provided as the preimage to the sparse mapping specified by the parallel iterables.
a mapping from the input domain to an option of the output domain. Once composed with whenMissing this map all values of the domain an appropriate value in the range of the function.
the resulting value when an item from denseDomain isn't contained in sparseKeys.
a CanBuildFrom object
the dense image of the mapping from the dense domain, using sparseMapping and whenMissing.
Given a sparse mapping represented as a map (sparseFeatures), convert to a dense format of the appropriate container type.
Given a sparse mapping represented as a map (sparseFeatures), convert to a dense format of the appropriate container type. This is done by creating a map based on sparseFeatures with a default value specified by whenMissing and mapping the new map's apply function over denseDomain.
An example:
val denseDomain = 3 to 6 val sparseKeys = Array(4, 6) val sparseVals = Iterable(1, 2) val sparseFeatures = (sparseKeys zip sparseVals).toMap val whenMissing = 0 val result = densify(denseDomain, sparseFeatures, whenMissing) val expected = Vector(0, 1, 0, 2) // NOTE: The resulting container type is a Vector because of the type of denseDomain. assert(result.getClass.getCanonicalName == "scala.collection.immutable.Vector") assert(result == expected)
type of the dense domain
type of the dense range
the container type of the input. An attempt is made to make the output container type as close as possible to the input container type. While this is a FilterMonadic, it really only needs to be a functor (because we only care about the map function. Flatmap doesn't matter).
the resulting type implementation.
the domain of dense values provided as the preimage to the sparse mapping specified by the parallel iterables.
a map from the domain to range
the resulting value when an item from denseDomain isn't contained in sparseKeys.
a CanBuildFrom object
the dense image of the mapping from the dense domain, using sparseMapping and whenMissing.
Given a sparse mapping represented as key-value pairs in parallel iterables, convert to a dense format of the appropriate container type.
Given a sparse mapping represented as key-value pairs in parallel iterables, convert to a dense format of the appropriate container type. This is done by mapping over the denseDomain, determining whether a key exists in sparseKeys. If a key exists, the associated value is substituted; otherwise, substitute with the whenMissing value.
In the event of duplicate keys, for each pair of duplicate keys, the key-value pair associated with the second encountered key will be used.
An example:
val denseDomain = 3 to 6 val sparseKeys = Array(4, 6) val sparseVals = Iterable(1, 2) val sparseValOptions = sparseVals.map(Option.apply) val whenMissing = None val result = densify(denseDomain, sparseKeys, sparseValOptions, whenMissing) val expected = Vector(None, Some(1), None, Some(2)) // NOTE: The resulting container type is a Vector because of the type of denseDomain. assert(result.getClass.getCanonicalName == "scala.collection.immutable.Vector") assert(result == expected)
Notice in the example above that when all of the keys are contained in denseDomain, then when sparsifying the results of densify, we get back the original sparseVals:
assert(result.flatten == sparseVals)
type of the dense domain
type of the dense range
the container type of the input. An attempt is made to make the output container type as close as possible to the input container type. While this is a FilterMonadic, it really only needs to be a functor (because we only care about the map function. Flatmap doesn't matter).
the resulting type implementation.
the domain of dense values provided as the preimage to the sparse mapping specified by the parallel iterables.
the keys in the sparse mapping (NOTE: (sparseKeys(i), sparseVals(i)) represents a key-value pair)
the values in the sparse mapping
the resulting value when an item from denseDomain isn't contained in sparseKeys.
a CanBuildFrom object
the dense image of the mapping from the dense domain, using the mapping created by sparseKeys, sparseVals and whenMissing.
Provides ways to transform items from between dense and sparse domains.