{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "ef7a9dcb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'\\nPrinciples of Functional Programming ->\\n\\n- Data should be separated from mutations\\n- Treat variables as immutable\\n- Treat functions as FCCs or objects\\n'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'''\n", "Principles of Functional Programming ->\n", "\n", "- Data should be separated from mutations\n", "- Treat variables as immutable\n", "- Treat functions as FCCs or objects\n", "'''" ] }, { "cell_type": "code", "execution_count": 4, "id": "80deb873", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "16\n" ] } ], "source": [ "x = 5 # initial data\n", "\n", "# Hidden Mutations\n", "x = 3 * x\n", "x += 1\n", "# Hidden Mutations\n", "\n", "print(x)\n", "\n", "# Can't rollback to the original data" ] }, { "cell_type": "code", "execution_count": 6, "id": "e018c2f2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "16\n" ] } ], "source": [ "x = 5\n", "\n", "x1 = 3 * x\n", "x2 = x1 + 1\n", "\n", "print(x2)\n", "\n", "# Too many repetitive copies of the data" ] }, { "cell_type": "code", "execution_count": 11, "id": "cd2cf4c0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "16\n" ] } ], "source": [ "x = 5\n", "\n", "def mutation_1(x):\n", " x = 3 * x\n", " x += 1\n", " \n", " return x\n", "\n", "def mutation_1_reverse(x):\n", " x -= 1\n", " x = x / 3\n", " \n", " return x\n", "\n", "x1 = mutation_1(x)\n", "\n", "print(x1)" ] }, { "cell_type": "code", "execution_count": 12, "id": "01d7ad48", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mutation_1_reverse(x1)" ] }, { "cell_type": "markdown", "id": "839491f7", "metadata": {}, "source": [ "# MAPS" ] }, { "cell_type": "code", "execution_count": 13, "id": "1257fd5d", "metadata": {}, "outputs": [], "source": [ "a = [1,2,3,4,5,6,7,8,9,10]" ] }, { "cell_type": "code", "execution_count": 16, "id": "fae0a1c4", "metadata": {}, "outputs": [], "source": [ "m = list(map(lambda x : x ** 2, a))" ] }, { "cell_type": "code", "execution_count": 17, "id": "2411df2f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m" ] }, { "cell_type": "code", "execution_count": 20, "id": "9090adaa", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "27.2 ms ± 416 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n" ] } ], "source": [ "%timeit [ i ** 2 for i in range(1, 100001) ]" ] }, { "cell_type": "code", "execution_count": 21, "id": "000e7438", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.68 ms ± 4.03 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)\n" ] } ], "source": [ "%timeit map(lambda x : x ** 2, list(range(1, 100001)))" ] }, { "cell_type": "code", "execution_count": 23, "id": "3b0505d8", "metadata": {}, "outputs": [], "source": [ "random = list(map(lambda x, y: x + y, [1,2,3], [4,5,6]))" ] }, { "cell_type": "code", "execution_count": 24, "id": "310e128e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[5, 7, 9]" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random" ] }, { "cell_type": "code", "execution_count": 29, "id": "4690255c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 6]" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1,2,3,4]\n", "b = [1,2,3,4,5,6]\n", "c = [1,2]\n", "\n", "list(map(lambda x, y, z: x + y + z, a, b, c))\n", "\n", "# Map stops at the length of the smallest iterable available" ] }, { "cell_type": "code", "execution_count": 32, "id": "cf41ec9a", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for +: 'int' and 'str'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[32], line 5\u001b[0m\n\u001b[1;32m 2\u001b[0m b \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m2\u001b[39m,\u001b[38;5;241m3\u001b[39m,\u001b[38;5;241m4\u001b[39m,\u001b[38;5;241m5\u001b[39m,\u001b[38;5;241m6\u001b[39m]\n\u001b[1;32m 3\u001b[0m c \u001b[38;5;241m=\u001b[39m {\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124ma\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;241m1\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mb\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;241m2\u001b[39m}\n\u001b[0;32m----> 5\u001b[0m \u001b[38;5;28;43mlist\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mmap\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43;01mlambda\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mx\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43my\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mz\u001b[49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mx\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43my\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mz\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mc\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 7\u001b[0m \u001b[38;5;66;03m# Map stops at the length of the smallest iterable available\u001b[39;00m\n", "Cell \u001b[0;32mIn[32], line 5\u001b[0m, in \u001b[0;36m<lambda>\u001b[0;34m(x, y, z)\u001b[0m\n\u001b[1;32m 2\u001b[0m b \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m2\u001b[39m,\u001b[38;5;241m3\u001b[39m,\u001b[38;5;241m4\u001b[39m,\u001b[38;5;241m5\u001b[39m,\u001b[38;5;241m6\u001b[39m]\n\u001b[1;32m 3\u001b[0m c \u001b[38;5;241m=\u001b[39m {\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124ma\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;241m1\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mb\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;241m2\u001b[39m}\n\u001b[0;32m----> 5\u001b[0m \u001b[38;5;28mlist\u001b[39m(\u001b[38;5;28mmap\u001b[39m(\u001b[38;5;28;01mlambda\u001b[39;00m x, y, z: \u001b[43mx\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43my\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mz\u001b[49m, a, b, c))\n\u001b[1;32m 7\u001b[0m \u001b[38;5;66;03m# Map stops at the length of the smallest iterable available\u001b[39;00m\n", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" ] } ], "source": [ "a = [1,2,3,4]\n", "b = [1,2,3,4,5,6]\n", "c = {\"a\": 1, \"b\": 2}\n", "\n", "list(map(lambda x, y, z: x + y + z, a, b, c))\n", "\n", "# Map stops at the length of the smallest iterable available" ] }, { "cell_type": "markdown", "id": "1a446e2d", "metadata": {}, "source": [ "# FILTERS" ] }, { "cell_type": "code", "execution_count": 33, "id": "57e3ca52", "metadata": {}, "outputs": [], "source": [ "a = list(range(1, 21))" ] }, { "cell_type": "code", "execution_count": 34, "id": "3995fb99", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 35, "id": "3b10fbf0", "metadata": {}, "outputs": [], "source": [ "f = list(filter(lambda x : x % 2 == 0, a))" ] }, { "cell_type": "code", "execution_count": 36, "id": "2e73fa20", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f" ] }, { "cell_type": "markdown", "id": "66ca109f", "metadata": {}, "source": [ "# ZIP" ] }, { "cell_type": "code", "execution_count": 37, "id": "f751087a", "metadata": {}, "outputs": [], "source": [ "a = [1, 2, 3]\n", "b = [\"a\", \"b\", \"c\", \"d\"]\n", "c = [True, False, False, False, True, True]" ] }, { "cell_type": "code", "execution_count": 39, "id": "1af577c7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(1, True, 'a'), (2, False, 'b'), (3, False, 'c')]" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(zip(a, c, b))" ] }, { "cell_type": "code", "execution_count": 42, "id": "a98028d4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(1, 3), (2, 4)]" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = {\"a\": 1, \"b\": 2}\n", "b = {\"c\": 3, \"d\": 4}\n", "\n", "list(zip(a.values(), b.values()))" ] }, { "cell_type": "markdown", "id": "b79b93cd", "metadata": {}, "source": [ "# REDUCE" ] }, { "cell_type": "code", "execution_count": 43, "id": "3ddf8641", "metadata": {}, "outputs": [], "source": [ "from functools import reduce" ] }, { "cell_type": "code", "execution_count": 44, "id": "9213fc4f", "metadata": {}, "outputs": [], "source": [ "a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" ] }, { "cell_type": "code", "execution_count": 46, "id": "2e281b8a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "55" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reduce(lambda x, y: x + y, a)" ] }, { "cell_type": "code", "execution_count": 47, "id": "518af912", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reduce(lambda x, y: x if x > y else y, a)" ] }, { "cell_type": "code", "execution_count": 48, "id": "64b9777f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] }, { "data": { "text/plain": [ "120" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n = int(input())\n", "\n", "reduce(lambda x, y: x * y, list(range(1, n + 1)))" ] }, { "cell_type": "code", "execution_count": 49, "id": "d5b5fce7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'this is a list of strings!'" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [\"this\", \"is\", \"a\", \"list\", \"of\", \"strings!\"]\n", "\n", "reduce(lambda x, y: f\"{x} {y}\", a)" ] }, { "cell_type": "markdown", "id": "1eabde81", "metadata": {}, "source": [ "# ARGS & KWARGS" ] }, { "cell_type": "code", "execution_count": 50, "id": "c5a2a291", "metadata": {}, "outputs": [], "source": [ "def custom_sum(a, b):\n", " return a + b" ] }, { "cell_type": "code", "execution_count": 52, "id": "7bb7885f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "custom_sum(4, 5)" ] }, { "cell_type": "code", "execution_count": 53, "id": "facd8a06", "metadata": {}, "outputs": [], "source": [ "def custom_sum(a, b, *args):\n", " print(a)\n", " print(b)\n", " print(args)" ] }, { "cell_type": "code", "execution_count": 56, "id": "33e1dd6b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "5\n", "(1, 2, 3, 8, 10)\n" ] } ], "source": [ "custom_sum(4, 5, 1, 2, 3, 8, 10)" ] }, { "cell_type": "code", "execution_count": 59, "id": "9674eda5", "metadata": {}, "outputs": [], "source": [ "def custom_sum(a, b, *args):\n", " result = a + b\n", " \n", " if args:\n", " result += reduce(lambda x, y: x + y, args)\n", " \n", " return result" ] }, { "cell_type": "code", "execution_count": 62, "id": "204073d8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "28" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "custom_sum(1, 2, 3, 4, 5, 6, 7)" ] }, { "cell_type": "code", "execution_count": 63, "id": "0e828288", "metadata": {}, "outputs": [], "source": [ "a, b, c, *d = (1, 2, 3, 4, 5, 6)" ] }, { "cell_type": "code", "execution_count": 64, "id": "7060dcf3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4, 5, 6]" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 66, "id": "dc0deb38", "metadata": {}, "outputs": [], "source": [ "def custom_sum(a, b, *meri_marzi_ka_variable):\n", " result = a + b\n", " \n", " if meri_marzi_ka_variable:\n", " result += reduce(lambda x, y: x + y, meri_marzi_ka_variable)\n", " \n", " return result" ] }, { "cell_type": "code", "execution_count": 67, "id": "7cb6d154", "metadata": {}, "outputs": [], "source": [ "def create_person(name, age, gender):\n", " Person = {\n", " \"name\": name,\n", " \"age\": age,\n", " \"gender\": gender\n", " }\n", " \n", " return Person" ] }, { "cell_type": "code", "execution_count": 68, "id": "f87571a6", "metadata": {}, "outputs": [], "source": [ "p1 = create_person(\"Bipin Kalra\", 5000, \"Male\")" ] }, { "cell_type": "code", "execution_count": 69, "id": "bab19d43", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'name': 'Bipin Kalra', 'age': 5000, 'gender': 'Male'}" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1" ] }, { "cell_type": "code", "execution_count": 72, "id": "7f08e178", "metadata": {}, "outputs": [], "source": [ "def create_person(name, age, gender, **kwargs):\n", " Person = {\n", " \"name\": name,\n", " \"age\": age,\n", " \"gender\": gender\n", " }\n", " \n", " print(\"kwargs -> \", kwargs)\n", " \n", " return Person" ] }, { "cell_type": "code", "execution_count": 73, "id": "e09335af", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "kwargs -> {'color': 'Blue', 'hobby': ['Basketball', 'Painting']}\n" ] }, { "data": { "text/plain": [ "{'name': 'Bipin Kalra', 'age': 5000, 'gender': 'Male'}" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "create_person(\n", " name = \"Bipin Kalra\",\n", " age = 5000,\n", " gender = \"Male\",\n", " color = \"Blue\",\n", " hobby = [\"Basketball\", \"Painting\"]\n", ")" ] }, { "cell_type": "code", "execution_count": 74, "id": "4308dd84", "metadata": {}, "outputs": [], "source": [ "def create_person(name, age, gender, **kwargs):\n", " Person = {\n", " \"name\": name,\n", " \"age\": age,\n", " \"gender\": gender\n", " }\n", " \n", " if kwargs:\n", " Person.update(kwargs)\n", " \n", " return Person" ] }, { "cell_type": "code", "execution_count": 75, "id": "d1c9a371", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'name': 'Bipin Kalra',\n", " 'age': 5000,\n", " 'gender': 'Male',\n", " 'color': 'Blue',\n", " 'hobby': ['Basketball', 'Painting']}" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "create_person(\n", " name = \"Bipin Kalra\",\n", " age = 5000,\n", " gender = \"Male\",\n", " color = \"Blue\",\n", " hobby = [\"Basketball\", \"Painting\"]\n", ")" ] }, { "cell_type": "code", "execution_count": 77, "id": "b626c5bd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'\\nPositional -> args -> Keyworded -> kwargs\\n'" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'''\n", "Positional -> args -> Keyworded -> kwargs\n", "'''" ] }, { "cell_type": "code", "execution_count": 78, "id": "2fbe16cc", "metadata": {}, "outputs": [], "source": [ "def random(x, y, *args, **kwargs):\n", " print(x, y)\n", " print(args)\n", " print(kwargs)" ] }, { "cell_type": "code", "execution_count": 79, "id": "ddf82903", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2\n", "(3, 4, 5)\n", "{'a': 1, 'b': 2}\n" ] } ], "source": [ "random(1, 2, 3, 4, 5, a = 1, b = 2)" ] }, { "cell_type": "code", "execution_count": 80, "id": "4fdc1d37", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "positional argument follows keyword argument (3556731755.py, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m Cell \u001b[0;32mIn[80], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m random(1, 2, a = 1, b = 2, 3, 4, 5)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m positional argument follows keyword argument\n" ] } ], "source": [ "random(1, 2, a = 1, b = 2, 3, 4, 5)" ] }, { "cell_type": "code", "execution_count": 81, "id": "0dd3794a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2, (3, 4), {'a': 5, 'b': 6})" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def sample_func(x, y, *args, **kwargs):\n", " return x, y, args, kwargs\n", "\n", "\n", "sample_func(1, 2, 3, 4, a=5, b=6)" ] }, { "cell_type": "code", "execution_count": 82, "id": "57f689af", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, [2, 3, 2, 3], 8, 10]\n" ] } ], "source": [ "a=[1,[2,3],4,5]\n", "result = lambda x: list(map(lambda y:2*y, x)) if isinstance(x,list) else lambda x:3*x\n", "print(result(a))" ] }, { "cell_type": "code", "execution_count": 83, "id": "53319b3a", "metadata": {}, "outputs": [], "source": [ "random_function = lambda x: list(map(lambda y : 2 * y, x))" ] }, { "cell_type": "code", "execution_count": 84, "id": "e2364b7d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, [2, 3, 2, 3], 8, 10]" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random_function(a)" ] }, { "cell_type": "code", "execution_count": 85, "id": "03e1be9a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 2, 3]" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[2, 3] * 2" ] }, { "cell_type": "code", "execution_count": null, "id": "b8b8d3a9", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.5" } }, "nbformat": 4, "nbformat_minor": 5 }