Nir
01/23/2021, 10:05 PMNir
01/23/2021, 10:07 PMNir
01/23/2021, 10:07 PMfun List<Food>.toAllergenPossibilities() = asSequence()
.flatMap { food -> food.allergens.map { it to food.ingredients } }
.groupingBy { (allergen, _) -> allergen }
.foldTo(mutableMapOf(),
initialValueSelector = { _, (_, ingredients) -> ingredients.toMutableSet() }) { _, set, (_, ingredients) ->
set.apply { retainAll(ingredients) }
}
Nir
01/23/2021, 10:08 PMNir
01/23/2021, 10:44 PMfun List<Food>.toAllergenPossibilities() = asSequence()
.flatMap { food -> food.allergens.map { it to food.ingredients } }
.groupingBy { (allergen, _) -> allergen }
.mapValues { it.second }
.foldTo(mutableMapOf(),
initialValueSelector = { _, ingredients -> ingredients.toMutableSet() }) { _, set, ingredients ->
set.apply { retainAll(ingredients) }
}
Nir
01/23/2021, 10:48 PMNir
01/23/2021, 11:56 PMNir
01/24/2021, 12:38 AMNir
01/24/2021, 12:39 AMilya.gorbunov
01/25/2021, 2:43 PMIterator<Pair<T, K>>
requires to allocate a pair for every element, this would significantly increase memory traffic of groupingBy
operations.Nir
01/25/2021, 2:46 PMNir
01/25/2021, 2:47 PMvalueOf
also a conscious decision for perf reasons or was it simply not considered?