[Question]
  [
[Add++](https://github.com/cairdcoinheringaahing/AddPlusPlus), the [Language of the Month](https://codegolf.meta.stackexchange.com/q/24222/), has the "collect" builtin as `BC`. Your task is to implement this builtin.
---
Consider a non-empty array, where each element is either:
* A positive digit between `1` and `9` inclusive, or
* A non-empty list of positive digits between `1` and `9`
For example, all the following such arrays meet this definition:
```
[1, 2, 3, 4, 5]
[7, [5, 3], 2]
[1, [2, 2], 3, [4, 4], 5]
[[1], 2, 3, [4], 5, [4], 2]
[[9, 4, 2]]
[[1], 2, 3, 4, 5, [6], 7]
[6, [1, 9, 4]]
```
Note that elements can be repeated, both the digits, the inner lists and the elements of the inner lists.
The `BC` builtin "collects" adjacent digits in the array, and groups them into a list, while leaving the existing lists untouched. Applying this to the above lists, the examples make this clear:
```
[[1, 2, 3, 4, 5]]
[[7], [5, 3], [2]]
[[1], [2, 2], [3], [4, 4], [5]]
[[1], [2, 3], [4], [5], [4], [2]]
[[9, 4, 2]]
[[1], [2, 3, 4, 5], [6], [7]]
[[6], [1, 9, 4]]
```
For example, with `[[1], 2, 3, 8, 9, [6], 7]`, we group the `2, 3, 8, 9` together, and the `7` to give `[[1], [2, 3, 8, 9], [6], [7]]`
You should take a ragged list as input, that meets the list format described above, and output a list of lists after the `BC` builtin has been applied to the input. This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest code in bytes wins.
## Test cases
```
[[9, 4, 2]] -> [[9, 4, 2]]
[6, [1, 9, 4]] -> [[6], [1, 9, 4]]
[7, [5, 3], 2] -> [[7], [5, 3], [2]]
[1, 2, 3, 4, 5] -> [[1, 2, 3, 4, 5]]
[1, [2, 2], 3, [4, 4], 5] -> [[1], [2, 2], [3], [4, 4], [5]]
[[1], 2, 3, 8, 9, [6], 7] -> [[1], [2, 3, 8, 9], [6], [7]]
[[1], 2, 3, [4], 5, [4], 2] -> [[1], [2, 3], [4], [5], [4], [2]]
[9, 8, 7, 6, [5, 4, 3], 2, 1] -> [[9, 8, 7, 6], [5, 4, 3], [2, 1]]
[[8, 2], 9, 5, 1, [6, 4], [4, 5]] -> [[8, 2], [9, 5, 1], [6, 4], [4, 5]]
[7, [5, 3], 2, [1, 2, 3], 9, 8, 7, 8, 9, [3, 4], [1], [2]] -> [[7], [5, 3], [2], [1, 2, 3], [9, 8, 7, 8, 9], [3, 4], [1], [2]]
```
  
[Answer]
# [R](https://www.r-project.org/), ~~76~~ ~~75~~ 71 bytes
Or **[R](https://www.r-project.org/)>=4.1, 64 bytes** by replacing the word `function` with a `\`.
*Edit: -1 byte thanks to [@Dominic van Essen](https://codegolf.stackexchange.com/users/95126/dominic-van-essen).*
```
function(x){y[]=cumsum(c(T,y<-sapply(x,is.list))|y);tapply(x,y,unlist)}
```
[Try it online!](https://tio.run/##hVJNa8MwDL3vV/hWG7xB3HzS9l/sluZQAoFAm5YlgZpuvz2zZHm13cAukSzpSS9P@lq6w9LNQzv114HfxUPXzaGdL@N84S3/lHr/Pp5ut7Pmd9mPH@d@nIT41mI3uaiW84Dhn6Xj4NhPJVkqmRKCiR1rTxPf1DXFmuY4HIeNeKP6XDK0iWRQEUDyRrKaEjGuIFwm2VbALA9XAA4SYF8mmobK5JBO5sOCxAoKrYJZWIXPFDjHfXAs/KuxyAGqkFPc1bYWbnCJ/2p1McFirSuVwQv1Kf7paWkCR89Xq52RquXpvBf1Khxv1M@fC0hpB5Il0caplPaRupWYwlXWpRW3QrJO8dxK7OTOghMpSWaCoCok9toW46t5Hp@ykT/O3iq2HoHEOeFxRxdnz9ZJGvTEmyCGSaDw8gs "R – Try It Online")
### Explanation
```
function(x)            # take x as input
{
y[]=                   # assign to elements of y (to preserve length of y)
cumsum(                # cumulative sum of
 c(T,                  # TRUE followed by
 y<-sapply(x,is.list)) # which elements of x are lists themselves
                       # (assign this to y)
|y                     # or y
);                     # this creates a mask on the positions of lists or first non-list elements 
tapply(x,y,unlist)     # apply unlist to x splitted by y
}
```
[Answer]
# [Python 2](https://docs.python.org/2/), 68 bytes
```
lambda L:eval(re.sub("[^\d]{3,}","],[",`[[0,L,0]]`))[1:-1]
import re
```
Textually replaces every stretch of more than 2 non digit characters (i.e. everything that is not ", ") with "],[". A little trickery at the ends is required to guarantee balanced brackets.
[Attempt This Online!](https://ato.pxeger.com/run?1=bZLNTsJAEMcTj_sUk562yUJogfKR1Ise8QmGUUooYZNCm1IkxvgkXkiM8Zn0aezuDkKRU3dm_vObr75_FS_VKt-Eh49lPP3cVcvW8Ps-S9bzRQKTcfqcZLJM29vdXHr4OF3Qa1e9ecojhZ6aIXbURHWIZr6PwbgVkNDrIi8rKFNm3e1XOkshGAvQKocY1kkhDVaVyf5Jb4pdJf32tsh0JT1o3YLn-wKKUm8quZTaj-Pcd6jDz80D4khBT0FIZLRnpsBIAQYKjOcYjejcJ3BQm30FXTIpTjKgkw8tp9aHtW3BfVY1fU6EoaFYL_ZMgTM5ncJoySxAm2zjjje0zdlGBxfJHCUO1502U9FW5G94mUwcqCseX3a6kaXWi4jc3D1eh4LgtFJWUEOCVmN6GLrJRra82UTE07ntOAyLkFX0T9Y8hzsUd_7XAq-ny4kBj3H1dA0CNhB0hSGE-61-AQ)
#### Old [Python 2](https://docs.python.org/2/), 73 bytes
```
lambda L:[map(int,x[1::3])for x in re.findall("(?:.\d.)+",`L`)]
import re
```
[Attempt This Online!](https://ato.pxeger.com/run?1=bZLNasJAEMehxzzFkNMuXQOJGjWQ9tBTwT7BuNQUDS7EJKSx2mfpRSilz9Q-Tfdj_Ij1lJ2Z3_znKx_f9Xu7qspo_5mns69Nm_fGP49Ftn5ZZDBNcJ3VTJWt2GGYJH3J86qBHagSmmWQq3KRFQXz2X0SzBYBv_XFfDrn0lPrumpazZDgw3aliiWEiQdKVJCCkV2-ZYVosu2zKutNy3jwWheqZT707sDn3IO60ZVZzhRP04o7qf3vzRPiRMBAQCSlYc9MD2MBGAownkM0luc-D0faHAroS5PikJE8-dDqaD7SthUeEtX1OQgjo2K9ODAFznB5CqNVJgBtso07vbFtzjY6ukimqKSw7rSbirYifaPLZEkBXfHwstNNrKpeROzmHtA6BISnlRIhOwhaxvQwdpNNbHmziZimc9txMgQhUfIf1j2HOxR1fmyB1tOnxJDGuHq6jgJ2JOQVDc9zv9Uf)
Just a stoopid regex that finds stretches of digits alternating with two arbitrary other characters (in the repr of the input).
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), ~~30~~ 27 bytes
```
##&@@@#&/@Split[#,#>0<#2&]&
```
[Try it online!](https://tio.run/##VY/NCoMwEITvvkYgpyk1/gut5BFaPBYPIkqFKqXY0xJf3e5GCu0lYWY/Znandrn3U7uMXbsN500pba1V@mjr52NcbgqqCk8q0o3eru@xX@zlNc7sH6rBqkavddfOKwVEJRJEziGgDGTA0osclCJ2PGJhECFmLt0FRWLHoITh3STjvgwoc8h/TRLKvz6tRIEcmRQkvgLG04WMSya5IZNkksa/ZbCvIphkFPyTCWE4QMqIDwnc9gE "Wolfram Language (Mathematica) – Try It Online")
```
          Split[#        ]  group adjacent items which
                 ,#>0<#2&     are both positive (non-lists)
##&@@@#&/@                  remove extraneous brackets
```
At times like this, it'd be nice if `@@`, `@@@` generalized to more `@`s...
[Answer]
# [J](http://jsoftware.com/), 22 19 bytes
```
<@;;.1~2+./\1,#@$&>
```
[Try it online!](https://tio.run/##y/qvpKeepmBrpaCuoKNgoGAFxLp6Cs5BPm7/bRysrfUM64y09fRjDHWUHVTU7P5rcqUmZ@QrpCkYWhtZG1ubWJtamymYW1tYa@hYagIJQwNNa0NDiCJ1x7wUhdTE5AyF5NScHIXMYoW8/HKFRIWczOISPT09dYgqoKmEjPsPAA "J – Try It Online")
-3 thanks to Bubbler
This is a little tricky in J, which requires boxes for heterogeneous arrays. The task becomes to "collect" iff the box contents are atoms. Contents which are already lists (either 1 items or more) should remain as single boxes.
Our strategy is then to "cut" (start a new group) unless adjacent elements are both atoms.
[Answer]
# [J-uby](https://github.com/cyoce/J-uby), 46 bytes
```
:slice_when+:|%([~:*&0|:!=&0]*2)|:map+:flatten
```
[Try it online!](https://tio.run/##bZDRCoIwFIbve4p1kZQu0KlTBz7JYYSFUmEiZEQgvfrazmZ64dXYd85/zrfdj6/zRzWlEs/2dqlP72vdBWLc7eErfC8cxbb0QumzwygeVR@Ipq2Goe5UTxoAKChJKGFSErlBwimBiBLDZ5hpmFISS9M6Qd3FNMMB6ZICM21YgsTMWdYhklMuxzXANcjW6oBJd857C0xqJW6tEidGSTRPya1CgQOME7cigLKrD7MPZ/b2X@IkYxc3doDfpX4 "J-uby – Try It Online")
Today I learned about `slice_when`, and J-uby's `+`.
-4 from translating GB's idea.
# [Ruby](https://www.ruby-lang.org/), 49 bytes
```
->a{a.slice_when{_1*0!=0||_2*0!=0}.map &:flatten}
```
[Attempt This Online!](https://ato.pxeger.com/run?1=bZBNCsJADIVx6ynixoVMpTP9sRUq3iMMpUqLghbRFpHqSdwU0UPpaZzJtNZFV5l5yUu-5P48lqtL_ciiV1lkVvDm1iKpkulpt12n8XmT5lXMJ_Yosq_XWNDjNt0nBxjPs11SFGl-M8bPYHmADBFDBi4DISXIISk-A-QMtN6JMyV6DBypS1tRVQmlUQPvX0WhyyiFru7zn0cuW19AY9BXwqwvj-RsYjc3JKdC8g2V24Ax4F2XwCCE1EAz-QYECbZ3MbO4ML_fkAbSaeyaDulc5pB1beIX)
-6 bytes from GB.
[Answer]
# [Japt](https://github.com/ETHproductions/japt), ~~6~~ 5 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
If the sub-arrays in the input could be nested another level deep in the output then the last 3 bytes could be removed.
```
ó* mc
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=8yogbWM&input=WzEsMixbOSwyXSwzLDQsNSxbNiw3LDgsOV0sNSw0LDMsMiwxLDgsOF0KLVE)
[Answer]
# [Factor](https://factorcode.org/) + `splitting.extras`, ~~43~~ 42 bytes
```
[ [ array? ] split*-when [ flatten ] map ]
```
[Try it online!](https://tio.run/##bVDBTsMwDL33K3xGWkTWla1w2HHiwmXiVPUQFY9VlCwkRmOK8u3FSQqdBIniWO/lPTs@qI5OdnzePz7t7kFZqy4OnBl6ol6/Cvwiqxy8odU4gMOPT9QdujkTL4gGjEWii7G9JngoCl8AL8@7hhUsIUCYkDs@MqEztuZTQcnI8heTnJf8qrpCPGPRq@RslRxmNvIhaTbsHusE9v3L@kmX77lezbp16q5iJvcir/SbVLlmVib3qPa5v3//8fODMDnnruQtSJmUOebJhLGBJo9@C20e/s3ifETN8GFQRJy18K4MtGOMDQghGEHVHcdv "Factor – Try It Online")
## Explanation
* `[ array? ] split*-when` Split on arrays (sublists) and keep the delimiters.
* `[ flatten ] map` Flatten each element. `flatten` doesn't touch arrays of depth 1 but otherwise decreases depth to 1.
```
                        ! { 1 { 2 2 } 3 { 4 4 } 5 }
[ array? ] split*-when  ! { { 1 } { { 2 2 } } { 3 } { { 4 4 } } { 5 } }
[ flatten ] map         ! { { 1 } { 2 2 } { 3 } { 4 4 } { 5 } }
```
[Answer]
# [Ruby](https://www.ruby-lang.org/), 46 bytes
```
->l{l.chunk{|x|x*0}.flat_map{|a,b|a==0?[b]:b}}
```
[Try it online!](https://tio.run/##BcHhCsIgFAbQV@n3@BItNyuwHuRyCR3IIAuJBIf67HbON/t9BDuO91ijWLf8edVWWplkFyG63/PtUm0Ovjlr5YM833zvIx0CkcIJZ5BmzFhgQBdcGWQMlGbm8Qc "Ruby – Try It Online")
[Answer]
# Haskell + [hgl](https://gitlab.com/WheatWizard/haskell-golfing-library), 21 bytes
In order to do this I needed to add support for `Either` types to hgl.
I was planning on doing this eventually and I don't think knowledge of this challenge influenced the way things were implemented.
So this is *non-competing* for whatever that means to you.
```
fR p<tv(mst p)<+gB ø
```
This takes a `List (Either Int (List Int))` to represent a ragged array.
Surprisingly we can do flip the order of the `Either` at no cost to bytes.
```
fL p<tv(mst p)<+gB ø
```
## Relfections
There are some things here that could be improved.
* `m<mst` should get it's own command. It's not used here but if I had had it this could have been 1 byte shorter:
```
fR p<sQ<m2t p<+gB ø
```
where `m2t = m<mst`. (Implemented in [871c2406](https://gitlab.com/WheatWizard/haskell-golfing-library/-/commit/871c240698fa7d0420ccb4115b4e51619607587d))
* There needs to be a `Bitraversable` class. The whole reason we have `mst p` a very costly construct is that we need to massage the correct monoid instance onto the left hand side of the `Either`. If we had `Bitraversable`, the entire `tv(mst p)` could just be a `bisequence`.
* Other `Bifunctor` classes might have also helped here and are probably needed in the long run. `Either` isn't the only type that would benefit from this. We have other bifunctors like `(,)` and `These` which would benefit as well.
* Here I expressed ragged lists as lists of either the element or more lists. This works for ragged lists of max depth 2 fine, but it would also be possible to represent ragged lists more generally using the free monad combinator. If I had that I could have tried an approach with that, and even so I should probably have that for future challenges where the depth restriction might not exist.
Most of these basically boil down to "better bifunctor" support, so I think some of that is warranted.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~12~~ 11 bytes
```
ŒḊ€aJŒg⁸ṁF€
```
[Try it online!](https://tio.run/##y0rNyan8///opIc7uh41rUn0Ojop/eHORodDW92A3P@H24EyO2f8/x9trqMQbaqjYByro2AEZBqCKRDPUkfBQkfBHEwC2dHGOgomsSAVIMIoNhYA "Jelly – Try It Online")
```
ŒḊ€   Depth of each (1 for lists, 0 for numbers)
aJ    Replace each 1 with its index  [0 0 1 0 1 1] -> [0 0 3 0 5 6]
Œg    Group runs of equal elements [[0 0] [3] [0] [5] [6]]
⁸ṁ    Reshape the original input to this list
F€    Flatten each element (turns entries like [[1 2]] back into [1 2])
```
This approach feels elegant in theory, but a lot of bytes are wasted on `€@µ€`, so there might be saves.
Jonathan Allan saved a byte! Thanks.
[Answer]
# [Python 2](https://docs.python.org/2/), ~~84~~ 79 bytes
```
s=[]
for x in input()+[{}]:
 if x>9:
    if s:print s
    s=x[:0];print x
 else:s+=x,
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m728oLIkIz_PaMGCpaUlaboWN_2LbaNjudLyixQqFDLzgKigtERDUzu6ujbWikshM02hws7SiosTyCi2KijKzCtRKObiLLatiLYyiLWGCFRwKaTmFKdaFWvbVuhAjIWavmBfdLRhrI6CkY6CsY5CtAmQaaqjYAllGsVCVAEA)
-5 bytes thanks to @ovs
[Answer]
# [Vyxal 2.7.1](https://github.com/Vyxal/Vyxal), 6 bytes
```
ƛf⁼;Ġ•
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyIiLCIiLCLGm2bigbw7xKDigKJ2ZiIsIiIsIls3LCBbNSwgM10sIDIsIDRdIl0=)
The link uses the latest version of vyxal (2.7.3 at the time of writing) which has a bug fix where molding no longer wraps singletons in lists, hence the extra `vf`.
## Explained
```
ƛf⁼;Ġ•
ƛf⁼;     # For each item in the input, does the flattened version equal the item? For integers, this returns a list, which does not equal the item. For lists, this just returns the list - this wouldn't work if there wasn't any depth restriction. 
   Ġ    # Group on consecutive items
    •   # and mold the input to that shape
```
[Answer]
# [Add++](https://github.com/cairdcoinheringaahing/AddPlusPlus), 6 bytes
```
L*~,BC
```
[Try it online!](https://tio.run/##S0xJKSj4/99Hq07Hyfn////R5grRpgrGsQpGsf/yC0oy8/OK/@tmAgA "Add++ – Try It Online")
~~Wow holy crap a challenge where Add++ will probably win.~~ Nevermind I tied myself with vyxal.
## Explained
```
L*~,BC
L*~,   ; a lambda that dumps its arguments onto the stack and returns the entire stack that:
    BC ; calls the built-in this challenge is all about. 
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 11 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
Really not sure if this is optimal, it's just the first way I though of...
```
,⁻;ɗƝŻœṗ⁸F€
```
**[Try it online!](https://tio.run/##XVC7DcIwEO0zxQ1wjZ3EcUTPDujkkgZlAcrQ0EMRGjZgAKKUyEiMkSxizmcLoTS238/v7MO@644h4NJPm8/wvvvJX@dxWPrndjk9wuvMq7/M4433XQhUELUIFYJ2DgsyCKQQIiW4YVwjlC4aGLOmGUqizgRpESNLVQxmiZT7d7NqmGhWEok/79LB5ZZ9CCaVV7kfQUnWprpWYrHfpFKSmdZTpwfphH5XWzlTmZNxHJIfcF8 "Jelly – Try It Online")**
### How?
```
,⁻;ɗƝŻœṗ⁸F€ - Link: list, A
    Ɲ       - for each of the neighbours:
   ɗ        -   last three links as a dyad, f(left, right):
,           -     pair them        (e.g. [1,2] and 3 -> [[1,2],3]])
  ;         -     concatenate them (e.g. [1,2] and 3 -> [1,2,3])
 ⁻          -     are these not equal? (i.e. was either left or right a list?)
     Ż      - prefix a zero
        ⁸   - use A as the right argument of...
      œṗ    - partition right at truthy indices of left
         F€ - flatten each
```
[Answer]
# [Retina 0.8.2](https://github.com/m-ender/retina/wiki/The-Language/a950ad7d925ec9316e3e2fb2cf5d49fd15d23e3d), 34 bytes
```
(?<=^\[|], )[^][]+(?=, \[|]$)
[$&]
```
[Try it online!](https://tio.run/##XU@7DoJAEOz5ii3QQNyGAw5IJJR@xLhECwsbC2Lpv5@3e6eJVpd57cxtt@f9cQ276nQJ1XKc1zNewlRjFcihWmYmZcq6QLmXEICJqWNyIgU8ExomZRQOEfZMrahcqOIiMnufMJxKRqLTVFLQyMc72jn4SAy/CsydX70/mTuW@tTb5WqmRpNjqpospN0@FcLm/M1NH3EJfQ/nMW0O6hbEj78B "Retina 0.8.2 – Try It Online") Link includes test cases. Would be 32 bytes if I could be bothered to remove all of the spaces from the lists. Explanation: Simply matches sublists composed of non-lists that are between two lists or positioned at the start and/or end of the main list and wraps them in a list. Using lookarounds to anchor the match avoids having to explicitly capture the sublist thus saving a byte.
```
(?<=^\[|], )
```
Match at the beginning of the main list or after a list.
```
[^][]+
```
Match anything that isn't a list.
```
(?=, \[|]$)
```
Match at the end of the main list or before a list.
```
[$&]
```
Wrap the sublist in a list.
[Answer]
# [Python 3](https://docs.python.org/3/), 87 bytes
```
lambda L:[[*x]for t,g in groupby(L,type)for x in[g,[g]][t==int]]
from itertools import*
```
[Try it online!](https://tio.run/##bZHJjoJAEIbvPEVlTmBaE1FxSfAJfIOaOmAE7IQtTTuRp2d6KUdxPEHV/9VfS3eDvrbNaizS77HK6vMlg9MBcXanolWgRQmygVK1t@48hCehhy6PrHI3eSwFlkSo01Q2migoVFuD1LnSbVv1IOuuVXo2@i/0Qx/Y0ko2uXU18aLXF9kcAoAqL7QAJcurhhTqrAvzn6wSDl70XSV1@DU/fkWRYVXe3yqLFaEts6lOmQnCh5B6I8FkNCLuBawFxEQwP8JLGGAiAJcCbOahJvSaC3Brwo2AFdkSj2zpmUPnY/jYxM54w9Q05yGMrYvL4to2eMHpKaNzZgBdsdO9384N5wbdvhWzSiybSael6DryN34vJhZMx8ef227vXM0hEr/3ms8hYPk8KRM0QdAxdoad32zv2ttLJLydv463YQiZon/Y9Dn8Q/HkfyPweVZcuOQ1Pj7dxAEnFvTB4xc "Python 3 – Try It Online")
Group the list by type (int or list), then wrap the int groups in list.
I thought `groupby` would be great for this job, however the `itertools` import ended up costing too many characters.
# [Python 2](https://docs.python.org/2/), 84 bytes
```
def f(L,s=[]):t=s and[s];return L and[f(L[1:],s+L[:1]),t+L[:1]+f(L[1:])][L>[[]]]or t
```
[Try it online!](https://tio.run/##bZHbjoJADIbveYrGK4ijCaiobPAJeIOmFybCOgkiYcZNfHp2plMP7HoF/fv172H6uz1fu2wcT3UDTVwpUyIlhS0NHLsTGvoaansbOqg4dgSmBSkzr7BIKVE2/MwlkRBWB0Qiug5gR33pr4MFczdR44RWdzXozsdLY0@6KyKAtm6sgkF/ny2UcDn2cf1zbBXDS9O32sazxWGWJI4danNrPdbEvsxL/aA7@0yUwUiJMCLuFawVZESwOMBbGGGuAFMFXnlkc3rXIty6cKNgRb4kIFt6acg@js9czMYboaZagDDzLqzi2jd4w@mVRnYWALmY88Fvx8PxoNs/xZIlSbtJp6XIHeWb/S0mSbiOjz/ebs@u7hB52Hst51CQvk4qBE0QZMbPsAub7bm9v0Qu24XrBBuBUCj6h02fIzyUTP4cQc6zksJU1vj4dBMHnFjQB49f "Python 2 – Try It Online")
```
f=lambda L,s=[]:L and[f(L[1:],s+L[:1]),(s and[s])+L[:1]+f(L[1:])][L[0]>9]or s and[s]
```
[Try it online!](https://tio.run/##bZHbjoJADIbveYrGK4ijWdBFIcEn4A2aXrAR1kkQCcNu4tOzM516wPUK@vfr38P01/F06ZJpaoq2On8dKyiVKZDyEqruiE1YYpyTMssS85giFRrWDUVeWQoREZb4QYeMLgPcmEmf@8swgrmaoLF6q7sadOfitRmPussDgLZuRgWD/j6NUMC56sP6t2oVw2vTt3oMF6vDIoosO9Tmp3VYE7oyJ/WD7sZ7ovBGSoQJMVOwVZAQweoAT2GAqQKMFTjllk3pWQtwZ8NPBRtyJR7Z0UND9rF8YmM2/hRqrnkIE@fCKm5dgyecHmlkZwGQiznv/fY8HA@6eymWLEnaTjovRe4o3@S1mCRhO97@eLuMXe0hUr/3Vs6hIH6cVAiaIciMm2HvN8u4vbtEKtv563gbgVAo@ofNn8M/lEx@H0HOs5HCWNZ4@3QzB5xZ0BuPPw "Python 2 – Try It Online")
I tried to golf [@pxeger's answer](https://codegolf.stackexchange.com/a/240515/92237) by using recursive function, but wasn't able to save any bytes.
These 2 functions simply loop through each element. If the current element is a digit, accumulate it in the list `s`. If the current element is a list, add the accumulated elements (`s`) if not empty to the result, followed by the current element.
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 57 bytes
```
a=>a.flatMap(c=n=>n[0]?c=[,n]:c[0]?c.push(n)&&[]:[c=[n]])
```
[Try it online!](https://tio.run/##fZLBboMwDIbve4qcKpAyOiiF0onuCfYElg8RK90mFNDo9vosMR6QkvVEZH/@bf/4U/2ovvr66K6Pun07D3U5qPKkorpR11fVBVWpy5OGJ3ypSpAajxW9o@67fw90uNkAHsGkNGI4VK3u2@YcNe0lqAOAQopUisSkwmex3YpF5OGGzaSAWAqbX@AZLsO3NblJ7qXYoVWcanKcw@DpZOQSk6VB9nOZG/ZVQWL7EAOpHcitx5kA6s0MeNSIHtsdaDvaNF@rMYBMmOXuaQHNxN/Eo4acMzP9vTwOFdTUuJuNTqbssRSx8ycZQocCwtZTHkZrChrQupmxPaPfky5zwCCuyHtXMF4LbzpNyBbvWCbmtf@7GEcEHBX0yAy/ "JavaScript (Node.js) – Try It Online")
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 6 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
.γd}€˜
```
[Try it online](https://tio.run/##yy9OTMpM/f9f79zmlNpHTWtOz/n/PzraMFbHSMdYx0THVCfaLFYn2jxWx0LHMhYA) or [verify all test cases](https://tio.run/##yy9OTMpM/V9Waa@k8KhtkoKSfeV/vXObU2ofNa05Pee/zv/oaEMdIx1jHRMd01gdhWhznWhTHeNYHSMQx1An2gjEMtaJNtExiYWoiDaMBWuIBgmASbDaaEugEUaxyCpMQPJmsTrmIEEzHaBxQDVYVUQDlVjoWMbGAgA).
**Explanation:**
```
.γ     # Adjacent group-by:
  d    #  Transform every integer into a 1 (with a >=0 check)
   }€  # After the group-by: map over each group:
     ˜ #  Flatten it
       # (after which the list of lists is output implicitly as result)
```
[Answer]
# [Burlesque](https://github.com/FMNSSun/Burlesque), ~~13~~ 11 bytes
```
{n!z?}gB)FL
```
[Try it online!](https://tio.run/##SyotykktLixN/V9dlPi/Ok@xyr423UnTzed/bXjO/2hDHQUjHQVjHQUTHQXTWK5ocx2FaFOgQCxQHMgFSkcbgZhgNdFARSaxEIXRhrEwrdFgMSgN0hVtCTbPKBZVnQlElRlQwBwoYwZkA80HqY2NBQA "Burlesque – Try It Online")
```
{
 n!   # Not for ints (if 0 then 1 else 0); most common element for lists
 z?   # Is zero
}gB   # Group by is list
)FL   # Flatten each resulting list (double groups those already as lists)
```
Since the lists are always non-zero we can get away with this hack.
An alternative is doing a list/int only op and then seeing if there's an error (e.g `tpis` transpose-iserr) unfortunately no single op I know can test for `is list`
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 29 bytes
```
⊞υ⟦⟧ＦＡ¿⁺ι⟦⟧⊞⊞Ｏυι⟦⟧⊞§υ±¹ι⭆¹Φυλ
```
[Try it online!](https://tio.run/##JYwxD4IwEIV3fsWNd0kdAA0aJhcTBpXEsenQYJEmDZDSGv99bcvy3ru8790wSTss0oTQ@21Cz4ALaotxsYDdvHqHRKBHwN74DXWuCTKb5LkqK91i01DTPgZlNrUjV9fNb/VL7UN9pFNYEiWyLXqrZ4cvF@1zlyuWDG7aOJVfGSJqQ@AFb@LPE4NaMKhiLLOl68LgzKDJGjOvGRxFIpJUQhQiHL7mDw "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
⊞υ⟦⟧
```
Push an empty list to the result in case the first element of the input is not a list.
```
ＦＡ
```
Loop through all of the elements of the input list.
```
¿⁼ι⟦⟧«
```
If adding the empty list to the element doesn't produce an empty list, then...
```
⊞⊞Ｏυι⟦⟧
```
... the element was a list, so push it to the result, plus another empty list in case the next element of the input is not a list, otherwise...
```
⊞§υ±¹ι
```
... append the element to the last list of the result.
```
⭆¹Φυλ
```
Filter out any empty lists (e.g. from consecutive lists in the input) and pretty-print the result.
[Answer]
# [Retina](https://github.com/m-ender/retina/wiki/The-Language), 29 bytes
```
["[["|"], ["]"]]"L`\b[^][]+\b
```
[Try it online!](https://tio.run/##XU8xDsIwDNz7CisSEhJZkrZp8wIWfnA1KpUYWBgQI38PsR2QYLLOd@c7P67P2/0Sym5/XAsc4F6OPcGxY3anddlwZvBh2UoBsqfBU2TukKooeJKNwKnC0VPPQnfCxIpUPhpGFEqXGMRlDAJ/tLOeQ6qL6ZeBqtuU@1nVNTRZ7tCiPQVxzhaV1STZyQKhdf7q2iPR0PdwK9M3o3RBffwN "Retina – Try It Online") Link includes test cases. Explanation: Based on @loopywalt's idea. Would be 28 bytes if I could be bothered to remove all the spaces from the lists.
```
L`\b[^][]+\b
```
List all runs of digits and separators (but excluding `[]`s).
```
["[["
```
Precede the entire result with `[[`.
```
|"], ["
```
Separate each run with `], [`.
```
]"]]"
```
Suffix `]]` to the result.
[Answer]
# [Perl 5](https://www.perl.org/) & probable regex polyglot, 53 bytes
```
s/(?<!\]),\[|],(?!\[)/],[/g;s/^\[+(.*[^]])]+$/[[\1]]/
```
[Try it online!](https://tio.run/##DctBCsJAEATAtwQ87JrWIZKViEIe0tO5iQhiFtejb3e07lWvr0eJaJbmS@fKcH6ENHfObALtdm62OPu033KRsvqNkT5IFkFOOAgnFAzgEeN/jCjSd63v@/pssas/ "Perl 5 – Try It Online")
First change all "`],`" or "`,[`" to "`],[`"; then make sure it's all enclosed in "`[[`...`]]`".  
I forgot the second bit on my first attempt, so it seemed a bit golfier than it turned out...
[Answer]
# [Julia 1.0](http://julialang.org/), 81 bytes
```
+(x,y=[])=x>[] ? x[1] isa Int ? x[2:end]+[[y...;x[1]]] : [y;[x[1]];+x[2:end]] : y
```
[Try it online!](https://tio.run/##bVHLboNADLzzFVZOILZIkIS8RHruoV9g@bBRqLQVohHQCr6e7nqdJpsmF@KZ8Xjs/fxujM7HeU7jUU0VUlKNRyR4hRFzAtNreGsHLot93Z4pRZyyLDs4mgj2gNMBuTikV42Dpzn6@OrAgGmhq/W5MW3dx0kE9qfVCSqof3STxe/1oLOL7vo6i/tLY4bYKFjAyxEWSeLll860Q9PGGqojpDoEU4tWcEoiO5iJGXGnYKWgsDmszV0ZYakAcwUOubIl3WMRbmy5VrAk1@IlG7phyD5WX9iajdeiCjEvwsK5MIorN@BOTjca2VkEyM3Me78th@Ogm4dmYUlomzRsRZ4o3@KxmYSwE6//eLsdu9pDlH7vlZxDQX47qSgokCBrXIat32zH490lStnOX8fbiAhFRf9k4XP4h5LkfxHkPEtpzGWNp08XOGBgQU88fgE "Julia 1.0 – Try It Online")
a recursive approach
# [Julia 1.0](http://julialang.org/), 82 bytes
```
f(;y=[])=y
f(x::Int,X...;y=[])=f(X...;y=[[y...;x]])
f(x,X...;y=[])=[y;[x];f(X...)]
```
[Try it online!](https://tio.run/##bVHLasMwELzrK5acZFANdhLnhXPvoffCsgeF2KBi3OC4xf56V491EqXRRdqZ2dmHvn4ao7Nhmmp5GEukpBxFLYf9/r3t1WeapozWcg5wdI@BKHHCRw2OBxzoEKQJTaL@7sCAaaGr9LkxbXWViQB7tDpBCdWvblL5UfU6vejuWqXyemlML42CBbwdYZEkQX7pTNs3rdRQHqGW2tnHDINQlnBKRNWePTthRs4I7U0CcadgpSAnBm@hwEIBZgocMrMFPWICNzZcK1iSSwmSDd0x9D5Wn9vYG6/n2hEWRJg7F4/iyhV4kNOdRu/MAvTJng9@W9@cb3TzlMwsMW07jVPRV@Q7f04mJmzF@eWn23lXu4gizL3idSjI7itlBUUS9BrXwzZMtvPl3SYKni5sJ9iwCFlF/2Txd4SP4s5vLfB6lpyY8Rgvvy5ywMiCXnj8AQ "Julia 1.0 – Try It Online")
the same idea, but with multiple dispatch
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 12 bytes
```
⁽-ḊƛÞj∷ßw;Þf
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyIiLCIiLCLigb0t4biKxpvDnmriiLfDn3c7w55mIiwiIiwiW1s4LCAyXSwgOSwgNSwgMSwgWzYsIDRdLCBbNCwgNV1dIl0=)
[Answer]
# [Arturo](https://arturo-lang.io), 37 bytes
```
$=>[chunk&=>[integer?&]|map=>flatten]
```
[Try it](http://arturo-lang.io/playground?ZYuhbd)
]