Skip to content

Instantly share code, notes, and snippets.

@kyryloz
Created May 29, 2017 19:00
Show Gist options
  • Save kyryloz/1c04b0971d582e83effd3c7031500f57 to your computer and use it in GitHub Desktop.
Save kyryloz/1c04b0971d582e83effd3c7031500f57 to your computer and use it in GitHub Desktop.
// Return the set of products that were ordered by every customer
fun Shop.getSetOfProductsOrderedByEveryCustomer(): Set<Product> {
val allProducts = customers.flatMap { it.orders.flatMap { it.products }}.toSet()
return customers.fold(allProducts, {
orderedByAll, customer ->
orderedByAll.intersect(customer.orders.flatMap { it.products }.toSet())
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment