Is there a collection function to pair one element...
# codereview
d
Is there a collection function to pair one element with all others from the same list? For example: given
listOf(A,B,C)
I would like to end up with the following pairs:
A to B, A to C, B to C
Currently I got something like this but it feels non-idiomatic and inefficient...
Copy code
val users = listOf(User("A"), User("B"), User("C"))
	val chats = users.flatMap{ u1 ->
        users.filterNot { u1 == it }
            .map { u2 -> Chat(setOf(u1, u2)) }
    }.toSet() // filters out duplicates
b
Does the order matter?
Instinctively I would have done:
Copy code
val l = listOf("A", "B", "C", "D")
 l.flatMapIndexed { i, a ->
    l.subList(i+1, l.size).map { setOf(a,it) }
}
If your lists are not too big that should be fine to go over them like that many times
d
Totally forgot to reply No, order doesn't matter and the list is not that big. I like your solution more as it doesn't require
filterNot
nor the
toSet
to filter duplicates thanks!