https://kotlinlang.org logo
m

Matheus

10/09/2018, 3:19 PM
Is there a more idiomatic way of "completing" a list? Simplifying what I have to do: I have a list of references (reference = date associated to a value) stored on an external service, and i need to write a function that returns a list with all references from a given period. The thing is, not all dates are guaranteed to have values, so I'll have to "complete" those with some default value. What i did, roughly is:
Copy code
private fun convert(recommendations: List<References>, period: Long): List<References> {
        val groupedValues: Map<LocalDate, List<References>> = requestFromExternalService(period)
                .asSequence()
                .groupBy { it.date() }

        return (period..0)
                .map { groupedValues[LocalDate.now().minus(it)] ?: Reference(date= LocalDate.now().minus(it), value = DEFAULT_VALUE)}
    }
Any thoughts? EDIT: Just noticed a couple of mistakes. I had to edit the code to simplify the explanation