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 betweenaianda(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 ofarrrow by row.truemeans split between the current position and the next element.arr0, ...: one or moreARRAY<T>.lambda: alambdaexpression 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
arrandflagsdo not match, an error is thrown.
Usage notes
- If a position in
flagsisNULL, it is treated as no split (equivalent tofalse). - The splitting rule of
ARRAY_REVERSE_SPLITis: at each position wherefi==true, split betweenaianda(i+1). - The splitting rule of
ARRAY_SPLITis: at each position wherefi==true, split betweenaianda(i-1).
Examples
-
Basic splitting: at each
trueposition, 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
NULLinflags:NULLis treated the same asfalse(no split).ARRAY_REVERSE_SPLIT([1,NULL,3], [false,null,false])->[[1,[NULL,3]]
-
lambda= x -> x-1applied 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-yapplied 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]]