Add recursive "apply()" helper to pattern

This commit is contained in:
jan 2018-04-14 14:53:53 -07:00
parent 4c535e6564
commit 3e06214b7e

View File

@ -94,6 +94,22 @@ class Pattern:
pat.subpatterns = [s for s in self.subpatterns if subpatterns_func(s)] pat.subpatterns = [s for s in self.subpatterns if subpatterns_func(s)]
return pat return pat
def apply(self,
func: Callable[['Pattern'], 'Pattern']
) -> 'Pattern':
"""
Recursively apply func() to this pattern and its subpatterns.
func() is expected to take and return a Pattern.
func() is first applied to the pattern as a whole, then the subpatterns.
:param func: Function which accepts a Pattern, and returns a pattern.
:return: The result of applying func() to this pattern and all subpatterns.
"""
pat = func(self)
for subpat in pat.subpatterns:
subpat.pattern = subpat.pattern.apply(func)
return pat
def polygonize(self, def polygonize(self,
poly_num_points: int=None, poly_num_points: int=None,
poly_max_arclen: float=None poly_max_arclen: float=None