Arbitrarily Deep Nested Pattern Matching
Date : March 29 2020, 07:55 AM
Does that help The suggested solution There seems to be no built-in construct to pattern-test nested heads automatically. We can achieve the goal by writing a function which would, for any given (sub)expression of the form f[___]...[___], efficiently determine f (which, with a slight abuse of terminology, we may call a symbolic head for the expression). Here is the code: ClearAll[shead];
SetAttributes[shead, HoldAllComplete];
shead[expr_] := Scan[Return, Unevaluated[expr], {-1}, Heads -> True];
In[105]:= Cases[{f[1], g[f[1]], f[1, 2, 3][1], f[1][2][3][4]}, x_ /; shead[x] === f]
Out[105]= {f[1], f[1, 2, 3][1], f[1][2][3][4]}
Clear[headPattern];
headPattern[head_] := _?(Function[Null, shead[#] === head, HoldFirst]);
In[108]:= Cases[{f[1], g[f[1]], f[1, 2, 3][1], f[1][2][3][4]}, headPattern[f]]
Out[108]= {f[1], f[1, 2, 3][1], f[1][2][3][4]}
In[110]:= m = n = 0;
g[x_] := n++;
h[x_] := m++;
{Cases[Hold[f[g[1]][h[2]]], x_ /; shead[x] === f :> Hold[x], Infinity], {m, n}}
Out[113]= {{Hold[f[g[1]][h[2]]]}, {0, 0}}
sheadEval[expr_] := Scan[Return, expr, {-1}, Heads -> True]
In[114]:= {Cases[Hold[f[g[1]][h[2]]], x_ /; sheadEval[x] === f :> Hold[x], Infinity], {m, n}}
Out[114]= {{Hold[f[g[1]][h[2]]]}, {2, 1}}
|
How to render an arbitrarily deep nested list?
Date : March 29 2020, 07:55 AM
help you fix your problem Use a recursive template, e.g. like this: var tree = {
subItems: [
{
name: "A",
subItems: [ { name:"AA", subItems: [] }, { name:"AB", subItems: [] }, { name:"AC", subItems: [] } ]
},
{
name: "B",
subItems: [ { name:"BA", subItems: [] }, { name:"BB", subItems: [{name:"BB1 (etc)", subItems: []}] } ]
}
]
};
ko.applyBindings(tree);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<script type="text/html" id="myTemplate">
<ul data-bind="foreach: $data">
<li>
<label data-bind="text: name"></label>
<div data-bind="template: { name: 'myTemplate', data: subItems }"></div>
</li>
</ul>
</script>
<div data-bind="template: { name: 'myTemplate', data: $root.subItems }"></div>
|
How to delete something in a JavaScript object that can be nested arbitrarily deep?
Date : March 29 2020, 07:55 AM
wish helps you I have a JavaScript object that manages all of my app's data. It looks something like this: , This'll do it. remove = function(address, tree) {
if (address.length === 1) {
delete tree.children[address[0]];
} else {
remove(address.slice(1), tree.children[address[0]]);
}
}
|
Coping with arbitrarily deep nested lists
Tag : list , By : Brian Drum
Date : March 29 2020, 07:55 AM
|
Flattening arbitrarily deep nested lists
Tag : python , By : semicolonth
Date : January 02 2021, 06:48 AM
Any of those help You can use a recursive generator to yield elements from nested lists: from typing import Collection
def check_nested(obj):
for sub_obj in obj:
# tuples, lists, dicts, and sets are all Collections
if isinstance(sub_obj, Collection):
yield from check_nested(sub_obj)
else:
yield sub_obj
l = [[[[[1, 2]]]]]
list(check_nested(l))
[1, 2]
# This will work for other formats
l = [[[[[1, 2]]]], [[3, 4]]]
list(check_nested(l))
[1, 2, 3, 4]
|