SyntaxHighlighter

Thursday, July 28, 2011

join and split

You can join elements in a List to form one String, and you can take a String and split it and put them into a List.


def langs = ['haskell', 'python', 'groovy', 'java', 'php', 'c']
println langs.join(' > ') // my interest level at the moment.
def scripts = "JavaScript, coffeeScript, clojureScript"
// notice again that there's no trailing colon ":" in below. Yay!
println scripts.split(',').collect { it.trim() }.join(":")


For some this is trivial, for others, not-so-trivial.
When I first encountered join() and split() in perl during my uni days, I thought this toy-like function wouldn't be really useful in real world applications. How wrong I was.
I'm not sure why this functionality never made into JDK still. Apache Common Lang's StringUtils is helpful here, but not when that function is the only thing I needed. I mean, am I the only one who wrote something like the following over and over again?


StringBuffer sb = "";
for (String s : list) {
sb.append(s).append(":");
}
String result = sb.toString().substring(0, sb.length() - 1);

Saturday, July 23, 2011

Dynamic type casting

Oh, I need to be careful with dynamic types in groovy.

If you do calculations on a BigDecimal with a Double (or double), you will get a Double back, not a BigDecimal.
Try the following in a groovyConsole to confirm.

import java.math.RoundingMode

def price = 10.25g
def tax = 1.1g
def afterTax = price * tax
println afterTax.getClass() // class java.math.BigDecimal
println afterTax.setScale(2, RoundingMode.HALF_UP) // 11.28

double tax_d = 1.1d
def afterTax_d = price * tax_d
println afterTax_d.getClass() // class java.lang.Double
println afterTax_d.setScale(2, RoundingMode.HALF_UP) // Will throw groovy.lang.MissingMethodException

Friday, July 8, 2011

Simpler if condition and Truthy/Falsy

Coming from Java land, I'm enjoying the ability to write shorter code in groovy than in Java. The condition inside an if statement is one of those. Given cool is a boolean, in Java I'd do this.
if (cool == true) {
// do something cool
} else {
// do something hot
}
In groovy, I can do this.
if (cool) {
// do something cool
} else {
// do something hot
}
But it's good to remember what actually gets counted as true or false. Although I don't like using null as a value, I had to deal with one the other day. I had a return value returned from a service call, the service call itself returned either a String or null.
def something = oldService.tellMeSomething()

if (something == null) {
// I don't like null, but I have to live with it for now.
} else {
// work on something
}
I got excited and changed the code to the following.
def something = oldService.tellMeSomething() // same service call
if (!something) {
// I don't like null, but I have to live with it for now.
} else {
// work on something
}
Then, I suddenly had a hunch that this might be wrong. The thing was that an empty String is a legitimate value returned, and I wasn't sure if an empty String would be evaluated as true or false. So I ran the following in a groovyConsole.
def e = ""
def n = null

if (e) {
println "empty string is truthy"
} else {
println "empty string is falsy"
}

// check for the opposite case too for a good measure.
if (!e) {
println '(!empty string) is truthy'
} else {
println "(!empty string) is falsy"
}

if (n) {
println "null is truthy"
} else {
println "null is falsy"
}


// check for the opposite case too for a good measure.
if (!n) {
println "(!null) is truthy"
} else {
println "(!null) is falsy"
}
If you ran that, or you know groovy better than I do, you might be relieved to know that I reverted my change back to the original (and I sighed a little too for the use of null).

Thursday, July 7, 2011

Groovy Date clearTime() method

Groovy has a nice Date class defined.
One I learned to use recently is clearTime() method.
Date today = new Date().clearTime()
This should result to today's date with no time component attached to it.

But, my code kept on failing.
So I dug around a bit, and found this: http://jira.codehaus.org/browse/GROOVY-4788

I was on groovy 1.7.3, because my project's on grails 1.3.7. As you can see on the jira page, at this version of groovy, clearTime() does not have a return value.

Watch out for it if you are stuck!

After thought: groovy 1.8 seem to have very nice improvements over previous versions of groovy. I hope grails 1.4 will get released soon!