Another groovier way to access some itens in my list.

Hello,

Yesterday I’ve posted this post: Uma maneira bem ‘a-lá groovy’ de se acessar o último elemento de uma lista! (English version here) that explains how we can use negative indexes to access the elements on one list using groovy. And a friend of mine, Paulo Suzart, that works with me commented that using Scala he can have some extra calls one one list.

lista.head //first element
lista.last //last element
lista.tail //all elements except the first one

I myself do not know scala well, ( except for the times he keeps talking about it! LOL). But this comments submitted me to my textmate to explore the list methods, and I got surprised when I saw all that methods exactly reproduced in groovy. My mistake that I did not posted this way in yesterday’s post, but here it is:

def lista = []
lista << “first element”
lista << “second element”
lista << “third element”

println lista.head() //”first element”
println lista.last() //”third element”
println lista.tail() //gives me another list, formed by “second element” and “third element”

That’s it! Hope it helps.

You can follow me on twitter: twitter.com/lucastex

Leave a Reply

Web Analytics