Skip to content
This repository has been archived by the owner on Aug 5, 2021. It is now read-only.

Collections Fold task should be changed #105

Open
NorthFury opened this issue Sep 12, 2017 · 2 comments
Open

Collections Fold task should be changed #105

NorthFury opened this issue Sep 12, 2017 · 2 comments

Comments

@NorthFury
Copy link

NorthFury commented Sep 12, 2017

I can't think of any scenario where I would solve the Collections Fold task using fold efficiently. I think it should be changed to showcase a realistic scenario for fold. This task could be reused as an example for reduce though. Here is a solution I think is good for this task.

    val customerProductSets = this.customers.map {
        it.orders.flatMap { it.products }.toSet()
    }

    if (customerProductSets.isEmpty()) {
        return emptySet<Product>()
    }

    return customerProductSets.reduce { acc, products -> acc.intersect(products) }

Or an even more optimized solution:

    if (this.customers.isEmpty()) return emptySet<Product>()

    return this.customers.asSequence()
            .map { it.orders.flatMap { it.products }.toSet() }
            .reduce { acc, products ->
                acc.intersect(products)
            }
@sultanahamer
Copy link

Also, the function name in N22Fold.kt is getSetOfProductsOrderedByEachCustomer which is confusing to the actual test testGetProductsOrderedByAllCustomers.

I inferred code to get all products ordered and test to get common product ordered by all customers.

@lewisevans
Copy link

That test is super confusing, definitely should be renamed to 'testGetProductsOrderedByAllCustomers' or 'testGetProductsOrderedByEveryCustomer'

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants