I'm doing this entry because while reading the for-comprehensions in both Scala and Clojure I realized we don't have that feature in Groovy...yet.
After googling a little bit, I found Stream, a framework created by Tim Yates. This project "is a library for Groovy that lets you create lazy Iterators (or streams) based on Iterators, Collections, Maps, Iterables or other Streams".
Thanks to Stream I was able to create some similar code to what I've seen in Scala in just a few minutes. I've just touched the surface but Stream looks amazing, specially if you have to deal with data generation.
Apart from the for-comprehension, a couple of weeks ago I read some tweet talking about how nice would be to have an "unless" conditional in Groovy. Well I did my own example using Categories.
Here's the code for both (GroovyConsole script) :
Thanks to Stream I was able to create some similar code to what I've seen in Scala in just a few minutes. I've just touched the surface but Stream looks amazing, specially if you have to deal with data generation.
Apart from the for-comprehension, a couple of weeks ago I read some tweet talking about how nice would be to have an "unless" conditional in Groovy. Well I did my own example using Categories.
Here's the code for both (GroovyConsole script) :
@Grab( 'com.bloidonia:groovy-stream:0.5.2' ) import groovy.stream.Stream class Condition{ def static unless(Object o,Boolean condition,Closure cl){ if(!condition){ o.with(cl) } } } def loop(generators,Closure cl){ Stream.from(generators).collect().each(cl) } use (Condition){ loop(car:["Ferrari","Aston Martin","Seat"], color:["red","blue","silver"]){next-> unless(next.color == "blue"){ println "I like $next.color ${next.car}s" } } }
And the output
Bottom line: The more I learn about other languages the more I enjoy Groovy... and other languages as well ;-)
BTW If you have to work with Groovy,Scala and Clojure I'd recommend you to use Gradle.
No comments:
Post a Comment