Skip to content

Commit

Permalink
Add 'strict' argument to 'stutter' and add Generator copy method
Browse files Browse the repository at this point in the history
  • Loading branch information
Qirky committed Jun 29, 2020
1 parent a4f601b commit d1d29db
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion FoxDot/lib/.version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.8.10
0.8.11
1 change: 1 addition & 0 deletions FoxDot/lib/Patterns/Generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ def func(self, index):
self.last_value = f(self.last_value, self.step[index])
return self.last_value


class PDelta(GeneratorPattern):
def __init__(self, deltas, start=0):
GeneratorPattern.__init__(self)
Expand Down
41 changes: 31 additions & 10 deletions FoxDot/lib/Patterns/Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,16 @@ def new(self, data):
return self.__class__(data + self.meta)

def transform(self, func):
return self.__class__([(item.transform(func) if isinstance(item, metaPattern) else func(item))for item in self])
"""
Recursively transforms values and nested patterns
"""
output = []
for item in self.data:
if isinstance(item, (metaPattern, GeneratorPattern)):
output.append(item.transform(func))
else:
output.append(func(item))
return self.__class__(output)

def int(self):
return self.transform(int)
Expand Down Expand Up @@ -550,21 +559,27 @@ def mirror(self):

return self.new(new)

def stutter(self, n=2):
""" Returns a new Pattern with each item repeated by `n`. Use
a list of numbers for stutter different items by different
amount. e.g.
```
>>> P[0, 1, 2, 3].stutter([1,3])
P[0, 1, 1, 1, 2, 3, 3, 3]
```
def stutter(self, n=2, strict=False):
"""
Returns a new Pattern with each item repeated by `n`. Use
a list of numbers for stutter different items by different
amount. e.g.
```
>>> P[0, 1, 2, 3].stutter([1,3])
P[0, 1, 1, 1, 2, 3, 3, 3]
```
Use strict=True to force generator patterns to return the
same value `n` times in a row.
"""
n = asStream(n)
lrg = max(len(self.data), len(n))
new = []
for i in range(lrg):
for j in range(modi(n,i)):
new.append(modi(self.data,i))
item = modi(self.data,i)
if strict and isinstance(item, GeneratorPattern):
item = item.copy()
new.append(item)
return self.new(new)

def arp(self, arp_pattern):
Expand Down Expand Up @@ -1434,6 +1449,12 @@ def map(self, mapping, default=0):
"""
return self.transform( lambda value: mapping.get(value, default) )

def copy(self):
'''
Returns a new Pattern Generator with same inputs
'''
return self.__class__(*self.args, **self.kwargs)

# TODO - handle callables
# funcs = {}

Expand Down

0 comments on commit d1d29db

Please sign in to comment.