ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button
  An Introduction to Haskell, Part 1: Why Haskell
Subject:   List comprehension in ruby too...
Date:   2007-05-26 03:23:29
From:   paulk_asert
Response to: List comprehension in ruby too...

The Groovy equivalent to this looks very similar:

(1..10).findAll {i -> i % 2 == 0}

but if you look here:


You can define the infinite stream of natural numbers which then lets you define all the even numbers as follows:

def evennumbers = naturalnumbers.filter{ it % 2 == 0 }

and then use it like this:

assert [2 4 6 8 10] == evennumbers.take(5)