To keep it compact I used a "trick" that in Groovy list[0] returns null instead of throwing IOOBException for empty list. This can be also written in more verbose way.