Commit 384cc5ab authored by Mareš Martin's avatar Mareš Martin
Browse files

Lambda a jiné funkce

parent 8f033f52
\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage[czech]{babel}
\usepackage{palatino}
\usepackage{verbatim}
\usetheme{Warsaw}
\title{Programování 1: Triky s funkcemi}
\author[Martin Mareš]{Martin Mareš\\\texttt{mj@ucw.cz}}
\institute{Katedra Aplikované Matematiky\\MFF UK Praha}
\date{2019}
\begin{document}
\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{footline}{}
\setbeamerfont{title page}{family=\rmfamily}
\shorthandoff{"}
\begin{frame}
\titlepage
\end{frame}
\input ../slidemac.tex
% ----------------------------------------------------------------------
\begin{frame}{Funkce jako parametr}
\verbatiminput{po_slozkach.py}
\py{%
po\_slozkach(secti, [1, 2, 3], [10, 20, 30])
}{%
[11, 22, 33] \cmt{(secti je jméno funkce, secti() její volání)}
}
\py{%
po\_slozkach(max, [10, 2, 3], [1, 20, 30])
}{%
[10, 20, 30]
}
\py{%
import operator\\
po\_slozkach(operator.add, [1, 2, 3], [10, 20, 30])
}{%
[11, 22, 33]
}
\end{frame}
% ----------------------------------------------------------------------
\begin{frame}{Lambda-funkce (funkce bez jména)}
\py{%
plus = lambda x, y: x+y \\
plus(1, 5)
}{%
6
}
\py{%
po\_slozkach(lambda x, y: x*y, [2,4], [3,7])
}{%
[6, 28]
}
\end{frame}
% ----------------------------------------------------------------------
\begin{frame}{Lambdy ve standardní knihovně}
\py{%
k = ["kočka", "na", "okně"]\\
sorted(k, key=lambda x: len(x))
}{%
['na', 'okně', 'kočka']
}
\py{%
min(k, key=lambda x: len(x))
}{%
'na'
}
\py{%
p = [(1,'leden'), (2,'unor'), (4,'duben')]\\
sorted(p, key=lambda x: x[1])
}{%
[(4, 'duben'), (1, 'leden'), (2, 'unor')]
}
\py{%
cisla = map(int, ["12", "34"])\\
list(cisla)
}{%
[12, 34] \cmt{(nebo také:~~{\tt map(int, input().split())})}
}
\py{%
suma = map(operator.add, [1,2], [10,20])\\
list(suma)
}{%
[11, 22]
}
\end{frame}
% ----------------------------------------------------------------------
\begin{frame}{Vnořené funkce}
\verbatiminput{vnorene.py}
\py{%
f()
}{%
[1, 2, 3]
}
\end{frame}
% ----------------------------------------------------------------------
\begin{frame}{Vnořené funkce: vracíme vnitřní funkci}
\begin{columns}
\begin{column}{0.5\textwidth}
\verbatiminput{vnorene2.py}
\end{column}
\begin{column}{0.5\textwidth}
\py{%
a = f() \\
a()
}{%
1
}
\py{%
a()
}{%
2
}
\py{%
b = f() \\
b()
}{%
1
}
\py{%
a()
}{%
3
}
\py{%
b()
}{%
2
}
\end{column}
\end{columns}
\end{frame}
% ----------------------------------------------------------------------
\begin{frame}{Generátory}
{\bf Generátor s~yield (korutina):}
\medskip
\verbatiminput{generator.py}
\medskip
{\bf Generátorové výrazy:} jako list comprehension, ale () místo []
\medskip
\py{%
g = (i**2 for i in range(100)) \\
max(g)
}{%
9801 \cmt{(seznam není nikde ulozený)}
}
\py{%
max(i**2 for i in range(100))
}{%
9801 \cmt{(nejsou třeba dvojí závorky)}
}
\end{frame}
% ----------------------------------------------------------------------
\end{document}
SLIDES=10-lambda.pdf
include ../Makerules
def gen(n):
for i in range(n):
yield i**2
for x in gen(10):
print(x)
# Spočítá z[i] = f(x[i], y[i])
def po_slozkach(f, a, b):
return [ f(x, y) for x, y in zip(a, b) ]
def secti(x, y):
return x + y
def f():
n = 0
def krok():
nonlocal n
n += 1
return n
return [krok(), krok(), krok()]
def f():
n = 0
def krok():
nonlocal n
n += 1
return n
return krok
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment