array_split
array_split — partitions a list or array into multiple lists or
arrays.
array_split (list_or_array, nparts);
list_or_arrayAny list or array to be partitioned into multiple lists or arrays.
npartsA number indicating the number of resulting arrays, or a function returning an index indicating which result part should contain each input element.
The input list_or_array, broken into parts. If the
input is a list, the result will be a list of lists. If the input is an
array, the result will be an array of arrays.
This function partitions a collection into either a specified number of sub-collections, or into a set of sub-collections based on an indexing function. If the number of parts is specified as a number, the result will be a collection of at most that many sub-collections.
If the number of parts is specified as a function, then it must be a function taking two arguments and returning an integer. The return integer indicates the index of the sub-collection to which the input element is assigned. The indexing function has the form:
function (index,element)
Returning a partition index, where:
indexThe index of this element within the input
list_or_array, starting from
zero.
elementThe element of the input list_or_array
corresponding to this index.
Gamma>players = [ "Matthews", "Marner", "Tavares", "Nylander", "Barrie", "Hyman", "Kapanen", "Kerfoot", "Rielly", "Spezza", "Mikheyev", "Muzzin", "Johnsson", "Holl" ];[ "Matthews", "Marner", "Tavares", "Nylander", "Barrie", "Hyman", "Kapanen", "Kerfoot", "Rielly", "Spezza", "Mikheyev", "Muzzin", "Johnsson", "Holl" ]Gamma> n = 3;3 // Split the array into N subarrays, filling from first to lastGamma>array_split(players, n)[[Matthews Marner Tavares Nylander Barrie] [Hyman Kapanen Kerfoot Rielly Spezza] [Mikheyev Muzzin Johnsson Holl]] // Split the array into subarrays by the first letter of each elementGamma>array_split(players, function(i,x) { local c = tolower(x[0]) - 'a'; c < 0 ? 26 : (c > 25 ? 26 : c); })[nil [Barrie] nil nil nil nil nil [Hyman Holl] nil [Johnsson] [Kapanen Kerfoot] nil [Matthews Marner Mikheyev Muzzin] [Nylander] nil nil nil [Rielly] [Spezza] [Tavares]] // Split the array into approximately equal subarrays // in a round-robin fashionGamma>array_split(players, function(i,x) { i%n; })[[Matthews Nylander Kapanen Spezza Johnsson] [Marner Barrie Kerfoot Mikheyev Holl] [Tavares Hyman Rielly Muzzin]]