Skip to content

Array Wrapper

ArrayWrapper

Provides utility methods for manipulating an Array. On the following methods, wrap is an ArrayWrapper.

Note

Since 6.10.15

The ArrayWrapper is iterable, so you can use the .each method to iterate over the elements (Wrappers).

Example

final array = p6.wrap.of ['fee', 'foo', 'bar']
array.each { element ->
    // element is a Wrapper (StringWrapper in this case)
    println element capitalize() toString()
}

Tip

To convert the Array to a string, use the toString method or define the variable as a String

Reading

get

Retrieves the wrapped Array

Syntax
Object[] wrap.get()

Example

final a = ['fee', 'foo']
final value = p6.wrap.of a get()
assert value == a 

get(index)

Retrieves the element at the specified index as a Wrapper.

Warning

The index is zero-based. An out-of-range index returns an empty wrapper.

Syntax
BaseWrapper wrap.get(int index)

Example

final String value = p6.wrap.of ['fee', 'foo'] get 0
assert value == 'fee'

first

Retrieves the first element as a Wrapper.

Syntax
BaseWrapper wrap.first()

Example

final String value = p6.wrap.of ['fee', 'foo'] first()
assert value == 'fee'

last

Retrieves the last element as a Wrapper.

Syntax
BaseWrapper wrap.last()

Example

final String value = p6.wrap.of ['fee', 'foo'] last()
assert value == 'foo'

toString

Retrieves the String representation of the wrapped value joined on the default separator ,.

Syntax
String wrap.toString()

Example

final s = p6.wrap.of ['fee', 'foo'] toString()
final String s = p6.wrap.of ['fee', 'foo']

Predicates & lookup

size

Returns the size of the array.

Syntax
NumberWrapper wrap.size()

Example

final value = p6.wrap.of ['fee', 'foo'] size()
assert value == 2

contains

Checks if the array contains one of the specified values.

Syntax
boolean wrap.contains(Object v1 [, Object v2, ...])

Example

final value = p6.wrap.of ['fee', 'foo'] contains 'fee'
assert value == true
final value = p6.wrap.of ['fee', 'foo'] contains ('fee', 'bar')
assert value == true
final value = p6.wrap.of ['fee', 'foo'] contains 'bar'
assert value == false

isEmpty / isNotEmpty / indexOf

New Feature

Since 6.10.24

Syntax
boolean wrap.isEmpty()
boolean wrap.isNotEmpty()
NumberWrapper wrap.indexOf(Object value)   // -1 when absent

Example

final idx = p6.wrap.of(['fee', 'foo', 'bar']).indexOf('foo') get()
assert idx == 1

Aggregation

sum / avg / min / max

New Feature

Since 6.10.24

Numeric aggregation; each element is coerced to a number. avg of an empty array is 0.

Syntax
NumberWrapper wrap.sum()
NumberWrapper wrap.avg()
NumberWrapper wrap.min()
NumberWrapper wrap.max()

Example

final total = p6.wrap.of([10, 20, 30]).sum() get()
assert total == 60

Transform

sort / reverse / distinct / slice

New Feature

Since 6.10.24

Each returns a new ArrayWrapper; the source is left unchanged.

Syntax
ArrayWrapper wrap.sort()
ArrayWrapper wrap.reverse()
ArrayWrapper wrap.distinct()
ArrayWrapper wrap.slice(int from, int to)   // [from, to), clamped

Example

final sorted = p6.wrap.of([3, 1, 2]).sort() join ','
assert sorted == '1,2,3'

Native Groovy

For closure-based operations (collect, findAll, find, groupBy, inject, …), iterate the ArrayWrapper directly with native Groovy — it is Iterable.

Output

join

Joins the array elements using the specified separator.

Syntax
StringWrapper wrap.join(String seq)

Example

final String value = p6.wrap.of ['fee', 'foo'] join '|'
assert value == 'fee|foo'

Combined example

Split a delimited amount list, then dedupe and aggregate it (numeric strings are coerced).

final amounts = p6.wrap.of('100;250;75;250').split(';')

assert amounts.size().get() == 4
assert amounts.distinct().size().get() == 3
assert amounts.sum().get() == 675
assert amounts.max().get() == 250
assert amounts.contains('75')