Kotlin

Collection Transformations

Swift

Mapping

                  val numbers = setOf(1, 2, 3)
println(numbers.map { it * 3 })
println(numbers.mapIndexed { idx, value -> value * idx })
                
                    let numbers = setOf(1, 2, 3)
print(numbers.map { $0 * 3 })
print(numbers.mapIndexed { idx, value -> value * idx })
                  
                  val numbers = setOf(1, 2, 3)
println(numbers.mapNotNull { if ( it == 2) null else it * 3 })
println(numbers.mapIndexedNotNull { idx, value -> if (idx == 0) null else value * idx })
                
                    let numbers = setOf(1, 2, 3)
print(numbers.mapNotNull { if ( $0 == 2) null else $0 * 3 })
print(numbers.mapIndexedNotNull { idx, value -> if (idx == 0) null else value * idx })

                  
                  val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key11" to 11)
println(numbersMap.mapKeys { it.key.toUpperCase() })
println(numbersMap.mapValues { it.value + it.key.length })
                
                    let numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key11" to 11)
print(numbersMap.mapKeys { it.key.toUpperCase() })
print(numbersMap.mapValues { it.value + it.key.length })
                  

Zipping

                  val colors = listOf("red", "brown", "grey")
val animals = listOf("fox", "bear", "wolf")
println(colors zip animals)
​
val twoAnimals = listOf("fox", "bear")
println(colors.zip(twoAnimals))
                
                    let colors = listOf("red", "brown", "grey")
let animals = listOf("fox", "bear", "wolf")
print(colors zip animals)
​
let twoAnimals = listOf("fox", "bear")
print(colors.zip(twoAnimals))
                  
                  val colors = listOf("red", "brown", "grey")
val animals = listOf("fox", "bear", "wolf")
​
println(colors.zip(animals) { color, animal -> "The ${animal.capitalize()} is $color"})
                
                    let colors = listOf("red", "brown", "grey")
let animals = listOf("fox", "bear", "wolf")
​
print(colors.zip(animals) { color, animal -> "The ${animal.capitalize()} is $color"})
                  
                  val numberPairs = listOf("one" to 1, "two" to 2, "three" to 3, "four" to 4)
println(numberPairs.unzip())
                
                    let numberPairs = listOf("one" to 1, "two" to 2, "three" to 3, "four" to 4)
print(numberPairs.unzip())
                  

Association

                  val numbers = listOf("one", "two", "three", "four")
println(numbers.associateWith { it.length })
                
                    let numbers = listOf("one", "two", "three", "four")
print(numbers.associateWith { it.length })
                  
                  val numbers = listOf("one", "two", "three", "four")
​
println(numbers.associateBy { it.first().toUpperCase() })
println(numbers.associateBy(keySelector = { it.first().toUpperCase() }, valueTransform = { it.length }))
                
                    let numbers = listOf("one", "two", "three", "four")
​
print(numbers.associateBy { it.first().toUpperCase() })
print(numbers.associateBy(keySelector = { it.first().toUpperCase() }, valueTransform = { it.length }))
                  
                  val names = listOf("Alice Adams", "Brian Brown", "Clara Campbell")
println(names.associate { name -> parseFullName(name).let { it.lastName to it.firstName } })
                
                    let names = listOf("Alice Adams", "Brian Brown", "Clara Campbell")
print(names.associate { name -> parseFullName(name).let { it.lastName to it.firstName } })
                  

Flattening

                  val numberSets = listOf(setOf(1, 2, 3), setOf(4, 5, 6), setOf(1, 2))
println(numberSets.flatten())
                
                    let numberSets = listOf(setOf(1, 2, 3), setOf(4, 5, 6), setOf(1, 2))
print(numberSets.flatten())
                  
                  val containers = listOf(
    StringContainer(listOf("one", "two", "three")),
    StringContainer(listOf("four", "five", "six")),
    StringContainer(listOf("seven", "eight"))
)
println(containers.flatMap { it.values })
                
                    let containers = listOf(
    StringContainer(listOf("one", "two", "three")),
    StringContainer(listOf("four", "five", "six")),
    StringContainer(listOf("seven", "eight"))
)
print(containers.flatMap { it.values })
                  

String representation

                  val numbers = listOf("one", "two", "three", "four")
​
println(numbers)         
println(numbers.joinToString())
​
val listString = StringBuffer("The list of numbers: ")
numbers.joinTo(listString)
println(listString)
                
                    let numbers = listOf("one", "two", "three", "four")
​
print(numbers)         
print(numbers.joinToString())
​
let listString = StringBuffer("The list of numbers: ")
numbers.joinTo(listString)
print(listString)
                  
                  val numbers = listOf("one", "two", "three", "four")    
println(numbers.joinToString(separator = " | ", prefix = "start: ", postfix = ": end"))
                
                    let numbers = listOf("one", "two", "three", "four")    
print(numbers.joinToString(separator = " | ", prefix = "start: ", postfix = ": end"))
                  
                  val numbers = (1..100).toList()
println(numbers.joinToString(limit = 10, truncated = "<...>"))
                
                    let numbers = (1..100).toList()
print(numbers.joinToString(limit = 10, truncated = "<...>"))
                  
                  val numbers = listOf("one", "two", "three", "four")
println(numbers.joinToString { "Element: ${it.toUpperCase()}"})
                
                    let numbers = listOf("one", "two", "three", "four")
print(numbers.joinToString { "Element: ${it.toUpperCase()}"})