Skip to content

Commit 042828c

Browse files
committed
first step into operations on sequences
1 parent 78f7354 commit 042828c

File tree

4 files changed

+65
-16
lines changed

4 files changed

+65
-16
lines changed

bibliography/bibliography.bib

+18-12
Original file line numberDiff line numberDiff line change
@@ -1798,7 +1798,7 @@ @techreport{PEP8
17981798

17991799
@techreport{PEP202,
18001800
author = a_warsaw_barry,
1801-
title = {List Comprehensions},
1801+
title = {List\pythonIdx{list!comprehension}\pythonIdx{comprehension!list} Comprehensions},
18021802
xdata = {rep_pep},
18031803
number = {202},
18041804
date = {2000-07-13},
@@ -1808,7 +1808,7 @@ @techreport{PEP202
18081808

18091809
@techreport{PEP234,
18101810
author = a_yee_ka_ping # and # a_van_rossum_guido,
1811-
title = {Iterators},
1811+
title = {\pythonilsIdx{Iterator}},
18121812
xdata = {rep_pep},
18131813
number = {234},
18141814
date = {2001-05-18},
@@ -1818,7 +1818,7 @@ @techreport{PEP234
18181818

18191819
@techreport{PEP255,
18201820
author = a_schemenauer_neil # and # a_peters_tim # and # a_hetland_magnus_lie,
1821-
title = {Simple Generators},
1821+
title = {Simple \pythonilsIdx{Generator}},
18221822
xdata = {rep_pep},
18231823
number = {255},
18241824
date = {2001-05-18},
@@ -1838,7 +1838,7 @@ @techreport{PEP257
18381838

18391839
@techreport{PEP274,
18401840
author = a_warsaw_barry,
1841-
title = {Dict Comprehensions},
1841+
title = {Dict Comprehensions\pythonIdx{dict!comprehension}\pythonIdx{comprehension!dict}},
18421842
xdata = {rep_pep},
18431843
number = {274},
18441844
date = {2001-10-25},
@@ -1848,7 +1848,7 @@ @techreport{PEP274
18481848

18491849
@techreport{PEP289,
18501850
author = a_hettinger_raymond,
1851-
title = {Generator Expressions},
1851+
title = {\pythonilIdx{Generator} Expressions},
18521852
xdata = {rep_pep},
18531853
number = {274},
18541854
date = {2002-01-30},
@@ -1858,17 +1858,16 @@ @techreport{PEP289
18581858

18591859
@techreport{PEP482,
18601860
author = a_langa_lukasz,
1861-
title = {Literature Overview for Type Hints},
1861+
title = {Literature Overview for \glslink{typeHint}{Type Hints}},
18621862
number = {482},
18631863
xdata = {rep_pep},
18641864
date = {2015-01-08},
18651865
url = {https://peps.python.org/pep-0482},
18661866
urldate = {2024-10-09},
18671867
}
18681868

1869-
18701869
@techreport{PEP484,
1871-
title = {Type Hints},
1870+
title = {\glslink{typeHint}{Type Hints}},
18721871
author = a_van_rossum_guido # and # a_langa_lukasz,
18731872
number = {484},
18741873
xdata = {rep_pep},
@@ -2106,6 +2105,13 @@ @inbook{PSF2024DM
21062105
urldate = {2024-08-22},
21072106
}
21082107

2108+
@inbook{PSF2024DTIPE,
2109+
title = {\pgls{doctest} -- Test Interactive \python\ Examples},
2110+
url = {https://docs.python.org/3/library/doctest.html},
2111+
xdata = {PSF2024TPSL},
2112+
urldate = {2024-11-07},
2113+
}
2114+
21092115
@inbook{PSF2024FSL,
21102116
title = {Formatted String Literals},
21112117
chapter = {7.1.1},
@@ -2114,11 +2120,11 @@ @inbook{PSF2024FSL
21142120
urldate = {2024-07-25},
21152121
}
21162122

2117-
@inbook{PSF2024DTIPE,
2118-
title = {\pgls{doctest} -- Test Interactive \python\ Examples},
2119-
url = {https://docs.python.org/3/library/doctest.html},
2123+
@inbook{PSF2024IFCIFEL,
2124+
title = {\pythonilIdx{itertools} -- Functions Creating \pythonilsIdx{Iterator} for Efficient Looping},
21202125
xdata = {PSF2024TPSL},
2121-
urldate = {2024-11-07},
2126+
url = {https://docs.python.org/3/library/itertools.html},
2127+
urldate = {2024-11-09},
21222128
}
21232129

21242130
@inbook{PSF2024IPM,

styles/listing.sty

+8-2
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ breaklines=true%
178178
\gitLoad{#1}{#2}{#3}%
179179
\expandafter\expandafter\expandafter\edef\expandafter\csname @pwp@gitUrl:lst:#4\endcsname{\gitUrl}%
180180
\expandafter\expandafter\expandafter\edef\expandafter\csname @pwp@gitFile:lst:#4\endcsname{\gitFile}%
181-
\lstinputlisting[float,label={lst:#4}#6,caption={#5~(\href{\csname @pwp@gitUrl:lst:#4\endcsname}{src})}]{\csname @pwp@gitFile:lst:#4\endcsname}%
181+
\begin{figure}[tb]%
182+
\lstinputlisting[label={lst:#4}#6,caption={#5~(\href{\csname @pwp@gitUrl:lst:#4\endcsname}{src})}]{\csname @pwp@gitFile:lst:#4\endcsname}%
183+
\end{figure}%
182184
}%
183185
%
184186
%%
@@ -222,7 +224,11 @@ breaklines=true%
222224
%% #4 the label (exec: will be pre-pended)
223225
%% #5 the caption
224226
\protected\gdef\gitOutputTool#1#2#3#4#5{%
225-
\gitOutputWithStyle{#1}{#2}{#3}{#4}{,label={exec:#4},caption={#5},float}{tool_style}%
227+
\gitExec{#1}{#2}{#3}%
228+
\expandafter\expandafter\expandafter\edef\expandafter\csname @pwp@gitFile:exec:#4\endcsname{\gitFile}%
229+
\begin{figure}[tb]%
230+
\lstinputlisting[style=tool_style,label={exec:#4},caption={#5}]{\csname @pwp@gitFile:exec:#4\endcsname}%
231+
\end{figure}%
226232
}%
227233
%
228234
%% Use latexgit to place a listing with code and the program output.

text/main/controlFlow/functions/functions.tex

+1
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@
611611
\endhsection%
612612
%
613613
\hsection{Functions as Parameters or Variables, \texttt{Callable}, and \texttt{lambda}s}%
614+
\label{sec:functionsAsVarsAndLambdas}%
614615
%
615616
We have just learned that we can basically construct a function call by placing the parameter values into collection objects and then invoke the function by \inQuotes{unpacking} the collection using either \pythonil{*} (for position-based parameters) or \pythonil{**} (for dictionaries).
616617
But there is one more interesting thing that we can do with functions.

text/main/controlFlow/iteration/iteration.tex

+38-2
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,6 @@
593593
\gitPythonAndOutput{\programmingWithPythonCodeRepo}{07_iteration}{generator_expressions_to_collection.py}{--args format}{iteration:generator_expressions_to_collection}{%
594594
Using generator expressions when creating collection datastructures\pythonIdx{Generator}\pythonIdx{list}\pythonIdx{dict}.}%
595595
%
596-
\FloatBarrier%
597-
%
598596
\begin{sloppypar}%
599597
Finally, generator expressions can also be passed to the constructors of collection datastructures or other functions that create such datastructures.
600598
In \cref{lst:iteration:generator_expressions_to_collection}, we first define a string \pythonil{csv_text} with the value~\pythonil{"22,56,33,67,43,33,12"}.
@@ -701,5 +699,43 @@
701699
\FloatBarrier%
702700
\endhsection%
703701
%
702+
\hsection{Operations on Iterators}%
703+
%
704+
\gitPythonAndOutput{\programmingWithPythonCodeRepo}{07_iteration}{filter_takewhile.py}{--args format}{iteration:filter_takewhile}{%
705+
An example for \pythonilIdx{takewhile}\pythonIdx{itertools!takewhile} and \pythonilIdx{filter}.}%
706+
%
707+
Sequences play a very important role in \python\ programming.
708+
The fact that generator functions and the \pythonilIdx{yield} keyword were added to the language just to provide a natural way to construct complex sequences speaks for itself~\cite{PEP255}.
709+
Naturally, there also are several utilities for working with and transforming sequences.
710+
Some of these tools are directly provided as built-in functions, some ship with the module~\pythonilIdx{itertools}~\cite{PSF2024IFCIFEL}.
711+
Here we want to discuss a few of them.
712+
713+
The first two tools we will look at are the built-in function \pythonilIdx{filter} and the \pythonilIdx{takewhile}\pythonIdx{itertools!takewhile} function from the \pythonilIdx{itertools} module.
714+
In the previous section, we implemented a generator function returning the endless sequence of prime numbers in \cref{lst:loops:for_loop_sequence_primes}.
715+
What would we do if we wanted a convenient way to create a list of all prime numbers which are less than 50 by using this never-ending generator?
716+
The answer can be found in \cref{lst:iteration:filter_takewhile}.
717+
718+
\pythonilIdx{takewhile} is a function with two parameters:
719+
The second parameter is an \pythonilIdx{Iterator}.
720+
Let's say that it provides a sequence of elements of some type~\pythonil{T}.
721+
The first parameter then is a predicate, which is a function accepting one element of type~\pythonil{T} and returning a \pythonil{bool} value.
722+
Back in \cref{sec:functionsAsVarsAndLambdas}, we learned that we can also pass functions or \pythonilsIdx{lambda} as arguments to other functions.
723+
This is a practical example of that.
724+
Basically, \pythonilIdx{takewhile} constructs a new \pythonilIdx{Iterator} which returns the elements from the original \pythonilIdx{Iterator} as long as the predicate function returns~\pythonil{True} for them.
725+
As soon as the predicate returns \pythonil{False}, it will stop the iteration.
726+
Therefore, the answer to \inQuotes{How can I extract all the numbers less than~50 from the prime sequence?} is simply to call \pythonil{akewhile(lambda z: z < 50, primes())}.
727+
This sequence is now no longer infinitely long and can conveniently be converted to a \pythonil{list}.
728+
729+
The built-in function~\pythonilIdx{filter} works quite similarly.
730+
It, too, accepts a predicate and an~\pythonilIdx{Iterator} as input.
731+
Different from \pythonil{takewhile}, the new \pythonilIdx{Iterator} created by \pythonilIdx{filter} does not stop if the predicate returns~\pythonil{False}.
732+
However, it only returns only those elements for which the predicate returned~\pythonil{True}.
733+
In \cref{lst:iteration:filter_takewhile}, we use this to select prime numbers~$x$ for which an integer~$y$ exists such that~$x=y^2+1$.
734+
This time, we implement the predicate as function \pythonil{sqr_plus_1} and pass this function to \pythonilIdx{filter}.
735+
Since there again probably infinitely many such prime numbers, we return only those that are less than~1000, for which we use~\pythonil{takewhile}.
736+
The results can be seen in \cref{exec:iteration:filter_takewhile}.
737+
\FloatBarrier%
738+
\endhsection%
739+
%
704740
\endhsection%
705741
%

0 commit comments

Comments
 (0)