I am working my way through the Koans and had a different implementation for the Sequence Koan than the “suggested” answer.
First, the “suggested” implementation for the Customer.getOrderedProducts is:
orders.flatMap(Order::products).asSequence()
It seems to me that we should turn this into a sequence earlier. My implementation was:
orders.asSequence().flatMap(Order::products)
Now the Sequence version of flatMap takes either an Iterable or a Sequence so I’m guessing that there would be no reason to have written:
orders.asSequence().flatMap { it.products.asSequence() }
as I expect that the Iterable implementation will handle the case essentially the same way.
This means that the “suggested” implementation for findMostExpensiveProductBy which is:
return customer
.orders
.asSequence()
.filter(Order::isDelivered)
.flatMap { it.products.asSequence() }
.maxByOrNull(Product::price)
probably should have been written as:
return customer
.orders
.asSequence()
.filter(Order::isDelivered)
.flatMap(Order::products)
.maxByOrNull(Product::price)
as, again, turning the product list into a sequence for flatMap is unnecessary.
So, am I missing something here?