3 ms·
If I read it correctly, you said "Scala standard library is not up to par", "Java is improving significantly in the coming few releases: pattern matching, recor
by dxxvi 7y ago
If I read it correctly, you said "Scala standard library is not up to par", "Java is improving significantly in the coming few releases: pattern matching, records, switch expressions". I see the other way around: Java 14 records, pattern matching and switch expressions are like a joke compared to Scala case class and pattern matching. For example, does Java 14 Stream have any method equivalent to `collect` in Scala, which is a combination of filter and map?
- apta 7y ago> Java 14 records, pattern matching and switch expressions are like a joke compared to Scala case class and pattern matching. What does Scala's pattern matching do that Java's (upcoming) doesn't? > For example, does Java 14 Stream have any method equivalent to `collect` in Scala, which is a combination of filter and map? Not that I'm aware, but then again, I don't see the large advantage compared to making two separate map + filter calls. Streams in Java are lazy, whereas calling `map` or `filter` on a concrete collection in Scala is eager, creating temporary intermediate collections only to discard them in the case of chaining calls.
- dxxvi 7y agoAn example of Scala's pattern matching: case class Person(name: String, age: Int) // a collection of 3 people val c1 = Vector(Person("A", 5), Person("B", 6), Person("C", 4)) // now I want a collection of people who have age > 4 and then their ages are doubled val c2 = c1 collect { case Person(name, age) if age > 4 => Person(name, age*2) } How do you write it in Java 14? Want something lazy like Java streams? It's LazyList in Scala 2.13 and Stream in Scala < 2.13.
- sorokod 7y agoIn Kotlin data class Person (val name: String, val age: Int) val c1 = listOf(Person("A", 5), Person("B", 6), Person("C", 4)) val c2 = c1.filter{ it.age > 4 }.map{ it.copy(age = it.age*2) }
- dxxvi 7y agoKotlin and Java don't have a powerful pattern matching like Scala. Maybe that's why they don't have `collect` for filter + map. However I'm learning kotlin now because my employer doesn't allow me to use Scala