is it possible to add multiple values when using ....
# android
p
is it possible to add multiple values when using .map function to transform a list? for example:
Copy code
fun List<Line>.asLineEntityList() = map { line ->
    if (line.id.contains("-")){
        val line1 = line.id.split("-")[0]
        val line2 = line.id.split("-")[1]
        LineEntity(line1.toInt(), line.name+" ("+line1+")")
        LineEntity(line2.toInt(), line.name+" ("+line2+")")
    } else {
        LineEntity(line.id.toInt(), line.name)
    }
}
This is not working, it is adding only the second LineEntity and not two
c
Have you tried
foldLeft
?
e
more to the point,
flatMap
Copy code
flatMap { line ->
    if ('-' in line) {
        val (line1, line2) = line.split('-')
        listOf(
            LineEntity(line1.toInt(), "${line.name}($line1)"),
            LineEntity(line2.toInt(), "${line.name}($line2)"),
        )
    } else {
        listOf(LineEntity(line.id.toInt(), line.name))
    }
}
1
p
flatMap works, thanks