January 28, 2015

Groovy Goodness: Getting the Indices of a Collection

Since Groovy 2.4 we can use the indices property on a Collection to get the indices of the elements in the collection. We get an IntRange object as a result.

def list = [3, 20, 10, 2, 1]
assert list.indices == 0..4

// Combine letters in alphabet
// with position (zero-based).
def alphabet = 'a'..'z'
def alphabetIndices = [alphabet, alphabet.indices].transpose()
// alphabetIndices = [['a', 0], ['b', 1], ...]

// Find position of each letter
// from 'groovy' in alphabet.
def positionInAlphabet = 'groovy'.inject([]) { result, value ->
    result << alphabetIndices.find { it[0] == value }[1] + 1

assert positionInAlphabet == [7, 18, 15, 15, 22, 25]

Code written with Groovy 2.4.

Groovy Goodness: Pop And Push Items In a List

Groovy adds the pop and push methods to the List class. With the pop method we remove the last element of the list. And with the push method we add an element to the end of the list.

def list = ['Groovy', 'is', 'great!']

// Remove last item from list
// with pop().
assert list.pop() == 'great!'
assert list == ['Groovy', 'is']

// Remove last item
// which is now 'is'.

// Add new item to end of
// the list (equivalent for add()).

assert list == ['Groovy', 'rocks!']

Code written with Groovy 2.4.

Groovy Goodness: Getting All But the Last Element in a Collection with Init Method

In Groovy we can use the head and tail methods for a long time on Collection objects. With head we get the first element and with tail the remaining elements of a collection. Since Groovy 2.4 we have a new method init which returns all elements but the last in a collection.

In the following example we have a simple list and apply the different methods:

def gr8Tech = ['Groovy', 'Grails', 'Spock', 'Gradle', 'Griffon']

// Since Groovy 2.4 we can use the init method.
assert gr8Tech.init() == ['Groovy', 'Grails', 'Spock', 'Gradle']
assert gr8Tech.last() == 'Griffon'

assert gr8Tech.head() == 'Groovy'
assert gr8Tech.tail() == ['Grails', 'Spock', 'Gradle', 'Griffon']

Code written with Groovy 2.4.

January 26, 2015

Groovy Goodness: Take Or Drop Last Items From a Collection

We know Groovy has a lot of nice methods for working with collections. For example in previous blog posts we have seen how to take or drop elements from a list and even with a condition. Since Groovy 2.4 we can now also use the dropRight and takeRight methods to take or drop elements from the end of the list.

In the following example we have a simple list and we use the dropRight and takeRight methods to get elements from the list:

def list = ['Simple', 'list', 'with', 5, 'items']

assert list.takeRight(1) == ['items']
assert list.takeRight(2) == [5, 'items']
assert list.takeRight(0) == []
// Whole list, because we take more items then the size of list
assert list.takeRight(6) == ['Simple', 'list', 'with', 5, 'items']

assert list.dropRight(1) == ['Simple', 'list', 'with', 5]
assert list.dropRight(3) == ['Simple', 'list']
assert list.dropRight(5) == []
assert list.dropRight(0) == ['Simple', 'list', 'with', 5, 'items']
assert list == ['Simple', 'list', 'with', 5, 'items']

def array = ['Rock on!', 'Groovy baby!'] as String[]
assert array.takeRight(1) == ['Groovy baby!'] as String[]
assert array.dropRight(1) == ['Rock on!'] as String[]

def range = 0..10
assert range.takeRight(2) == [9,10]
assert range.takeRight(4) == 7..10
assert range.dropRight(5) == 0..5

Written with Groovy 2.4.

January 7, 2015

Gradle Goodness Notebook is Published

Today Gradle Goodness Notebook is published. This book is an electronic publication with all blog posts about Gradle Goodness bundled. The posts are slightly edited and categorized into sections.

The book is published at Leanpub and is available in three formats: PDF, MOBI (for Kindle) and EPUB (for iPad). Updates for the book are free. So new Gradle Goodness blog posts will be added to the book and you will get those free.

It is also possible to buy a Goodness Notebooks bundle. This bundle contains the Groovy and Gradle Goodness Notebook books.

I hope you will enjoy the book and I will keep it up-to-date with new content when I publish new Gradle Goodness blog posts.