Skip to content

Instantly share code, notes, and snippets.

@Serchinastico
Created September 17, 2018 16:27
Show Gist options
  • Save Serchinastico/d44c6321dee7d6312b0e6b7cacd8c8fb to your computer and use it in GitHub Desktop.
Save Serchinastico/d44c6321dee7d6312b0e6b7cacd8c8fb to your computer and use it in GitHub Desktop.
fold koan
// 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) { acc, customer ->
acc.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