ARRAY_REVERSE_SPLIT
Function
Split the input array into multiple subarrays according to given boolean flags.
- Splitting rule (left to right): for
arr=[a1,a2,...,an]
andflags=[f1,f2,...,fn]
, at every position wherefi==true
, split betweenai
anda(i+1)
.- For example, with
arr=[3, 4, 5]
andflags=[false, true, false]
, the second flag is true, so split between the second and third elements, resulting in two subarrays[3, 4]
and[5]
.
- For example, with
Syntax
ARRAY_REVERSE_SPLIT(arr, flags)
ARRAY_REVERSE_SPLIT(lamda, arr0, ...)
ARRAY_REVERSE_SPLIT(lambda, arr0, ...)
is equivalent toARRAY_REVERSE_SPLIT(arr0, ARRAY_MAP(lambda, arr0, ...))
Parameters
arr
:ARRAY<T>
.flags
:ARRAY<BOOLEAN>
, whose length must match that ofarr
row by row.true
means split between the current position and the next element.arr0, ...
: one or moreARRAY<T>
.lambda
: alambda
expression applied toarr0, ...
to produceflags
, which are then used for splitting.
Return value
- Returns
ARRAY<ARRAY<T>>
. Elements of inner arrays are the same as those ofarr
. - If the element counts of
arr
andflags
do not match, an error is thrown.
Usage notes
- If a position in
flags
isNULL
, it is treated as no split (equivalent tofalse
). - The splitting rule of
ARRAY_REVERSE_SPLIT
is: at each position wherefi==true
, split betweenai
anda(i+1)
. - The splitting rule of
ARRAY_SPLIT
is: at each position wherefi==true
, split betweenai
anda(i-1)
.
Examples
-
Basic splitting: at each
true
position, split from the right side neighbor.ARRAY_REVERSE_SPLIT([1,2,3,4,5], [false,true,false,true,false])
->[[1,2], [3,4], [5]]
ARRAY_REVERSE_SPLIT(['a','b','c'], [false,false,false])
->[['a','b','c']]
-
With
NULL
inflags
:NULL
is treated the same asfalse
(no split).ARRAY_REVERSE_SPLIT([1,NULL,3], [false,null,false])
->[[1,[NULL,3]]
-
lambda= x -> x-1
applied toarr=[1, 2, 3]
producesflags=[0,1,2]
, equivalent toflags=[false,true,true]
ARRAY_REVERSE_SPLIT(x->x-1, [1, 2, 3])
is equivalent toARRAY_REVERSE_SPLIT([1, 2, 3], [false,true,true])
->[[1, 2], [3]]
-
lambda= (x,y) -> x-y
applied toarr=[1, 2, 3]
andarr1=[0,1,2]
producesflags=[true,true,true]
ARRAY_REVERSE_SPLIT((x,y) -> x-y, [1, 2, 3], [0, 1, 2])
is equivalent toARRAY_REVERSE_SPLIT([1, 2, 3], [true,true,true])
->[[1], [2], [3]]