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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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 result } assert positionInAlphabet == [ 7 , 18 , 15 , 15 , 22 , 25 ] |
Code written with Groovy 2.4.