[Edu-sig] poking around in Py3k (recycling old algebra)
kirby urner
kirby.urner at gmail.com
Thu May 28 19:03:39 CEST 2009
Where I could see Ellipsis being used is in an OEIS-like context
(Sloan's AT&T thing) and going like [1, 12, 42, 92, ...] which you
then feed to a factory function, say get_seq.
get_seq has lookup powers (ala OEIS) and loads the right looping
construct, then gives you an itertools like thingy that spits out the
supplied values without recomputing them, then kicks in with the
generated values once the ... is encountered).
e.g.
>>> from oeis_lib import get_seq
>>> cuboctahedrals = get_seq([1, 12, 42, 92, ...]
>>> next(cuboctahedrals)
1
>>>
...
For example, an open-ended iterable for cuboctahedral numbers may be written as:
>>> class CCP:
def __init__(self):
self.value = -1
def __next__(self):
self.value += 1
if self.value == 0:
return 1
else:
return 10 * pow(self.value, 2) + 2
def __iter__(self):
return self
>>> seq = CCP()
>>> next(seq)
1
>>> next(seq)
12
>>> next(seq)
42
>>> next(seq)
92
>>> next(seq)
162
If you wanted an iterable that kicked in only after 92, you could go:
>>> from itertools import dropwhile
>>> evaldotdot = dropwhile(lambda x: x <= 92, CCP())
>>> next(evaldotdot)
162
>>> next(evaldotdot)
252
>>> next(evaldotdot)
362
>>> next(evaldotdot)
492
>>>
See:
http://www.research.att.com/~njas/sequences/A005901
(hey, links to my web site! -- oh wait, I knew that)
Kirby
On Thu, May 28, 2009 at 9:21 AM, Scott David
Daniels<Scott.Daniels at acm.org> wrote:
> Scott David Daniels wrote:
>>
>> kirby urner wrote:
>>>
>>> ... Hey, did you know Ellipsis is a new primitive object ...
>>
>> Actually, it has been around for quite a while.... [broken example]
>
[fixed example]
More information about the Edu-sig
mailing list