[Tutor] Text processing and functional programming?
Abel Daniel
abli at freemail.hu
Fri Aug 15 00:27:44 EDT 2003
> > And further on think about closures. They allow nice constructs (people
> > who know Common Lisp or Scheme use them all the time).
> >
> > In [2]: def make_counter(start=0, step=1):
> > ....: def cfun(step=step, counter=[start]):
> > ....: counter[0] += step
> > ....: return counter[0]
> > ....: return cfun
>
> Isn't this what yield does? Are there kinds of closures that can't be
> implemented with iterators? Or are iterators another way of creating
> closures?
In the above, cfun can get extra parameters, so you can do:
>>> c=make_counter()
>>> c()
1
>>> c()
2
>>> c(2)
4
I don't think this is possible with iterators.
Abel Daniel
More information about the Tutor
mailing list