I found a piece of code I do not understand.
I am transforming a JSONArray
into a List
.
Kotlin provides the function mapTo
in it's stdlib
(link)
mapTo
inline fun <T, R, C : MutableCollection<in R>> Iterable<T>.mapTo( destination: C, transform: (T) -> R ): C (source)
Applies the given transform function to each element of the original collection and appends the results to the given destination.
This functions has 2 parameters and can be used like this (as expected):
(0..jsonArray.length() - 1).mapTo(targetList, {it -> jsonArray[it].toString()})
But apparently this is also valid syntax (not expected):
(0..jsonArray.length()-1).mapTo(targetList) {it -> jsonArray[it].toString()}
As you can see, the function parameters end after outputList
and the lambda expression is just put at the end of the function call.
Furthermore this is legal (as expected):
val transformation = {it : Int -> jsonArray[it].toString()}
(0..jsonArray.length()-1).mapTo(targetList, transformation)
but this is not (???):
val transformation = {it : Int -> jsonArray[it].toString()}
(0..jsonArray.length()-1).mapTo(targetList) transformation