a few extensions for the itertools

  • Thread starter Mathias Panzenboeck
  • Start date
T

Tim Chase

No, because the builtin sum want's a list. This can also
handle any kind of iterable, so this would work:

isum(i**2 for i in xrange(100))

sum would need firs the whole list to be generated:

sum([i**2 for i in xrange(100)]) Really?

sum(i**2 for i in xrange(20000000))
2666666466666670000000L

seems to work fine, and judging by the memory usage it
pretty obviously doesn't create an intermediate list.

Very strange. I must have made some strange error. I tried
that (more than once) and it failed. Don't know why. But now
it works here, too. What did I write, when it failed?

Could you have been trying it on a version of python <2.4?

tim@oblique:~$ python2.3
Python 2.3.5 (#2, Sep 4 2005, 22:01:42)
[GCC 3.3.5 (Debian 1:3.3.5-13)] on linux2
Type "help", "copyright", "credits" or "license" for more
information. File "<stdin>", line 1
sum(i**2 for i in xrange(100))
^
SyntaxError: invalid syntax


tim@oblique:~$ python2.4
Python 2.4.1 (#2, May 5 2005, 11:32:06)
[GCC 3.3.5 (Debian 1:3.3.5-12)] on linux2
Type "help", "copyright", "credits" or "license" for more
information.328350


-tkc
 
K

Klaas

Mathias said:
It's like the . operator in haskell:

fchain(f,g,h) is the same like lambda *args,**kwargs: f(g(h(*args,**kwargs)))

I used this once:

composedfun = lambda arg: reduce(lambda x, f: f(x), funcs, arg)

so I can see the potential need (it is not terribly obvious in short-
or long- form).

If this belongs anywhere, though, it is functools, not itertools.

-Mike
 
M

Mathias Panzenboeck

Klaas said:
I used this once:

composedfun = lambda arg: reduce(lambda x, f: f(x), funcs, arg)

Nice! Certainly more beautiful than my version.
so I can see the potential need (it is not terribly obvious in short-
or long- form).

If this belongs anywhere, though, it is functools, not itertools.

Yes, got in there by accident.
 
K

Kay Schluehr

Klaas said:
I used this once:

composedfun = lambda arg: reduce(lambda x, f: f(x), funcs, arg)

so I can see the potential need (it is not terribly obvious in short-
or long- form).

If this belongs anywhere, though, it is functools, not itertools.

-Mike

Since you are going to exploit the universality of fold [1] here. Maybe
we should also take the next step beyond itertools ( i.e.
generatortools ) into account?

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/498264

Of course I've nothing against a nice interface for the most convenient
usecases just like the proposed isum that could be expressed by
greduce.

[1] http://www.cs.nott.ac.uk/~gmh/fold.pdf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top