Guaranteed order of groupingBy aggregate Map in Kotlin - kotlin

I am using a groupingBy and then aggregate to process a list of items and group them into pairs. The order of the input list is important, and the resulting list should also be ordered.
aggregate returns a Map indicating that the processing order is not preserved.
Example:
data class Foo(val a: Int, val b: String)
val r1 = Foo(1, "a")
val r2 = Foo(1, "b")
val r3 = Foo(2, "c")
val r4 = Foo(2, "d")
val r5 = Foo(2, "e")
val records = listOf(r1, r2, r3, r4, r5)
val result = records.groupingBy { it.a }
.aggregate { _, acc: Pair<Int, List<String>>?, element, _ ->
if (acc == null) {
element.a to listOf(element.b)
} else {
acc.first to acc.second + element.b
}
}.values.toList()
println(result)
Returns [(1, [a, b]), (2, [c, d, e])] - which is the correct order, but is it guaranteed?
PLEASE NOTE that this is a simple example that may be sorted by a - but that is NOT the solution, as the records sorting is provided by the user of the function.

aggregate's documentation does not guarantee this, but mutableMapOf's documentation does guarantee it. So I would use aggregateTo(mutableMapOf()) { /*...*/ } to future-proof your code. The fact that they don't guarantee it means either they forgot to, or they deliberately want to keep open the option of switching the underlying implementation for performance in the future.

If you want to be sure that the Pairs of the aggregation result are ordered, you could do:
fun main() {
data class Foo(val a: Int, val b: String)
val r1 = Foo(2, "a")
val r2 = Foo(2, "b")
val r3 = Foo(1, "c")
val r4 = Foo(1, "d")
val r5 = Foo(2, "e")
val records = listOf(r1, r2, r3, r4, r5)
val result = records.groupingBy { it.a }
.aggregate { _, acc: Pair<Int, List<String>>?, element, _ ->
if (acc == null) {
element.a to listOf(element.b)
} else {
acc.first to acc.second + element.b
}
}.values.sortedBy {it.first}.toList()
println(result)
}
This would output [(1, [c, d]), (2, [a, b, e])].

Since you have the original list you could sort the list after grouping using the index of the elements in the original list.
data class Foo(val a: Int, val b: String)
fun main() {
val r1 = Foo(2, "a")
val r2 = Foo(2, "b")
val r3 = Foo(1, "c")
val r4 = Foo(1, "d")
val r5 = Foo(2, "e")
val records = listOf(r1, r2, r3, r4, r5)
// shuffle the records - just to test to make sure the sorting works
val shuffledRecords = records.shuffled()
val result = shuffledRecords.groupingBy { it.a }
.aggregate { _, acc: Pair<Int, List<String>>?, element, _ ->
if (acc == null) {
element.a to listOf(element.b)
} else {
acc.first to acc.second + element.b
}
}
.values
// after grouping, ensure the values are sorted using the ordering from the original list
.map { (k, values) -> k to values.sortedBy { value -> records.indexOfFirst { it.b == value } } }
println(result)
}
Even though the records were shuffled, the output is still ordered
[(1, [c, d]), (2, [a, b, e])]

Related

How to destructure Kotlin nested pairs with forEach

I need to destructure Kotlin nested pairs. How can I do this simply without using pair.first/pair.second?
val chars = listOf('A', 'B', 'C')
val ints = listOf(1, 2, 3)
val booleans = listOf(true, false, false)
val cib: List<Pair<Pair<Char, Int>, Boolean>> = chars.zip(ints).zip(booleans)
cib.forEach { ((c, i), b) -> // compile error
println("$c $i $b")
}
Not sure if there really is a way of desctructuring a Pair<Pair<*,*>> straight away, but you could do this:
cib.forEach { (pair, b) ->
val (c, i) = pair
//do stuff with c, i, b
}

Find the list item of Pairs with the minimum value

