|
4 | 4 | "cell_type": "markdown",
|
5 | 5 | "metadata": {},
|
6 | 6 | "source": [
|
| 7 | + "[<img src=\"http://cloud.blobcity.net/assets/images/badge.png\" height=\"20\" style=\"margin-bottom:-15px\" />](https://cloud.blobcity.com/#/ps/shared-cloudbook/66c4fcaa-b0e4-4e0a-b275-49cdf007667a)\n", |
| 8 | + "\n", |
7 | 9 | "# Functions\n",
|
8 | 10 | "\n",
|
9 | 11 | "Functions are one of the most important parts of Python. Like any other language they allow to create re-usable components of code. But functions in Python are special. They are essentially objects in themselves and can be passed around as parameters. We will take a look at this in detail in the later parts of this module. \n",
|
|
722 | 724 | "``` python\n",
|
723 | 725 | "process3() # defaults to process3(num)\n",
|
724 | 726 | "process3(num) # same as above\n",
|
725 |
| - "```" |
| 727 | + "```\n", |
| 728 | + "\n", |
| 729 | + "## Lambda Functions\n", |
| 730 | + "\n", |
| 731 | + "These are also called anonymous functions. This is a way of writing functions in a single statement. The result of the function can be a non `None` return value. We use the keyword `lambda` to define a lambda function. \n", |
| 732 | + "\n", |
| 733 | + "In the previous seciton, we had defined a square function as\n", |
| 734 | + "\n", |
| 735 | + "``` python\n", |
| 736 | + "def square(x): \n", |
| 737 | + " return x*x\n", |
| 738 | + "```\n", |
| 739 | + "\n", |
| 740 | + "By using a lambda function, we can define this function in a single line as shown below. " |
| 741 | + ] |
| 742 | + }, |
| 743 | + { |
| 744 | + "cell_type": "code", |
| 745 | + "execution_count": 23, |
| 746 | + "metadata": {}, |
| 747 | + "outputs": [ |
| 748 | + { |
| 749 | + "name": "stdout", |
| 750 | + "output_type": "stream", |
| 751 | + "text": [ |
| 752 | + "9\n" |
| 753 | + ] |
| 754 | + } |
| 755 | + ], |
| 756 | + "source": [ |
| 757 | + "square = lambda x: x * x\n", |
| 758 | + "\n", |
| 759 | + "print(square(3))" |
| 760 | + ] |
| 761 | + }, |
| 762 | + { |
| 763 | + "cell_type": "markdown", |
| 764 | + "metadata": {}, |
| 765 | + "source": [ |
| 766 | + "Whether we define the `square` function using the `def` keyword, or `lambda` keyword, they both produce exactly the same function.\n", |
| 767 | + "\n", |
| 768 | + "So why have `lambda` functions? \n", |
| 769 | + "\n", |
| 770 | + "In simpliest forms, they make programs more elegant. It involves lesser typing for coders, but in some scenarious where you want to pass a short function as a parameter, it is much more convenient to make an inline definition of a lambda function than have a full blown function imlementation. \n", |
| 771 | + "\n", |
| 772 | + "Let's take a look at how our `process3` function would work if we were to use a lambda function. " |
| 773 | + ] |
| 774 | + }, |
| 775 | + { |
| 776 | + "cell_type": "code", |
| 777 | + "execution_count": 24, |
| 778 | + "metadata": {}, |
| 779 | + "outputs": [ |
| 780 | + { |
| 781 | + "name": "stdout", |
| 782 | + "output_type": "stream", |
| 783 | + "text": [ |
| 784 | + "9\n", |
| 785 | + "27\n" |
| 786 | + ] |
| 787 | + } |
| 788 | + ], |
| 789 | + "source": [ |
| 790 | + "def process3(func):\n", |
| 791 | + " print(func(3))\n", |
| 792 | + " \n", |
| 793 | + "process3(lambda x: x * x)\n", |
| 794 | + "process3(lambda x: x * x * x)" |
| 795 | + ] |
| 796 | + }, |
| 797 | + { |
| 798 | + "cell_type": "markdown", |
| 799 | + "metadata": {}, |
| 800 | + "source": [ |
| 801 | + "It is much shorter code is it not?\n", |
| 802 | + "\n", |
| 803 | + "Over here, we did not define a function called `square` and a function called `cube`. Rather we put the logic as a lambda function while invoking the `process3` function itself. \n", |
| 804 | + "\n", |
| 805 | + "Now, we could also write the above code in a more elegant manner as shown below. Nice uh?" |
| 806 | + ] |
| 807 | + }, |
| 808 | + { |
| 809 | + "cell_type": "code", |
| 810 | + "execution_count": 25, |
| 811 | + "metadata": {}, |
| 812 | + "outputs": [ |
| 813 | + { |
| 814 | + "name": "stdout", |
| 815 | + "output_type": "stream", |
| 816 | + "text": [ |
| 817 | + "9\n", |
| 818 | + "27\n" |
| 819 | + ] |
| 820 | + } |
| 821 | + ], |
| 822 | + "source": [ |
| 823 | + "process3 = lambda func: print(func(3))\n", |
| 824 | + "process3(lambda x: x * x)\n", |
| 825 | + "process3(lambda x: x * x * x)" |
| 826 | + ] |
| 827 | + }, |
| 828 | + { |
| 829 | + "cell_type": "markdown", |
| 830 | + "metadata": {}, |
| 831 | + "source": [ |
| 832 | + "## Currying Functions\n", |
| 833 | + "\n", |
| 834 | + "Currying functions is a concept that allows us to create additional functions from existing functions. It involves partially applying arguents to the function to create a new function. Lets take a look at an example to understand this best. " |
| 835 | + ] |
| 836 | + }, |
| 837 | + { |
| 838 | + "cell_type": "code", |
| 839 | + "execution_count": 26, |
| 840 | + "metadata": {}, |
| 841 | + "outputs": [ |
| 842 | + { |
| 843 | + "name": "stdout", |
| 844 | + "output_type": "stream", |
| 845 | + "text": [ |
| 846 | + "3\n" |
| 847 | + ] |
| 848 | + } |
| 849 | + ], |
| 850 | + "source": [ |
| 851 | + "def sum(a,b):\n", |
| 852 | + " return a + b\n", |
| 853 | + "\n", |
| 854 | + "print(sum(1,2))" |
| 855 | + ] |
| 856 | + }, |
| 857 | + { |
| 858 | + "cell_type": "markdown", |
| 859 | + "metadata": {}, |
| 860 | + "source": [ |
| 861 | + "The above function is very simple, and simply does the sumation of the two numbers passed as parameter. But what if we wanted to create a function that adds 10 to any number that is passed as a parameter to it. \n", |
| 862 | + "\n", |
| 863 | + "We can create a new function that looks like this\n", |
| 864 | + "\n", |
| 865 | + "``` python\n", |
| 866 | + "def sum(a):\n", |
| 867 | + " return a + 10\n", |
| 868 | + "```\n", |
| 869 | + "\n", |
| 870 | + "Or we can use the previously defined `sum` function, by creating a curried function. Let's see how we do this." |
| 871 | + ] |
| 872 | + }, |
| 873 | + { |
| 874 | + "cell_type": "code", |
| 875 | + "execution_count": 27, |
| 876 | + "metadata": {}, |
| 877 | + "outputs": [ |
| 878 | + { |
| 879 | + "name": "stdout", |
| 880 | + "output_type": "stream", |
| 881 | + "text": [ |
| 882 | + "15\n" |
| 883 | + ] |
| 884 | + } |
| 885 | + ], |
| 886 | + "source": [ |
| 887 | + "addTen = lambda a: sum(a, 10)\n", |
| 888 | + "\n", |
| 889 | + "print(addTen(5))" |
| 890 | + ] |
| 891 | + }, |
| 892 | + { |
| 893 | + "cell_type": "markdown", |
| 894 | + "metadata": {}, |
| 895 | + "source": [ |
| 896 | + "In a single line, we defined a new function called `addTen` that partially applies an argument to an already existent function `sum`. We have fix the value of `b = 10` when creating the curried function `addTen`.\n", |
| 897 | + "\n", |
| 898 | + "In this case the first argument `a` to the function `sum` is said to be currier, and the resulting function `addTen` is said to be a curried function." |
726 | 899 | ]
|
727 | 900 | }
|
728 | 901 | ],
|
|
0 commit comments