partition

inline fun <T> Iterable<T>.partition(size: Int): Iterable<List<T>>(source)

Divides an iterable into unmodifiable sublists of the given size (the final iterable may be smaller). For example, partitioning an iterable containing [a, b, c, d, e] with a partition size of 3 yields [[a, b, c], [d, e]] -- an outer iterable containing two inner lists of three and two elements, all in the original order.

Note: The current implementation eagerly allocates storage for size elements. As a consequence, passing values like Int.MAX_VALUE can lead to OutOfMemoryError.

Note: if this is a List, use Lists.partition instead.

Return

an iterable of unmodifiable lists containing the elements of this divided into partitions

See also

Parameters

size

the desired size of each partition (the last may be smaller)

Throws

if size is non-positive


inline fun <T> Iterator<T>.partition(size: Int): Iterator<List<T>>(source)

Divides an iterator into unmodifiable sublists of the given size (the final list may be smaller). For example, partitioning an iterator containing [a, b, c, d, e] with a partition size of 3 yields [[a, b, c], [d, e]] -- an outer iterator containing two inner lists of three and two elements, all in the original order.

The returned lists implement RandomAccess.

Note: The current implementation eagerly allocates storage for size elements. As a consequence, passing values like Int.MAX_VALUE can lead to an OutOfMemoryError.

Return

an iterator of immutable lists containing the elements of iterator divided into partitions

See also

Parameters

size

The desired size of each partition (the last may be smaller)

Throws

if size is non-positive


fun <T> List<T>.partition(size: Int): List<List<T>>(source)

Returns consecutive sublists of a list, each of the same size (the final list may be smaller). For example, partitioning a list containing [a, b, c, d, e] with a partition size of 3 yields [[a, b, c], [d, e]] -- an outer list containing two inner lists of three and two elements, all in the original order.

The outer list is unmodifiable, but reflects the latest state of the source list. The inner lists are sublist views of the original list, produced on demand using List.subList, and are subject to all the usual caveats about modification as explained in that API.

Return

a list of consecutive sublists

See also

Parameters

size

the desired size of each sublist (the last may be smaller)

Throws

if partitionSize is non-positive