https://kotlinlang.org logo
#announcements
Title
# announcements
i

ilya.gorbunov

10/15/2017, 7:36 PM
@dave08 There's no one-liner for that, but you can implement this operation with the help of `associateBy`:
Copy code
fun <F,S,K> setPartition(
				first: Iterable<F>,
				second: Iterable<S>,
				firstGetKey: F.() -> K,
				secondGetKey: S.() -> K
		): List<Triple<K, F?, S?>>
{
    val firstByKey = first.associateBy(firstGetKey)
    val secondByKey = second.associateBy(secondGetKey)
    val allKeys = firstByKey.keys + secondByKey.keys
    return allKeys.map { Triple(it, firstByKey[it], secondByKey[it]) }
}
d

dave08

10/16/2017, 2:13 AM
@ilya.gorbunov When the key is missing in first, or in second, the triple should have the key, the value of the object that exists and null for the one missing... That's what complicates things. Also wondering if first is a set of 50 to 100 objects and B is 5 to about 50 objects, in a framework like Vert.x with lots of requests coming in, and a pipeline of processing on the triple, would the coroutine version perform better? The stdlib version is almost always more readable and less error prone...
i

ilya.gorbunov

10/16/2017, 2:16 AM
d

dave08

10/16/2017, 2:19 AM
@ilya.gorbunov Oh, now I noticed that it would work fine.. I'm on my tablet so I can't check 😊 What about performance?
i

ilya.gorbunov

10/16/2017, 2:22 AM
I expect coroutine version to be slower. You can measure yourself how much by writing a benchmark.
d

dave08

10/16/2017, 2:24 AM
Thanks, I'll check!
4 Views