Skip to content

Commit

Permalink
Add Generate CACHE HEAD property and improve PChain behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
Qirky committed Jun 27, 2020
1 parent c947149 commit a3e57bd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
17 changes: 14 additions & 3 deletions FoxDot/lib/Patterns/Generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,29 @@ def __init__(self, mapping, **kwargs):
self.mapping = {}
i = 0
for key, value in mapping.items():
self.mapping[key] = asStream(value)
self.mapping[key] = self._convert_to_list(value)
# Use the first key to start with
if i == 0:
self.last_value = key
i += 1

self.init_random(**kwargs)

def func(self, index):
self.last_value = self.choice(self.mapping[self.last_value])
def func(self, *args, **kwargs):
index = self.last_value
if isinstance(self.last_value, GeneratorPattern):
index = index.CACHE_HEAD
if index in self.mapping:
self.last_value = self.choice(self.mapping[index])
return self.last_value

def _convert_to_list(self, value):
if isinstance(value, list):
return value
elif isinstance(value, Pattern):
return value.data
return [value]

class PZ12(GeneratorPattern):
""" Implementation of the PZ12 algorithm for predetermined random numbers. Using
an irrational value for p, however, results in a non-determined order of values.
Expand Down
5 changes: 5 additions & 0 deletions FoxDot/lib/Patterns/Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,11 @@ def getitem(self, index=None, *args):
self.cache[index] = value
return value

@property
def CACHE_HEAD(self):
''' Returns the last value used if it exists '''
return self.cache.get(self.index - 1)

def new(self, other, func=Nil):
""" Creates a new `GeneratorPattern` that references
this pattern but returns a modified value based on
Expand Down

0 comments on commit a3e57bd

Please sign in to comment.