[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]


 > Lazy evaluation can win in space even when all the values are 
used, if they
 > aren't needed at once, though.  For example, Haskell's
 >  foldl1 (+) [1..2^40]

In comparing 'foldl' to 'reduce', you missed the fairly easy 
solution of using the Python 'xrange' built-in function to generate 
a lazy sequence of integers; i.e.:

	reduce(operator.add, xrange(2L**40))

There's a similar 'xreadlines' function which lazily iterates over 
the contents of a file.

Somewhat unintuitively, though, these functions do not return 
generators; rather, they return 'xrange' or 'xreadlines' objects, 
which are implemented seperately as C extension types.

Also, while you *can* do this:

	>>> r = xrange(2L**40)
	>>> r[6]

The following results from trying to do the same with a generator:

	>>> s = gen_range(2L**40)
	>>> s[6]
	Traceback (most recent call last):
	  File "<stdin>", line 1, in ?
	TypeError: unsubscriptable object

This lack of caching of previously generated values is something 
that's not readily apparent to a new user of iterators, and keeps 
them from being perfect replacements for lazy sequences.

With generators/iterators and the type/class unification in Python 
2.2, there is an ever-shrinking list of language features I wish 
Python had:

	1. 'lambda' accepting a full suite of expressions, rather than 
a single statement
	2. a 'case' or 'switch' statement, with arbitrary boolean conditions
	3. the addition of the '*arg' and '**kw' function prototype 
syntax to general cases of tuple unpacking and pattern matching 
(especially if paired with a 'case' as described below)

I think I can live without macros, given the above 
provisions...actually, I've been living without macros for most of 
the last year, and generally done alright.
P.S.: Before anyone says anything, I know that if Python had 
macros, I could implement any or all of the above myself. I'll come 
right out and say it: I would be the first to jump to a Python 
built on top of Scheme that still supported most of the standard 
libraries, *and* the indentation syntax (at least for day-to-day 
coding, with the S-exp representation available for low-level 

Lennon Day-Reynolds