3 ms·
It's perfectly reasonable to rag on it for this. What's not reasonable is that the code can be written in a much more compact way by not trying for an "applica
by wnoise 16y ago
It's perfectly reasonable to rag on it for this. What's not reasonable is that the code can be written in a much more compact way by not trying for an "applicative" style.
List even = new ArrayList();
for(int i : new Integer[]{1,2,3,4})
if (i%2==0) even.add(i);
Still larger than necessary, and this doesn't have all the necessary class cruft, but still much smaller.
Stolen shamelessly from https://gist.github.com/817635 https://gist.github.com/817635
- va_coder 16y agoMany of the other languages write this in functions that are first class citizens. The functions are ready to be used. In Java you would still need to put this inside another class to make it run and other classes would need to instantiate that class to run it. That's a big difference.
- lfborjas 16y agoThis is admittedly better, note, tough, that I was trying to do it in the mindset of the other languages included (just having one explicit list and filtering out instead of in); however, as it was stated before, it's never good to try to write code in the idiomatic style of other languages if the end result is something not idiomatic at all, it's like trying to play the bach chaconne for violin in a tuba or <put better analogy here> : it can be done but it's just forcing it.