Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c7d7766

Browse files
committed
Improve docs:
* Simplify the pure python examples * Add a quantify() example
1 parent f5c96fb commit c7d7766

1 file changed

Lines changed: 13 additions & 16 deletions

File tree

Doc/lib/libitertools.tex

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ \subsection{Itertool functions \label{itertools-functions}}
7878
Make an iterator that returns consecutive integers starting with \var{n}.
7979
Does not currently support python long integers. Often used as an
8080
argument to \function{imap()} to generate consecutive data points.
81-
Also, used in \function{izip()} to add sequence numbers. Equivalent to:
81+
Also, used with \function{izip()} to add sequence numbers. Equivalent to:
8282

8383
\begin{verbatim}
8484
def count(n=0):
@@ -103,9 +103,7 @@ \subsection{Itertool functions \label{itertools-functions}}
103103
for element in iterable:
104104
yield element
105105
saved.append(element)
106-
if len(saved) == 0:
107-
return
108-
while True:
106+
while saved:
109107
for element in saved:
110108
yield element
111109
\end{verbatim}
@@ -124,13 +122,12 @@ \subsection{Itertool functions \label{itertools-functions}}
124122
\begin{verbatim}
125123
def dropwhile(predicate, iterable):
126124
iterable = iter(iterable)
127-
while True:
128-
x = iterable.next()
129-
if predicate(x): continue # drop when predicate is true
125+
for x in iterable:
126+
if not predicate(x):
127+
yield x
128+
break
129+
for x in iterable:
130130
yield x
131-
break
132-
while True:
133-
yield iterable.next()
134131
\end{verbatim}
135132
\end{funcdesc}
136133

@@ -209,9 +206,7 @@ \subsection{Itertool functions \label{itertools-functions}}
209206
\begin{verbatim}
210207
def islice(iterable, *args):
211208
s = slice(*args)
212-
next = s.start or 0
213-
stop = s.stop
214-
step = s.step or 1
209+
next, stop, step = s.start or 0, s.stop, s.step or 1
215210
for cnt, element in enumerate(iterable):
216211
if cnt < next:
217212
continue
@@ -278,9 +273,7 @@ \subsection{Itertool functions \label{itertools-functions}}
278273

279274
\begin{verbatim}
280275
def takewhile(predicate, iterable):
281-
iterable = iter(iterable)
282-
while True:
283-
x = iterable.next()
276+
for x in iterable:
284277
if predicate(x):
285278
yield x
286279
else:
@@ -358,6 +351,10 @@ \subsection{Examples \label{itertools-example}}
358351
... "Returns True if pred(x) is False for every element in the iterable"
359352
... return True not in imap(pred, seq)
360353
354+
>>> def quantify(pred, seq):
355+
... "Count how many times the predicate is True in the sequence"
356+
... return sum(imap(pred, seq))
357+
361358
>>> def padnone(seq):
362359
... "Returns the sequence elements and then returns None indefinitely"
363360
... return chain(seq, repeat(None))

0 commit comments

Comments
 (0)