Suppose I have a data list val data = listOf("F 1", "D 2", "U 1", "D 3", "F 10")
and I wanna perform the given logic on each element.
I have to add var acc2 = 0
outside to do this. I wonder is there any possible to get two accumulators in fold / reduce that this method be without side effect.
val data = listOf("F 1", "D 2", "U 1", "D 3", "F 10")
var acc2 = 0
val result = data.fold(0, { acc, str ->
when (str.split(" ")[0]) {
"F" -> {
acc str.split(" ")[1].toInt() * acc2
}
"D" -> {
acc2 = str.split(" ")[1].toInt()
acc
}
"U" -> {
acc2 -= str.split(" ")[1].toInt()
acc
}
else -> {
acc
}
}
})
CodePudding user response:
You can use a simple object, wrapping both values.
data class Position(var horizontal: Int, var depth: Int)
val data = listOf("F 1", "D 2", "U 1", "D 3", "F 10")
val result = data.fold(Position(0, 0)) { position, str ->
val (command, value) = str.split(" ")
when (command) {
"F" -> position.horizontal = value.toInt()
"D" -> position.depth = value.toInt()
"U" -> position.depth -= value.toInt()
}
position
}
If you don't want to create a new class for this, you may utilize the class Pair<Int, Int>
from the stdlib of Kotlin.
CodePudding user response:
Thanks to above comments I have tried to use Pair
in my solution
val result = data.fold(Pair(0, 0), { accs, str ->
val (command, value) = str.split(" ")
when (command) {
"F" -> Pair(accs.first value.toInt() * accs.second, accs.second)
"D" -> Pair(accs.first, accs.second value.toInt())
"U" -> Pair(accs.first, accs.second - value.toInt())
else -> accs
}
})