Skip to content
Snippets Groups Projects
Commit acd93a0f authored by W. Spencer Smith's avatar W. Spencer Smith
Browse files

Addition of comparison bw lists comp, maps and filters for L16

parent c1a1943a
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -391,6 +391,30 @@ print(contains("happy", adjectives))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Connection Between FP Topics}
\begin{itemize}
\item List comprehensions can be implemented by maps and filters
\item All list comprehensions can be implemented via for loops
\item Not all for loops can be implemented by a list comprehension
\item Details
\href{http://treyhunner.com/2015/12/python-list-comprehensions-now-in-color/}
{here}
\end{itemize}
\begin{verbatim}
N = range(10)
dbl = lambda n: n*2
odd = lambda n: n%2==1
list(map(dbl, filter(odd, N)))
# versus
[n * 2 for n in N if n % 2 == 1]
\end{verbatim}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}
\frametitle{Homework: Index Set}
......
\frametitle{Contains}
\structure<1>{Write a function \texttt{contains(x, xs)} that takes a value \texttt{x} and a
list of values \texttt{xs} and returns $\exists ( y | y \in xs : y = x)$}
\begin{verbatim}
def contains(x, xs):
return reduce(func?, seq?, False)
adjectives = ["lazy", "grouchy", "scheming"]
print(contains(4, [3, 4, 5, 6, 8]))
print(contains("happy", adjectives))
\end{verbatim}
\uncover<2->{\texttt{reduce(lambda a, b: a or b, [y==x for y in xs], False)}}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment