MCPcopy Index your code
hub / github.com/clips/pattern / flatten

Method flatten

pattern/vector/__init__.py:1576–1586  ·  view source on GitHub ↗

Flattens nested clusters to a list, down to the given depth. Cluster((1, Cluster((2, Cluster((3, 4)))))).flatten(1) => [1, 2, Cluster(3, 4)].

(self, depth=1000)

Source from the content-addressed store, hash-verified

1574 return max([0] + [1 + n.depth for n in self if isinstance(n, Cluster)])
1575
1576 def flatten(self, depth=1000):
1577 """ Flattens nested clusters to a list, down to the given depth.
1578 Cluster((1, Cluster((2, Cluster((3, 4)))))).flatten(1) => [1, 2, Cluster(3, 4)].
1579 """
1580 a = []
1581 for item in self:
1582 if isinstance(item, Cluster) and depth > 0:
1583 a.extend(item.flatten(depth-1))
1584 else:
1585 a.append(item)
1586 return a
1587
1588 def traverse(self, visit=lambda cluster: None):
1589 """ Calls the given visit() function on this cluster and each nested cluster, breadth-first.

Callers 2

hierarchicalFunction · 0.95
centroidFunction · 0.45

Calls 2

extendMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected