More functional programming on the horizon. This portion of Kotlin Koans demonstrated folding. I personally had never tackled a challenge like this so it took me more time to figure it out than the other problems. My job was to go through all customers and the products they ordered and reduce them down to a single set of objects. Here is the Kotlin code.
fun Shop.getSetOfProductsOrderedByEveryCustomer(): Set { // Return the set of products ordered by every customer return customers.fold(allOrderedProducts, { orderedByAll, customer -> orderedByAll.intersect(customer.orderedProducts) }) }
As usual, I tried to do the same problem in Java for comparison purposes, but I wasn’t able to figure it out! (If you know the solution, please leave it in the comments section!). I’ll have to admit that I am weak in some of the functional programming areas.
You can click here to see Part 21.
One thought on “Kotlin Koans—Part 22”