I ended up converting it to Python Mode as well:
"""
* indicesOf() & splitListAsList2d() (v1.0.11)
* GoToLoop (2020/Feb/21)
*
* https://Discourse.Processing.org/t/
* splitting-an-arraylist-for-each-pvector-0-0-updated/17883/14
"""
from sys import stderr
DELIM_VEC, EXTRA_DELIM_VEC = PVector(), __pvector__(EPSILON, TAU, PI)
vecs = (
__pvector__(), # 0
DELIM_VEC, # 1
PVector(100, 100), # 2
__pvector__(150, 100), # 3
PVector(150, 150), # 4
PVector(EPSILON, TAU, PI), # 5
PVector(300, 100), # 6
None, # 7
EXTRA_DELIM_VEC, # 8
DELIM_VEC, # 9
PVector(350, 100), # 10
PVector(350, 150), # 11
EXTRA_DELIM_VEC, # 12
DELIM_VEC # 13
)
print vecs, len(vecs)
def setup():
delimIndexes = indicesOf(vecs, DELIM_VEC, EXTRA_DELIM_VEC)
print delimIndexes
global vecs2d
vecs2d = splitListAsList2d(vecs, delimIndexes)
for vecs1d in vecs2d: print vecs1d
print len(vecs2d)
exit()
def indicesOf(container, *delims):
if not container or not _isIterable(container):
print >> stderr, "Parameter 'container' is empty or non-iterable!"
return ()
if len(delims) is 1 and _isIterable(delims[0]): delims = delims[0]
if _isMappable(container): container = container.values()
if _isMappable(delims): delims = delims.values()
indices = []
gotDelims = not not delims
for idx, elem in enumerate(container):
if not elem: indices.append(idx)
elif gotDelims:
for delim in delims:
try:
if elem == delim:
indices.append(idx)
break
except TypeError:
if elem.equals(delim):
indices.append(idx)
break
return tuple(indices)
def splitListAsList2d(container, *indices):
list2d = []
if not container or not _isIterable(container):
print >> stderr, "Parameter 'container' is empty or non-iterable!"
return list2d
containerSize = len(container)
indexes = _validateIndices(containerSize - 1, *indices)
sequenceGotInterrupted = True
if not indexes: return list2d
if _isMappable(container): container = container.values()
elif _isSet(container): container = tuple(container)
for i in range(containerSize):
if i in indexes:
sequenceGotInterrupted = True
continue
if sequenceGotInterrupted:
list1d = []
list2d.append(list1d)
sequenceGotInterrupted = False
list1d.append(container[i])
return list2d
def _validateIndices(maxIndex, *indices):
if len(indices) is 1 and _isIterable(indices[0]): indices = indices[0]
if _isMappable(indices): indices = indices.values()
if not indices:
print >> stderr, "Parameter 'indices' is empty!"
return ()
maxIdx = min(abs(maxIndex), MAX_INT - 3)
indexes = []
errorTypes = TypeError, ValueError
for idx in indices:
try: idx = int(idx)
except errorTypes: continue
0 <= idx <= maxIdx and idx not in indexes and indexes.append(idx)
indexes.sort()
return tuple(indexes)
def _isIterable(obj, PROP='__iter__'): return hasattr(obj, PROP)
def _isMappable(obj, PROP='values'): return hasattr(obj, PROP)
def _isSet(obj, PROP='isdisjoint'): return hasattr(obj, PROP)