3 ms·
Except Kotlin enjoys the compatibility with Java, plus it outfits those java collections with extension methods and some compiler tricks to achieve all the same
by joshlemer 5y ago
Except Kotlin enjoys the compatibility with Java, plus it outfits those java collections with extension methods and some compiler tricks to achieve all the same functionality as the Scala collections (in fact, I would say even more self-consistent and useably than in Scala). Just as in Scala, in Kotlin you can use functional transforations that Scala users are so accustomed to:
Kotlin:
listOf(1,2,3,4)
.map{i -> i + 1}
.filter { it % 2 == 0 }
.flatMap { listOf(it, it * 2, it * 3) }
Kotlin even has a similar take to Scala's views, which they call sequences:
listOf(1,2,3,4)
.asSequence()
.map{i -> i + 1}
.filter { it % 2 == 0 }
.flatMap { listOf(it, it * 2, it * 3) }
.toList()
Is this really so "lacking in capability and grace" compared to Scala? The only think missing is persistent immutable collections, which are implemented in kotlinx.
- valenterry 5y ago> The only think missing is persistent immutable collections, which are implemented in kotlinx. You said: > You have to litter your whole code with `.asScala` and `.asJava`, the java collections don't work with for comprehensions, etc. So how does kotlinx achieve zero-cost compatibility with Java collections without having something like `.asKotlin` and `.asJava`? Because otherwise there is no difference between Scala and Kotlin in this regards.
- joshlemer 5y agobecause the persistentList/Collection/etc in kotlinx implement the appropriate java collection interfaces: import java.util.List; public class Foo { public static void blah(List<Integer> list) { } } Kotlin: import kotlinx.collections.immutable.persistentListOf fun main(): Unit { Foo.blah(persistentListOf(1,2,3)) } This compiles fine