Better solution for Sequences Koan?

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?

It might be of interest to link to the exercise itself.

It seems to me that we should turn this into a sequence earlier. My implementation was:
orders.asSequence().flatMap(Order::products)

I agree, if you’re to convert an Iterable to a Sequence you should do so before doing any operations on it.

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.

Indeed, taking the extra step to convert a list to a sequence here doesn’t seem useful, considering each item will be operated on on a one by one basis either way.

I suppose the reason was that there was no such overload of flatMap when this koan was written.

I’ve submitted your suggestions in this PR: https://github.com/Kotlin/kotlin-koans-edu/pull/32/files.