...
 
Commits (2)
\begin{module}[id=python-ho-functions]
\importmhmodule[path=python/en/lambda]{python-lambda}
\importmhmodule[path=python/en/lists]{python-lists}
\begin{frame}[fragile]
\frametitle{Higher-Order Functions in $\pythonLanguage$}
\begin{itemize}
\item
\begin{definition}[id=def.ho-function]
We call a \trefi[python-functions]{function} a \defii{higher-order}{function}, iff
it takes a \trefi[python-functions]{function} as \trefi[python-functions]{argument}.
\end{definition}
\item
\begin{definition}[id=def.map-filter]
\lstinline|map| and \lstinline|filter| are built-in \trefiis{higher-order}{function}
in $\pythonLanguage$. They take a \trefi[python-functions]{function} and a
\trefi[python-lists]{list} as arguments.
\begin{itemize}
\item \lstinline[mathescape]|map($f$,$L$)| returns the list of $f$-values of the
membes of $L$.
\item \lstinline[mathescape]|filter($p$,$L$)| returns the sub-list $L'$ of those $l$
in $L$, such that \lstinline[mathescape]|$p$($l$)=True|.
\end{itemize}
\end{definition}
\item
\begin{example} Mapping over and filtering a list
\lstinputmhlisting{python/ex/map-filter.py}
\end{example}
\end{itemize}
\end{frame}
\end{module}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
% LocalWords: importmhmodule observation,id namedfunction defii defiis qquad linerange
% LocalWords: lstinputmhlisting trefii
\begin{module}[id=python-lambda]
\importmhmodule[path=python/en/functions]{python-functions}
\begin{frame}[fragile]
\frametitle{Anonymous and Higher-Order Functions (\lstinline|lambda|)}
\begin{itemize}
\item
\begin{assertion}[type=observation,id=namedfunction]
A $\pythonLanguage$ function definition combines making a function object with
giving it a name.
\end{assertion}
\item
\begin{definition}
$\pythonLanguage$ also allows to make \defii{anonymous}{functions} via the
\lstinline|lambda| constructor for \defiis{function}{object}:
\begin{lstlisting}[mathescape]
lambda ($p_1$,$\ldots$,$p_n$): $\pmetavar{expr}$
\end{lstlisting}
\end{definition}
\item
\begin{example}
The following two $\pythonLanguage$ fragments are equivalent:
\begin{center}
\begin{tabular}{p{2cm}@{\qquad}p{4cm}}
\lstinputmhlisting[linerange=1-2]{python/ex/lambda.py} &
\lstinputmhlisting[linerange=3]{python/ex/lambda.py}
\end{tabular}
\end{center}
The right one is just a \trefii[python-variables]{variable}{assignment} that assigns
a \trefii{function}{object} to the \trefi[python-variables]{variable}
\lstinline|cube|.\lec{In fact $\pythonLanguage$ uses the right one internally}
\end{example}
\item
\begin{omtext}[title=Question]
Why use \trefiis{anonymous}{function}?
\end{omtext}
\item
\begin{omtext}[title=Answer]
We may not want to invent (i.e. waste) a name if the function is only used
once\lec{examples on the next slide}
\end{omtext}
\end{itemize}
\end{frame}
\end{module}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
% LocalWords: importmhmodule observation,id namedfunction defii defiis qquad linerange
% LocalWords: lstinputmhlisting trefii
......@@ -45,7 +45,7 @@
\end{example}
\newpage
\item
\begin{example}[title=Functions vs. Methods]\strut
\begin{example}[title=Functions vs. Methods]
\begin{center}\vspace*{-1em}
\begin{tabular}{p{5.4cm}@{\qquad}p{5.2cm}}
\begin{lstlisting}
......
def cube (x):
x*x*x
cube = lambda (x): x*x*x
>>> li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61]
>>> map(lambda x: x*2 , li)
[5, 7, 97, 77, 23, 73, 61]
>>> filter(lambda x: (x%2 != 0) , li)
[10, 14, 44, 194, 108, 124, 154, 46, 146, 122]
......@@ -20,6 +20,8 @@
\mhinputref{python/en/functions-intro}
\mhinputref{python/en/functions}
\mhinputref{python/en/funcargs}
\mhinputref{python/en/lambda}
\mhinputref{python/en/ho-functions}
\nmhinputref{python/snip/fileio-intro}
\mhinputref{python/en/input-print}
\nmhinputref{python/snip/fileio-intro}
......