Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail From: Cthun Newsgroups: comp.lang.java.programmer Subject: Re: Style Police (a rant) Date: Sun, 11 Sep 2011 17:11:05 -0400 Organization: Ph'nglui mglw'nfah Cthulhu R'lyeh wgah'nagl fhtagn Lines: 29 Message-ID: References: NNTP-Posting-Host: 5DQ/CWgDvL8hbDUH9FOxtg.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: WinVN 0.99.12z (x86 32bit) X-Notice: Filtered by postfilter v. 0.8.2 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7823 On 11/09/2011 3:20 PM, Wanja Gayk wrote: $ public List withoutDupes(final List xs) { $ return new Object() { $ List withoutDupes(final List head, final List tail) { $ if(tail.isEmpty()){return head;} $ if (head.contains(tail.get(0))) { $ return withoutDupes(head, tail.subList(1, tail.size())); $ } $ return withoutDupes( $ new ArrayList(head){{add(tail.get(0));}} $ , tail.subList(1, tail.size()) $ ); $ } $ }.withoutDupes(Collections.emptyList(), xs); $ } > This is a whole different beast (and prone to crash with a stack > overflow exception on larger lists by the way). > Admitted, it is not entirely functional due to the "add"-call, but quite > close. Still it is pretty compact code (there is a certain beauty in > recursion, isn't it?) and not hard to understand either. > > Bullying someone to functional code would be pretty stupid, as the > current JVMs still have a hard time detecting tail recursions and it > lacks data structures that do lazy evaluation in the Java SE. There's a way around that, and it's called Clojure. It compiles to JVM bytecode and has both lazy lists and a special operator for doing tail recursion (the compiler turns it into an iteration).