|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "3c102256", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# 7. Advanced Python: Functional Programming\n" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "markdown", |
| 13 | + "id": "ea7548fe", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + " - Functional programming is separation of concerns\n", |
| 17 | + " - packaging our code in chunks so that everything is well organized based on functionality\n", |
| 18 | + " - one thing its good at\n", |
| 19 | + " - separate data and behaviour/functionality, unlike OOP\n", |
| 20 | + " -Makes our code:\n", |
| 21 | + " \n", |
| 22 | + " 1. clean + understandable\n", |
| 23 | + " 2. easy to extend -> grow\n", |
| 24 | + " 3. easy to maintain -> not storing\n", |
| 25 | + " 4. memory efficient\n", |
| 26 | + " 5. dry -> non-redundant" |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "markdown", |
| 31 | + "id": "ecb6220b", |
| 32 | + "metadata": {}, |
| 33 | + "source": [ |
| 34 | + "## 7.a Pure Functions\n" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "markdown", |
| 39 | + "id": "63bc0c69", |
| 40 | + "metadata": {}, |
| 41 | + "source": [ |
| 42 | + "They are used to :\n", |
| 43 | + " - one input, always only one and same output\n", |
| 44 | + " - should not produce side effects. It shouldn't create changes in outside world of the function. i.e. printing. it displays something on the screen, which is the out side world\n", |
| 45 | + " - They help because it creates less-bug code. It is better to understand and modify without many errors.\n", |
| 46 | + " - Things not touching each other and being discrete" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "markdown", |
| 51 | + "id": "f5278152", |
| 52 | + "metadata": {}, |
| 53 | + "source": [ |
| 54 | + "### Map Fucntion\n", |
| 55 | + "\n", |
| 56 | + "- map(fun, iterable) -> returns the obj" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "code", |
| 61 | + "execution_count": 3, |
| 62 | + "id": "9459d2a5", |
| 63 | + "metadata": {}, |
| 64 | + "outputs": [ |
| 65 | + { |
| 66 | + "name": "stdout", |
| 67 | + "output_type": "stream", |
| 68 | + "text": [ |
| 69 | + "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]\n" |
| 70 | + ] |
| 71 | + } |
| 72 | + ], |
| 73 | + "source": [ |
| 74 | + "def mul2(num):\n", |
| 75 | + " return num * 2\n", |
| 76 | + "print(list(map(mul2,list(range(10)))))" |
| 77 | + ] |
| 78 | + } |
| 79 | + ], |
| 80 | + "metadata": { |
| 81 | + "kernelspec": { |
| 82 | + "display_name": "Python 3", |
| 83 | + "language": "python", |
| 84 | + "name": "python3" |
| 85 | + }, |
| 86 | + "language_info": { |
| 87 | + "codemirror_mode": { |
| 88 | + "name": "ipython", |
| 89 | + "version": 3 |
| 90 | + }, |
| 91 | + "file_extension": ".py", |
| 92 | + "mimetype": "text/x-python", |
| 93 | + "name": "python", |
| 94 | + "nbconvert_exporter": "python", |
| 95 | + "pygments_lexer": "ipython3", |
| 96 | + "version": "3.8.8" |
| 97 | + } |
| 98 | + }, |
| 99 | + "nbformat": 4, |
| 100 | + "nbformat_minor": 5 |
| 101 | +} |
0 commit comments