I was wondering whether there is a stronger Nothing or whether this can easily be constructed. By stronger I mean the following:
If we have Table[Nothing,{i,1,3}] it will return {}. If we write Table[Nothing,{i,1,3},{j,1,3}] it will return {{},{},{}}. The Nothing cannot delete these lists since it is no longer there. Often I do want a list like this to collapse to {}. Practically, there is a very easy solution to retrieve the output I wish (such as {}->Nothing), but mostly I'm just curious for a solution of this type.
The new nothing would differ from Nothing in that {nothing} -> nothing if {nothing} is an element of a list (instead of to {}).
P.S. To clarify the objective: the new nothing would be applied to get nice lists without empty lists for objects like Table[If[cnd[i],x[i],nothing,nothing],{i,1,n},{j,1,m}].
I am close to an answer with
nothing /: List[nothing] := nothing; nothing /: List[a__, nothing] := List[a]; nothing /: List[nothing, b__] := List[b]; which I think is acceptable for me. However, it gets the wrong result for the outer list {nothing}->nothing if the whole thing turns out filled with nothing but nothing's.
How do I define an object so that it has a different definition depending on whether it is contained in a list or not?
nothing /: List[a__, nothing, b___] := List[a, b]; nothing /: List[a___, nothing, b__] := List[a, b];. Note the addition of triple underscores. That's actually important. By the way, you'll want to wrap those up-values inHoldPatternfor if you want to tweak at all. $\endgroup$nothing /: HoldPattern[List[a___, nothing, b__]] := List[a, b];. PreventsList[a___, nothing, b__]from evaluating toList[a___, b__]but matchesList[a___, nothing, b__]. If you check the*Valuesfor a function you'll see this is how they're stored. $\endgroup$