Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

106
Visualizações
Two accumulators in reduce / fold

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
        }
    }
})
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório

0

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
     }
})
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda