Home > Blockchain >  Feedback on Lambdas
Feedback on Lambdas

Time:10-13

I was hoping someone could provide me some feedback on better/cleaner ways to do the following:

    val driversToIncome = trips
        .map { trip -> 
            // associate every driver to a cost (NOT UNIQUE)
            trip.driver to trip.cost }
        .groupBy (
            // aggregate all costs that belong to a driver
            keySelector = { (driver, _) -> driver },
            valueTransform = { (_, cost) -> cost }
        )
        .map { (driver, costs) -> 
            // sum all costs for each driver
            driver to costs.sum() }
        .toMap()

CodePudding user response:

You can do it like this:

val driversToIncome = trips
    .groupingBy { it.driver }
    .fold(0) { acc, trip -> acc   trip.cost }

It groups trips by driver and while grouping it sums costs per each driver separately.

Note that groupingBy() does not do anything on its own, it only prepares for the grouping operation. This solution avoids creating intermediary collections, it does everything in a single loop.

Then fold() calls the provided lambda sequentially on each item belonging to the specific group. Lambda receives a result from the previous call and it provides a new result (result is called accumulator). As a result, it reduces a collection of items to a single item.

You can read more about this kind of transformations in documentation about Grouping and Aggregation. Also, they aren't really inventions of Kotlin. Such operations exist in other languages and data transformation tools, so you can read about it even on Wikipedia.

  • Related