7 ms·
Why I absolutely love java
not
- dstein 16y agoYou cheated. Do it without the fancy schmancy lists.
- yakto 16y agoJob security?
- foresterh 16y agoNo C#?
- lfborjas 16y agoI don't do C#, but feel free to fork the gist and add it :)
- mbthomas 16y agoEnjoy - https://gist.github.com/817556 https://gist.github.com/817556
- deleted 16y ago[deleted]
- lfborjas 16y agonice!
- kenjackson 16y agoIn fairness to the Java version, you should probably add all the additional stuff. using System.Linq; class Filter { static void Main() { var a = new []{1, 2, 3, 4}.Where(x=>x%2==0); System.Console.WriteLine(a); } } It's unclear if printing is necessary or not, as the Java one does an explicit print, but the others don't.
- lfborjas 16y agoThe other ones are meant to be run in a REPL, but java doesn't have one (lest you count beanshell, which ships with a really old version of java, one before generics) so I had to print it explicitly
- nrkn 16y agousing System.Linq; class F { static void Main() { new[] {1, 2, 3, 4}.Where( i => i % 2 > 0 ).ToList().ForEach( System.Console.WriteLine ); } }
- TeHCrAzY 16y agoIt's always irritated me that ForEach isn't available on IEnumerable<T>
- redthrowaway 16y agoI know it's fun to rag on Java, but this is a meaningless comparison. Java isn't a scripting language, and it wasn't ever intended to be one. It was never designed to solve quick little problems in a few lines, it was designed to deal with massive enterprise apps with complicated dependency trees over multiple architectures, and it does that job pretty damned well. Stop complaining that Java isn't Python/Ruby. It's like saying that x86 assembly is terrible because string concatenation takes so many loc. Judging a language by standards it was never designed to meet is pointless. If you want to decide whether Java is a good language or not, see if it solves the problems it was designed to solve. I'd say it does. I may not like working in Java for small projects, but that doesn't make it a bad language.
- wnoise 16y agoIt'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.
- earino 16y agoperl (because I feel like it): grep { $_ % 2 == 0 } (1,2,3,4,5);
- zakovyrya 16y agoeven shorter: grep { $_ % 2 == 0 } 1..5;
- rlmw 16y ago-- Haskell filter even [1,2,3,4]
- T_S_ 16y agoOr take 2 (filter even [1..]) // take as many as you want
- lfborjas 16y agothis looks really nice, gotta learn me some haskell
- T_S_ 16y agoYou can. For free. Later this week in Mountain View. From some of the world's best and friendliest Haskell experts. <http://wiki.hackerdojo.com/w/page/32992961/Haskell-Hackathon-2011> http://wiki.hackerdojo.com/w/page/32992961/Haskell-Hackathon...; Newbies take special note of the Saturday session.
- ricree 16y agoI'm currently in the process of learning it, and I'd definitely recommend it. If nothing else, it's great having a compiler that can catch a large amount of errors without getting bogged down in the type boilerplate that many other static languages require. Implicit static typing is definitely something I miss when working in other languages.
- 16y ago
- democracy 16y agoJAVA for (int i:new int[]{1,2,3,4}) System.out.print(i%2==0?i:"");
- lfborjas 16y agohehe, clever! although we are building a new list in the other languages, we're never using it, so this is just as good in the immediate context.
- andralex 16y agoD rocks as usual: filter!`a % 2 == 0`(iota(1, 5)) (no eager computation, no dynamic allocation)
- bitsai 16y agoI don't know about the other lisp dialects, but the anonymous function is actually not needed in the Clojure version: (filter even? [1 2 3 4])
- mike_esspe 16y agoScala List(1, 2, 3, 4).filter(_ % 2 != 0) And it's very easy to move to Scala from Java :)
- deleted 16y ago[deleted]
- uvTwitch 16y agoC#: var filtered = (new int[]{1,2,3,4}).Where<int>( i => i%2 != 0 );
- lars 16y agoThis is what really shows the problem with Java.
- zackb 16y agoAnd C#...
- rayvega 16y agoAlternatively: var filtered = new List<int>{1, 2, 3, 4}.FindAll(x => x % 2 == 0);
- solutionyogi 16y agoAs C# has some type inference, you can reduce it further: var filtered = (new[] { 1, 2, 3, 4 }).Where(i => i % 2 != 0); Not bad for a static language. :)
- nrkn 16y agoHi David :) You can drop the parentheses, and int is inferred by the compiler: var filtered = new[]{ 1, 2, 3, 4 }.Where( i => i % 2 != 0 ); Alternatively: var filtered = Enumerable.Range( 1, 4 ).Where( i => i % 2 > 0 );
- zackb 16y agoOr you can just use groovy and get the best of both worlds :) [1,2,3,4].findAll {it % 2 == 0}
- rll 16y agoPHP array_filter(array(1,2,3,4),function($v) { return !($v&1); });
- eldenbishop 16y ago-- Groovy (1..4).findAll{!(it % 2)}
- deleted 16y ago[deleted]
- lfborjas 16y agopretty cool!
- deleted 16y ago[deleted]
- dekz 16y agocoffeescript [1..4].filter (x) -> !(x%2)
- ichramm 16y agoThat's the problem of java developers, they dont use the bitwise operations: if(e.intValue()%2 != 0) == >>> if((e.intValue()& 1) != 0)
- jankassens 16y agoThere's no problem here. You can be pretty sure simple calculations like this (and multiplications with a power of 2 vs. shifting etc.) will run the same machine code in the end.
- sfraser 16y agoI think you are right in this case - I updated my example code above to use bitwise and it is running in the same amount of time. (100,000 iterations in about 6 milliseconds)
- daleharvey 16y agoErlang [X || X <- [1,2,3,4], X rem 2 == 0]
- va_coder 16y agoumm is that supposed to show off Erlang? Maybe it's just me but for such a simple task that looks bad.
- daleharvey 16y agoit was just for completeness, certainly not to show off, however list comprehensions are virtually identical to python and much more comprehensible than the line noise introduced in ruby list comprehensions. the fact that this is called out twice as being "ugly" twice in comparison to the other examples auggests to me that erlang is called out as being ugly purely from hearsay as opposed to actually being ugly. (I do think erlang can be ugly, I also think thats its biggest strength, but I dont think this example is ugly)
- va_coder 16y agougly is subjective - an opinion.
- guygurari 16y agoerlang is called out as being ugly purely from hearsay I have no preconceptions about Erlang. I've never seen the opinion that Erlang is ugly, and your code above is the first Erlang I've ever seen. I also know nothing about Haskell, but I found the Haskell example to be the most elegant. So, a more reasonable conclusion: The fact that two people found the code ugly suggests that it is ugly.
- guygurari 16y agoUgh. Thanks for convincing me not to learn Erlang.
- deleted 16y ago[deleted]
- santadays 16y agoimport static com.google.common.collect.Lists.*; import static com.google.common.collect.Collections2.*; import com.google.common.base.*; public class AdmittedlyNotMuchBetter { public static void main(String[] args) { System.err.println(filter(newArrayList(1,2,3,4),new Predicate<Integer>() { public boolean apply(Integer input) {return input % 2 == 0;} })); } } btw, how do you preserve source indentation in the comments? [edit: formatting, thanks]
- jules 16y agoJ solution anyone?
- losvedir 16y ago&1/^@4%2!# Just a guess...
- huherto 16y agopublic class filter { public static void main (String [] args) { System.out.println("2, 4"); } } FTFY, While it may be fun that show off with your favorite language, the Java version is intentionally complex. Edit: In case you missed the point. This version is as absurd trying to be short as the other version is absurd trying to be long. Also, I can guarantee that an "Enterprise" version would be 200 lines long :-P
- sfphotoarts 16y agothis misses the point, if you hard-code the result then still the other languages are more concise.
- jbri 16y ago"Enterprise" developer at work here, folks!
- aroberge 16y agoThe other examples filter from an existing list, essentially used as a placeholder. It could be substituted with any other list and the code sample would work. Your example use some hardcoded values - strings at that, not integers - hence not the same thing at all.
- blinkingled 16y agoIf you want to love Java - love it for the tooling. IDEs are bloated but the integrated features they provide - like built in profiler(Netbeans), postmortem analysis tools (Eclipse MAT), the ease of debugging it provides - remote attach, JConsole type stuff where you can visualize loads of interesting stuff while the JVM is still executing your program, things like EProf that comes with HP JVMs - it can be turned on and off dynamically with low overhead and no changes required - the sort of data it gives makes it a piece of cake to figure out what is going wrong (it even tells you during the profile which exceptions were thrown and how many times!). I make a living writing and supporting Enterprise Java apps and if it wasn't for these rich set of tools, my job would have been a nightmare. (Before flaming me saying if he wasn't writing code so badly in first place, he wouldn't need those tools - I also support code written by other people and writing complex, well performing code still remains notoriously hard.)
- julian37 16y agoThis can be a lot shorter using Google Guava [1]: Collections2.filter( Lists.newArrayList(1, 2, 3, 4), new Predicate<Integer>() { public boolean apply(Integer i) { return i % 2 == 0; } }); As I understand it, JDK 8 will support lambdas which will make it shorter still. [1] http://code.google.com/p/guava-libraries/ http://code.google.com/p/guava-libraries/
- democracy 16y agoOr apache collections: CollectionUtils.filter(new ArrayList(Arrays.asList(1, 2, 3, 4)), new Predicate() { public boolean evaluate(Object v) { return ((Integer) v).intValue() % 2 == 0; } });
- shikyo 16y agoOr with http://code.google.com/p/totallylazy/ http://code.google.com/p/totallylazy/ (ignoring imports): sequence(1,2,3,4).filter(even());
- deleted 16y ago[deleted]
- va_coder 16y agoWhat is Doug Cutting is saying these days about Java? He wrote Lucene, Hadoop and other awesome things in Java. I don't like Java much but I'd love to hear his side of the argument.
- djacobs 16y agoI used this exact example a few months ago to compare Java and Ruby [1]. Funny how it keeps coming up. (While I was using Java, I never even knew there was a need for higher-order functions.) [1] http://news.ycombinator.com/item?id=1947723 http://news.ycombinator.com/item?id=1947723
- nl 16y agoScrew that. Others have already pointed out the Google Guava library. This version is almost all just standard Java, just using Guava to create the list of numbers. ArrayList<Integer> list = com.google.common.collect.Lists.newArrayList(1,2,3,4); for (Iterator<Integer> it = list.iterator(); it.hasNext(); ) {if (it.next() % 2 == 0) {it.remove();}} System.out.println(list); Also, Java does have REPL. It's called the Eclipse scrapbook view. I'm sure Netbeans has something similar (and that is part of one of the standard Java SDK downloads).
- fleitz 16y agoIf you look close enough you can almost figure out what it's doing between all the line noise.
- nl 16y ago// create an array list with 4 integer elements ArrayList<Integer> list = com.google.common.collect.Lists.newArrayList(1,2,3,4); // get an Iterator from the list, and use it to iterate though the list for (Iterator<Integer> it = list.iterator(); it.hasNext(); ) { // check if this element is even. // If we want the odd numbers, change to != 0 if (it.next() % 2 == 0) { // remove it if it is even it.remove(); } } // print the list System.out.println(list);
- TeHCrAzY 16y agoNot sure if you were being a smart ass, but it would be much more readable without any of the comments, and simply with the new lines.
- nl 16y agoI'm being a smartass. Kinda.. The commenter said they could almost figure out what it's doing. It's not clear from that that comment if it is just the formatting that was the problem (which I agree, isn't very readable).
- 16y ago
- sfraser 16y agoTo be clear I <3 clojure, but I'm taking the bait just for fun: 1) The chunk of Java code shows a complete program - the others show function definitions 2) The Java is written purposely to be as verbose as possible 3) The Java sends it's output to stdout, unlike the others 4) The Java code is needlessly written using generics Things the comparison ignores: 1) The Java code is more toolable than the others 2) The Java code will outperform most of the other examples once it has been subject to Hotspot Here's a more fair comparison, at the function level to level the playing field (and only one import needed: java.util.*): List trollBait() { return new ArrayList(){{ for (int i : new int[]{1, 2, 3, 4}) { if (i % 2 == 0) add(i); }}}; } Here's why I absolutely do heart Java: I just ran this 100,000 times in 6 milliseconds on a Core i7 920 (2.67 GHz). And I scanned it with Findbugs, and PMD, and a security vulnerability tool. And I refactored it in my IDE with crazy IntelliJ foo-jitsu. And it's more strongly typed. And it's runtime memory model is safer.
- lfborjas 16y agonow we're talking, this is a really good response! I almost agree with all of your points, except for the performance, and only in thoroughness: a benchmark will be nice. And for clarity: the other ones aren't function definitions, they also build a new list, like your example. The examples are meant to be run in a REPL and that's why they don't send to stdout, although it will be just another function application in most cases (but your example works in beanshell, the stand-alone java quasi-REPL, so we're ok). I think all of them can be considered a full-program in those languages: as a stand-alone chunk of code that can be compiled/interpreted and will produce an executable output, that is. And, yes, the autoboxing was the only reason I chose generics, but I tried for it to be concise, given my limited knowledge of java; the max-verbose mode would have used a Runnable or something evil to simulate a higher order function.
- sfraser 16y agoThanks... my experience with clojure on the JVM is it is in general slower than optimally tuned Java code. I really can't imagine that any of the other languages you listed would easily outperform the code I have here, but maybe since the use-case is a sweet spot for functional? The person who responded to me with the F# example reported a crazy low time, and I am wondering if they have a defect in the code. I think it is possible a true functional language could make some tweaks that Java can't (like realizing the function is idempotent and caching the response instead of recalculating it). Maybe that is why the F# example was so quick - either the way it times itself is defective, or perhaps the runtime memoizes the function. My experience with all the languages in your sampler is that their runtime performance usually lags the Java on the JVM for general purpose programming. This is not an argument not to use them - I would prefer many of them in certain use cases. But if I want a managed runtime, and have some performance critical code that I am OK with being written in an imperative language, it is hard to beat the performance of simple Java on the JVM. There has been so much tuning to Hotspot over the last decade. That's been my experience anyways.
- waterhouse 16y agoegrep: echo '1 2 3 4' | egrep -o '\<[0-9]*[02468]\>'
- fleitz 16y agoF# [1..4] |> List.filter(fun x -> (x % 2) = 0) C# using System.Collections.Generic; public static class Foo { public static void Main(string[] args){ Enumerable.Range(1,4).Where(x => (x % 2) == 0); } }
- deleted 16y ago[deleted]
- srgseg 16y agoEvery time I see one of these ugly Java code examples, I always scratch my head because my code never looks like this. Here is what these code examples are exploiting: 1. Java isn't designed for scripting. All of my Java programs require many classes. I hardly have any main() methods because most things I write are classes invoked by other classes, e.g. in the case of a servlet being invoked by the web application server. 2. It's rare in practice that I need to hard code the contents of data structures inside my own code. Normally they are loaded from data files. 3. Import statements are automatically thrown in by the IDE and most people hardly even notice they're there and type nothing more than Alt-Enter any time there is an import line missing. So really, what I really am typing in java is only: List<Integer> result = new ArrayList<Integer>(); for(int i : source) if(i%=2) result.add(i); Now that's not really all that complicated is it?
- djacobs 16y agoThe point is that you have to write the for loop and if statement and concatenation for each different function you want to define. The better way is to use higher order functions, which Java lacks.
- mitko 16y agoAdvice, drop the 1 and 3: just [2,4] - its cleaner.
- vilya 16y agoHere's a C++ 0x version, using a lambda function: #include <iostream> #include <algorithm> int main(int argc, char** argv) { int src[] = { 1, 2, 3, 4 }; int* end = std::remove_if(src, src + sizeof(src) / sizeof(int), [](int i) -> bool { return i % 2 != 0; }); for (int* p = src; p != end; ++p) std::cout << *p << " "; std::cout << std::endl; } I wasn't sure about the lambda function syntax, but this compiles with g++ 4.5 (interestingly, even with the -Wall option, g++ doesn't warn about main not returning a value). Shows that even c++ can be shorter than java...
- serichsen 16y agoTL;DR
- kaffiene 16y agoWell. given how completely and utterly useless Java is, it's incredible that it's ended up powering so many major internet sites and important systems like banks. Obviously, successful internet companies like Google are all idiots and don't realise that <Scripting Flavour of the month> is the only solution for their issues and if only they'd adopt that, then they'd be really successful. After all, the REAL issues in large scale software engineering are how few characters we use to solve a toy problem. All that actual software engineering being done in Java doesn;t count because Java was hyped 15 years ago (all those business running on Java don't realise that their success isn't actually real, and they're all hype too). Message to HN: Grow the fuck up. Ruby / Python / Lisp are cool. I like them. They're not the right tool for everything. Java is extremely good at things it's being used for right now and it maintains that position by being better than other language / ecosystems. That will undoubtedly change at some point but not yet.