Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Simplifications to new_stream.py
Browse files Browse the repository at this point in the history
  • Loading branch information
mwhansen committed Jan 10, 2014
1 parent 00bd664 commit b7afcd7
Showing 1 changed file with 5 additions and 53 deletions.
58 changes: 5 additions & 53 deletions src/sage/combinat/species/new_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ def getitem_decorator(func):
from sage.misc.all import sage_wraps
@sage_wraps(func)
def wrapper(self, n):
# Handle constant
if self._constant is not False:
# Handle (eventually) constant streams
if self._constant is not False or self.is_constant():
if self._constant is False:
self.set_constant(self.get_constant_position(),
self.get_constant())
pos, value = self._constant
if n >= pos:
return value
Expand Down Expand Up @@ -65,58 +68,9 @@ def __iter__(self):
i += 1
raise StopIteration

def map(self, func):
"""
EXAMPLES::
sage: from sage.combinat.species.new_stream import Stream
sage: s = Stream(compute=lambda x: x)
sage: ss = s.map(lambda x: x*x)
sage: [ss[i] for i in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
"""
return MappedStream(func, self)

class ConstantStream(Stream):
def __init__(self, constant):
super(ConstantStream, self).__init__()
self.set_constant(0, constant)

class DictCachedStream(Stream):
def __init__(self, **kwds):
self._cache = {}
super(DictCachedStream, self).__init__(**kwds)

def __setitem__(self, n, value):
self._cache[n] = value

@Stream.getitem_decorator
def __getitem__(self, n):
value = self._cache.get(n, None)
if value is None:
value = self.compute(n)
self._cache[n] = value
return value
else:
return value

class MappedStream(DictCachedStream):
def __init__(self, func, stream):
self._func = func
self._stream = stream
super(MappedStream, self).__init__()

def compute(self, n):
value = self._func(self._stream[n])
if self._constant is False and self._stream.is_constant():
self._constant = (n, value)
return value


class ListCachedStream(Stream):
def __init__(self, **kwds):
self._cache = []
self._cached = kwds.pop('cached', True)
super(ListCachedStream, self).__init__(**kwds)

def __setitem__(self, n, value):
Expand Down Expand Up @@ -152,8 +106,6 @@ def __len__(self):

@Stream.getitem_decorator
def __getitem__(self, n):
if not self._cached:
return self.compute(n)
pos = len(self._cache)
while pos <= n:
value = self.compute(pos)
Expand Down

0 comments on commit b7afcd7

Please sign in to comment.