Colton Idle
11/16/2021, 5:24 PMpeople.addAll(
getAllPeople().map { Pair(it.first.orEmpty(), it.last.orEmpty()) }
)
I basically have a variable called people which is a Pair<>, and then I have the return of getAllPeople which is a Person.Paul Griffith
11/16/2021, 5:29 PMgetAllPeople()
only ever returns a 2 element list?getAllPeople()
returns List<Pair<String, String>>
and you want people
to be a List<Person>
?people
entirely in the map
call:
val people = getAllPeople().map { it.first to it.last }
Colton Idle
11/16/2021, 5:34 PMPaul Griffith
11/16/2021, 5:35 PMJoffrey
11/16/2021, 5:52 PMI basically have a variable called people which is a Pair<>, and then I have the return of getAllPeople which is a Person.You mean lists of those things, right?
Paul Griffith
11/16/2021, 5:54 PMwbertan
11/16/2021, 5:56 PMdata class People(val firstName: String?, val lastName: String?)
fun getAllPeople(): List<People> = emptyList()
fun asas() {
val people = mutableListOf<Pair<String, String>>()
// people.addAll(
// getAllPeople().map { Pair(it.firstName.orEmpty(), it.lastName.orEmpty()) }
// )
getAllPeople()
.map { it.firstName.orEmpty() to it.lastName.orEmpty() }
.let(people::addAll)
}
For me the reason would be to read first what is happening first, so basically getAllPeople
, then map
, the people.ddAll
.
But I would try to avoid the mutable list anyway, which would make it nicer to read from the begin.
Anyway, my only suggestion here would be if you could potentially avoid mutability and perhaps can use immutable data there?
Also what @Paul Griffith said above.Klitos Kyriacou
11/16/2021, 8:54 PMto
infix function to create the pair. The word "to" implies a key-value relationship. First name does not map to surname; it's just a pair of names. Therefore I would use Pair(firstName, lastName) even though it's slightly longer. It's more readable.rook
11/17/2021, 4:48 PMgetAllPeople().mapTo(people){...}
Colton Idle
11/17/2021, 9:58 PMJoffrey
11/17/2021, 10:01 PMval mappedList = sourceList.map { ... }
is preferable over creating a mutable list first and then using mapTo
Pair
. It would seem better to make the properties of Person
non nullable in the first place, or to create an equivalent data class with non-nullable fields if you really need the current Person
classPaul Griffith
11/17/2021, 10:03 PMmap
should be used to apply it. mapTo
is useful if, say, you have a collection with some elements already (that you can't fit into your starting collection) or something like that, but in the problem described, you don't need (and should avoid) creating the list and then populating it in two stepsColton Idle
11/17/2021, 10:08 PMJoffrey
11/17/2021, 10:11 PMPair
stuff then. The mapTo
vs map
still holds, though. In the case you mention, you should use map
.Colton Idle
11/17/2021, 10:18 PMJoffrey
11/17/2021, 10:30 PMmapTo
is what should be used instead to avoid an extra list. But our point was that if you don't already have a mutable list, you should probably not create a mutable list at all and just use the result of map directly