3 ms·
"Remember filtering lists with for loops? Thanks to the functional approach to collections, we can also reduce all the complex, multiline instructions to only o
by moondowner 9y ago
"Remember filtering lists with for loops? Thanks to the functional approach to collections, we can also reduce all the complex, multiline instructions to only one line of code."
Erm, same can be done in Java with the Java 8 stream API.
I like Kotlin and use it with Spring, but articles like this where programming languages are not compared properly make me cringe.
- EwanToo 9y agoThe last time I looked, Java 8 wasn't available (and probably never will be?) available on Android, which is what this article is talking about. Edit - http://www.infoworld.com/article/3180758/mobile-development/android-switches-to-native-java-8-support.html http://www.infoworld.com/article/3180758/mobile-development/... it looks like Android is gaining Java 8 support now
- GrumpyBen 9y agoNo, some Java 8 features are available (https://developer.android.com/studio/preview/features/java8-support.html https://developer.android.com/studio/preview/features/java8-...) but streams are only available on Android Nougat
- on_and_off 9y agoYou don't have access to this API in Android. Except maybe if you minAPI is something ludicrously high like 25, but that's not going to be the case for almost all apps in the coming year. Kotlin is useable right now.
- deleted 9y ago[deleted]
- norswap 9y agoTrue, but the stream API isn't very friendly. You have to get a stream (.stream()), filter, then collect in a collection (.collect(..)) for which you need to find a suitable collector (the stdlib supplies some for lists and maps, but you have to find where they are defined). These are small things, and easily corrected with a bit of know-how and/or utility methods. But I think it has had a huge impact on the (non-)adoption of streams. Another obvious API gap: going from an iterator or iterable to a stream requires much more ceremony than it should.
- vbezhenar 9y agoActually I'm not sure if Kotlin's code is that performant. It'll create new ArrayList on each step just to discard it after. It won't matter for tiny lists, but even for moderate lists it does matter. If you want to iterate without creating new arrays on each step, you'll need to use Sequence. And now there's another problem: Sequence doesn't know its lengths. So when you'll collect computation result, you can't just allocate exact memory, you'll need to allocate some memory, then grow it, and so on. Java streams are lazy and they try hard to know expected output size, so they are better in some aspects. That said, Sequence have very simple implementations while Java Streams are hairy monsters under the hood and you can use Java Streams from Kotlin without problems, if you're targeting JDK8.
- StevePerkins 9y agoEven after 10,000 armchair lawyer discussions about the Google-Oracle lawsuit, I feel like 99% of developers still do not grok that Android's "Java" isn't really Java. They wrote their own implementation of an earlier version, and at this point will probably never update its API to fully match Java 8 or beyond. Android devs who want advancements will have to add "shims", like retrolambda or (quite frankly) Kotlin.
- pjmlp 9y agoAka language fork.
- jondwillis 9y agoWow! One line of code that is 400 characters long and difficult to parse! I don't know anyone worth their salt who writes functionally that doesn't use line breaks liberally to make things readable. Functional programming has very little to do with terseness IMO.