val index = listOf("abc", "def", "ghi", "jkl", "mno")
.mapIndexed { index, v ->
var t = 0
var p = 0
for (s in v) {
t += ("deh".get(p++).toInt() - s.toInt()).absoluteValue
}
Pair(index, v)
}
.minOf {
val iterator = iterator<Pair<Int, String>>(it)
if (!iterator.hasNext()) throw NoSuchElementException()
var minValue = iterator.next().second
while (iterator.hasNext()) {
val v = selector(iterator.next())
minValue = minOf(minValue, v)
}
return minValue
}
This is an alternative solution and works, but I am wondering if the solution can be done using mapOf as shown above?
val index = listOf("abc", "def", "ghi", "jkl", "jad", "jaa", "mno")
.mapIndexed { index, v ->
var t = 0
var p = 0
for (s in v) {
t += ("jac".get(p++).toInt() - s.toInt()).absoluteValue
}
Pair(index, t)
}.toSortedSet(compareBy { it.second })
.first()
I create a map of Pairs and I want to find the index of the map item where the Pair with the value (the second item in the pair) is the lowest value (minimum) of all the pairs. If possible, I would like to use the minOf function. The first example above will not compile because of bugs in the minOf function. Not sure how to iterate over the map of Pairs.
You can use minBy {} to get the minimum value from a collection, although often it's safer to use minByOrNull {} in case no minimal value can be computed (which could happen if the list is empty).
import kotlin.math.absoluteValue
fun main() {
val minElement = listOf("abc", "def", "ghi", "jkl", "jad", "jaa", "mno")
.minByOrNull { v ->
var t = 0
var p = 0
for (s in v) {
t += ("jac".get(p++).toInt() - s.toInt()).absoluteValue
}
t
}
println(minElement)
}
jad
Run in Kotlin Playground
If you also want to find the index of the minimal value, then you can use withIndex(), which will pair each list element with its index.
import kotlin.math.absoluteValue
fun main() {
val minIndexedElement = listOf("abc", "def", "ghi", "jkl", "jad", "jaa", "mno")
.withIndex() // adds the index to each element
.minByOrNull { (_, v) ->
var t = 0
var p = 0
for (s in v) {
t += ("jac".get(p++).toInt() - s.toInt()).absoluteValue
}
t
}
println(minIndexedElement)
}
IndexedValue(index=4, value=jad)
Run in Kotlin Playground
Another solution would be to extract the character codes from "jar" and from each item, and then to zip the two code lists. zip allows for a transform closure in which the calculation with the two codes can be made. After that sum() gives the wanted value.
data class Result(val index: Int, val string: String, val computedValue: Int)
val list = listOf("abc", "def", "ghi", "jkl", "jad", "jaa", "mno")
val result = list
.mapIndexed { idx, str ->
val codes1 = "jac".toCharArray().map { it.code }
val codes2 = str.toCharArray().map { it.code }
val computed = codes1.zip(codes2) { code1, code2 -> (code1 - code2).absoluteValue }.sum()
Result(idx, str, computed)
}
.minByOrNull { it.computedValue }
println(result) // Output: Result(index=4, string=jad, computedValue=1)
Instead of the helper data class Result a Triple instance could be used:
...
Triple(idx, str, computed)
}
.minByOrNull { it.third }
// Output: (4, jad, 1)
Or if the calculated value is not needed, it could be dropped like that:
...
?.let { it.first to it.second }
// Output: (4, jad)

Transform Kotlin List to certain size by padding or concatenating the last n elements

