From f972303c983c8bdc1d98396f53fae60cded76bd1 Mon Sep 17 00:00:00 2001 From: Aleksey Zubakov Date: Tue, 27 Sep 2022 02:21:28 +0300 Subject: [PATCH] Code from second seminar. Here are examples of *functions* and *higher-order functions*. All signatures of functions are annotated with types and could be typechecked with mypy or pyright. --- 02/01.py | 3 +++ 02/02.py | 5 +++++ 02/03.py | 37 +++++++++++++++++++++++++++++++++++++ 02/04.py | 19 +++++++++++++++++++ 02/README.md | 31 +++++++++++++++++++++++++++++++ 02/main_01.py | 19 +++++++++++++++++++ 6 files changed, 114 insertions(+) create mode 100644 02/01.py create mode 100644 02/02.py create mode 100644 02/03.py create mode 100644 02/04.py create mode 100644 02/README.md create mode 100644 02/main_01.py diff --git a/02/01.py b/02/01.py new file mode 100644 index 0000000..67bddb6 --- /dev/null +++ b/02/01.py @@ -0,0 +1,3 @@ +from main_01 import is_prime + +print(is_prime(17)) diff --git a/02/02.py b/02/02.py new file mode 100644 index 0000000..63f52ae --- /dev/null +++ b/02/02.py @@ -0,0 +1,5 @@ +def square(x: int) -> int: + pass + + +square("asdasd") diff --git a/02/03.py b/02/03.py new file mode 100644 index 0000000..29aaba0 --- /dev/null +++ b/02/03.py @@ -0,0 +1,37 @@ +import doctest + +from typing import Callable + + +# term :: int -> int +def cube(x: int) -> int: + """ + Evaluates cube of given number. + >>> cube(3) + 27 + >>> cube(5) + 125 + """ + return x * x * x + + +# term :: int -> int +def summation( + n: int, + term: Callable[[int], int], +) -> int: + """ + Sums first n numbers of series. + >>> summation(5, cube) + 225 + >>> summation(5, lambda k: 8 // ((4 * k - 3) * (4 * k - 1))) + 2 + """ + total = 0 + for k in range(1, n + 1): + total += term(k) + return total + + +if __name__ == "__main__": + doctest.testmod() diff --git a/02/04.py b/02/04.py new file mode 100644 index 0000000..5652d63 --- /dev/null +++ b/02/04.py @@ -0,0 +1,19 @@ +from typing import Callable, Tuple + +type_F = Callable[[int], int] + + +def factory(total: int) -> Tuple[type_F, type_F]: + def adder(k): + return k + total + + def subtractor(k): + return total - k + + return adder, subtractor + + +if __name__ == "__main__": + add, min = factory(5) + print(add(1)) + print(min(3)) diff --git a/02/README.md b/02/README.md new file mode 100644 index 0000000..4a59265 --- /dev/null +++ b/02/README.md @@ -0,0 +1,31 @@ +### Что здесь лежит? +- `01.py` и `main_01.py` демонстрируют как разбивать +код на отдельный файлы; +- `02.py` -- пример простой типизированной функции; +- `03.py`-- обобщение частичной суммы ряда с помощью функций; +- `04.py` -- примеры функций-замыканий; + +### Как запускать? + +Желательно установить [mypy](https://github.com/python/mypy) или даже +[pyright](https://github.com/microsoft/pyright) и проверять типизацию +ими перед запусками: +```sh +$ mypy *.py +``` +или +```sh +$ mypy 01.py +``` + +`01.py` и `02.py` можно запустить, как и в прошлый раз: +```sh +$ python 01.py +``` + +Если файл содержит doctest _(прим.: пример в комментарии к функции; да, он +рабочий)_, добавьте флаг `-v` в конце: + +```sh +$ python 03.py -v +``` diff --git a/02/main_01.py b/02/main_01.py new file mode 100644 index 0000000..7e97720 --- /dev/null +++ b/02/main_01.py @@ -0,0 +1,19 @@ +def is_prime(n: int) -> bool: + """ + >>> is_prime(10) + True + >>> is_prime(17) + True + >>> is_prime(21) + False + """ + for el in range(2, n): + if n % el == 0: + return False + return True + + +if __name__ == "__main__": + import doctest + + doctest.testmod()