Proudly debugging the system since 1981

Tag: functional programming

Fold

Mi ci è voluta più di qualche imprecazione per arrivare a questa implementazione :

fun Shop.getSetOfProductsOrderedByEveryCustomer(): Set<Product> {
    return customers.fold(allOrderedProducts, {
        orderedByAll, customer -> 
orderedByAll.minus( orderedByAll.filter { !customer.orderedProducts.contains(it) }) })
}

Salvo poi scoprire nelle soluzioni che esisteva il magico intersect

fun Shop.getSetOfProductsOrderedByEveryCustomer(): Set<Product> {
    // Return the set of products ordered by every customer
    return customers.fold(allOrderedProducts, {
        orderedByAll, customer ->
        orderedByAll.intersect(customer.orderedProducts)
    })
}

L’esercizio era questo : https://github.com/Kotlin/kotlin-koans/blob/master/src/ii_collections/_22_Fold_.kt

Comunque Kotlin è bellissimo.

Momenti di tristezza

Scoprire che le anonymous functions in php funzionano egreggiamente e in java, mediamente, è ancora una valle di lacrime (almeno per me, visto che java8 resta un miraggio).

Se vi andasse di approfondire qui e qui.

© 2024 b0sh.net

Tema di Anders NorenSu ↑