I often need to either shorten or pad a List to a certain amount of entries. For that I use a function like this:
fun List<String>.compactOrPadEnd(size: Int): List<String> {
if (this.size < size)
return this + List(if (this.size < size) size - this.size else 0) { "" }
else
return this.subList(0, size - 1) + this.subList(size - 1, this.size).joinToString("")
}
val list0 = emptyList<String>()
val list1 = listOf("A")
val list2 = listOf("A", "B")
val list3 = listOf("A", "B", "C")
val list4 = listOf("A", "B", "C", "D")
val list5 = listOf("A", "B", "C", "D", "E")
val size = 3
list0. compactOrPadEnd(size).onEach(::println) // [ , , ]
list1. compactOrPadEnd(size).onEach(::println) // [A, , ]
list2. compactOrPadEnd(size).onEach(::println) // [A, B, ]
list3. compactOrPadEnd(size).onEach(::println) // [A, B, C]
list4. compactOrPadEnd(size).onEach(::println) // [A, B, CD]
list5. compactOrPadEnd(size).onEach(::println) // [A, B, CDE]
The above code is more readable with separate functions:
fun List<String>.padEnd(size: Int) =
this + List(if (this.size < size) size - this.size else 0) { "" }
fun List<String>.compact(size: Int) =
this.subList(0, size - 1) + this.subList(size - 1, this.size).joinToString("")
fun List<String>.compactAndPadEnd(size: Int): List<String> =
if (this.size < size) padEnd(size) else compact(size)
I find both solutions too clumsy. I went through all the built-in collection functions to come up with something simpler, but to no avail.
Small side question: is there a better name than compactAndPadEnd?
You can write this as one case (i.e. without if-else) if you take advantage of the fact that joinToString happens to return your pad element "" when the list is empty.
fun List<String>.resizeEnd(size: Int): List<String> =
this.subList(0, min(size - 1, this.size)) +
this.subList(min(size - 1, this.size), this.size).joinToString("") +
List(max(0, size - this.size - 1)) { "" }
Notice that I'm creating a list of size size - this.size - 1 at the end. -1 because one of the empty strings would have been the one returned by joinToString("").
If you don't mind drop and take creating extra lists, you can make it shorter:
fun List<String>.resizeEnd(size: Int): List<String> =
this.take(size - 1) +
this.drop(size - 1).joinToString("") +
List(max(0, size - this.size - 1)) { "" }
You can also generalise this to:
fun <T> List<T>.resizeEnd(size: Int, padElement: T, foldFunction: (T, T) -> T): List<T> =
this.subList(0, min(size - 1, this.size)) +
this.subList(min(size - 1, this.size), this.size).fold(padElement, foldFunction) +
List(max(0, size - this.size)) { padElement }
But the catch is that padElement must be the identity for foldFunction.
As an alternative, you could create a List of fixed size using an initializer function combined with a when to initialize each item:
fun List<String>.resize(size: Int) = List(size) {
when {
it == size - 1 && this.size > size -> this.subList(size - 1, this.size).joinToString("")
this.size > it -> this[it]
else -> ""
}
}
I'm not entirely sure if this is any less clumsy, I suppose that depends on personal preference.
I see some slight clean-up you can do on your function. You're redundantly checking this.size < size, you could lift return out of the condition branches, and you could use take/drop for brevity.
fun List<String>.compactOrPadEnd(size: Int): List<String> {
return if (this.size < size)
this + List(size - this.size) { "" }
else
take(size - 1) + drop(size - 1).joinToString("")
}
Personally, I'd call it concatEndOrPad.

Kotlin: mutable map of mutable list won't update the list

