I'm working on updating some Kotlin code that depends on arrow-kt. As part of the migration process from arrow 1.2 to 2.x, I need to get rid of arrow partiallyN() functions and replace them with native Kotlin lambdas.
The functions I am working on look like this:
private val simpleResultsProcessor: (MutableList<String>?, String) -> List<String> = { results, row ->
if (results == null) {
val newResults = mutableListOf<String>()
newResults.add(row)
newResults
} else {
results.add(row)
results
}
}
private fun select(sql: List<String>, resultsProcessor: (String) -> List<String>): Option<List<String>>
{
var result = none<List<String>>()
sql.forEach {
result = resultsProcessor(it).toOption()
}
return result
}
The real code is more complicated, but this simple list builder demonstrates the behavior I am struggling with.
I then have a test function that calls the select() function using an arrow partiallyN and a Kotlin lambda.
@Test
fun `test partiallyN replacement`()
{
val res1 = select(listOf("AB", "CD", "EF"), simpleResultsProcessor.partially1(mutableListOf()))
val res2 = select(listOf("AB", "CD", "EF") ) { sql-> simpleResultsProcessor(mutableListOf(), sql)}
assertThat(res1.isSome()).isTrue
assertThat(res1.getOrNull()).containsExactly("AB", "CD", "EF")
//res1 == ["AB", "CD", "EF"]
assertThat(res2.isSome()).isTrue
assertThat(res2.getOrNull()).containsExactly("AB", "CD", "EF")
//res2 == ["EF"]
}
I would expect both calls to produce the same results, however the lambda is overwriting the existing values rather than adding to them. What am I missing here? What is the partially function doing special and how do I write a lambda that will replicate that behavior?