3 ms·
That's not a fair comparison. What about: IntStream.range(0, 100).filter(i -> i % 2 == 0).collect(Collectors.toList()); Disclaimer: I'm not writing Java nowa
by toong 11y ago
That's not a fair comparison. What about:
IntStream.range(0, 100).filter(i -> i % 2 == 0).collect(Collectors.toList());
Disclaimer: I'm not writing Java nowadays, so this is my quick prototype from a quick google-session, it probably won't compile :D
Edit: looks like I'm second, my google-session took too long :-)
Maybe bonus points for C#:
Enumerable.Range(0, 100).Where(i => i % 2 == 0);
- pmelendez 11y agoTrue... although that is to me even less readable than the first Java version. The clojure version still wins by a large distance in my book because of its elegance.
- yogthos 11y agoThat's better, but Clojure still has less noise, and in my experience that adds up significantly in the long run.
- highCs 11y ago> IntStream.range(0, 100).filter(i -> i % 2 == 0).collect(Collectors.toList()); This has been added to java to catchup with functional programming languages.
- toong 11y agoI know those are functional concepts, but it feels more like trying to catch up with C# (LINQ) to me.
- dkersten 11y agoThat's not bad, but in the clojure version, I have to know/learn three functions: filter, even? and range In the Java version, I need to know/learn IntStream.range, IntStream.filter, IntStream.collect, the even lambda, and Collectors.toList Of course, this is comparing one-liners, so doesn't necessarily translate to larger programs. Just pointing out that I don't find this one particular example to be in Javas favour.