(Kotlin newbie here) I have a text file with rows that look like these:
1-1-1
1-1-2
1-1-3
2-1-1
2-1-2
etc.
I have to transform these data to a map where the key is the first 2 elements and the value is a list of the third elements that that match the key. For example, the above records will transform into this JSON:
1-1: [1, 2, 3]
2-1: [1, 2]
etc.
I'm unable to increment the list. Here's a simplified version, I get stuck on the "else":
fun main () {
val l1 = mutableListOf("1-1-1", "1-1-2", "1-1-3", "2-1-1", "2-1-2")
val m = mutableMapOf<String, List<Int>>()
for (e in l1) {
val c = e.split("-")
val key = "${c[0]}-${c[1]}"
if (m[key] == null) m[key] = listOf(c[2].toInt())
else println("How do I append to the list?")
}
println(m)
}
Output:
{1-1=[1], 2-1=[1]}
But I want:
{1-1=[1, 2, 3], 2-1=[1, 2]}
Thank you (comments about idiomatic form are welcome!)
If we continue to follow your strategy, what you need is for the value type to be a MutableList. Then you can add to the existing MutableList when there's already an existing list for that key:
fun main() {
val l1 = mutableListOf("1-1-1", "1-1-2", "1-1-3", "2-1-1", "2-1-2")
val m = mutableMapOf<String, MutableList<Int>>()
for (e in l1) {
val c = e.split("-")
val key = "${c[0]}-${c[1]}"
if (m[key] == null) m[key] = mutableListOf(c[2].toInt())
else m[key]!!.add(c[2].toInt())
}
println(m)
}
This can be more natural using getOrPut(). It returns the existing MutableList or creates one and puts it in the map if it's missing. Then we don't have to deal with if/else, and can simply add the new item to the list.
fun main() {
val l1 = mutableListOf("1-1-1", "1-1-2", "1-1-3", "2-1-1", "2-1-2")
val m = mutableMapOf<String, MutableList<Int>>()
for (e in l1) {
val c = e.split("-")
val key = "${c[0]}-${c[1]}"
m.getOrPut(key, ::mutableListOf).add(c[2].toInt())
}
println(m)
}
But we can use the map and groupBy functions to create it more simply:
val m = l1.map { it.split("-") }
.groupBy(
{ "${it[0]}-${it[1]}" }, // keys
{ it[2].toInt() } // values
)
You can achieve your desired output with a single call to groupBy of the Kotlin standard library.
val input = listOf("1-1-1", "1-1-2", "1-1-3", "2-1-1", "2-1-2")
val result = input.groupBy(
{ it.substringBeforeLast("-") }, // extract key from item
{ it.substringAfterLast("-").toInt() } // extract value from item
)
The first lambda function extracts the key to group by of every list item. The second lambda function provides the value to use for each list item.
You can also do it by first mapping your values to Pairs and then group them as follows:
fun main(args: Array<String>) {
val input = listOf("1-1-1", "1-1-2", "1-1-3", "2-1-1", "2-1-2")
val result = input.map {
val values = it.split("-")
"${values[0]}-${values[1]}" to values[2]
}.groupBy ({ it.first }) { it.second }
println(result)
}

Pair items in a list with one another in Kotlin

I want to pair items in a list with one another
Example
list("A","B","C") to -> list(Pair(A,B),Pair(A,C),Pair(B,C))
list("A","B","C","D") to -> list(Pair(A,B),Pair(A,C),Pair(A,D),Pair(B,C),Pair(B,D),Pair(C,D))
I have tried using zipWithNext, but it does not help my cause. If anyone can show me how I can achieve this?
You can simply nest for loops and use ranges for that:
fun permute(list: List<String>): List<Pair<String, String>> {
var result: MutableList<Pair<String, String>> = mutableListOf()
for (i in 0..(list.size - 1)) {
val s = list.get(i)
for (j in (i + 1)..(list.size - 1)) {
val p = Pair(s, list.get(j))
result.add(p)
}
}
return result
}
There might be ways that are more Kotlin style, but I don't know one at the moment...
Using this method in a fun main() like this
fun main() {
val list = listOf("A", "B", "C", "D")
println(permute(list))
}
will output
[(A, B), (A, C), (A, D), (B, C), (B, D), (C, D)]
kotlin way )
var a = listOf("A", "B", "C", "D")
var pairs = a.mapIndexed { index, s ->
a.slice(index + 1 until a.size).map { Pair(s, it)}
}.flatten()
print(pairs)
If you were looking for a Pair chaining here is how to do it:
fun main() {
val a = listOf("A", "B", "C", "D")
val listPair: MutableList<Pair<String, String>> = mutableListOf()
a.forEachIndexed{ index, _ ->
if (index != a.size - 1) {
val pair = Pair(a.get(index), a.get(index + 1))
listPair.add(pair)
}
}
println(listPair)
}
Result: [(A, B), (B, C), (C, D)]