{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## lambda(λ) function in Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Basics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* `Python` supports to make anonymous functions which are not bound to a name(@ runtime).\n", "* It uses a construct called `\"lambda\"`.\n", "* **This is not exactly the same as lambda in functional programming languages.**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# func(x) returns the power of given parameter\n", "def func(x):\n", " return x**2\n", "print(func(5))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# this will do the same thing with out using functions\n", "lam = lambda x:x**2\n", "print(lam(5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* lambda function does not have `return` statement.\n", "* As well as we can use \"lambda\" function inside of the regular function." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# implementing lambda function inside of the function\n", "def increment(num):\n", " return lambda x:x+num\n", "\n", "print(increment(20)(25)) # 20+25=45\n", "\n", "a = increment(15)\n", "b = increment(25)\n", "print(a(5), b(5)) # 15+5=20 , 25+5=30\n", "\n", "print(a(a(10))) # 15 + 15 + 10 = 40" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* That increment function generates an anonymous function(lambda) at the runtime and returns that lambda function.\n", "\n", "* beauty of the lambda function is that, we can use function pipeline {Ex: a(b(c(512))) } to get a answer.\n", "___\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Go into deep\n", "\n", "* Let's use lambda functions with a python list.\n", "* For this we can use several library functions such as `filter()` , `map()` , `reduce()`.\n", "\n", "filter() : creates a list of elements for which a function returns true.\n", "\n", "map() : applies a function to all the items in an input list.\n", "\n", "reduce() : performing some computation on a list and returning the result.\n", "* You have to import reduce function _`from functools import reduce`_" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "inList = [6, 20, 14, 2, 5, 18, 10, -7]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "less_than_ten = list(filter(lambda x : x < 10, inList))\n", "print(less_than_ten)\n", "\n", "even_numbers = list(filter(lambda y : y % 2 == 0, inList))\n", "print(even_numbers)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "remainder_list = list(map(lambda x : x%4, inList))\n", "print(remainder_list)\n", "\n", "modified_list = list(map(lambda y : y + 100, inList))\n", "print(modified_list)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from functools import reduce\n", "sum_of_inList = reduce(lambda x, y : x + y, inList)\n", "print(sum_of_inList)\n", "\n", "print(sum(inList)) # by built-in function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Applications" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Sieve of Eratosthenes (Finding prime numbers)\n", "![algo](./Sieve_of_Eratosthenes_animation.gif)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113]\n" ] } ], "source": [ "numbers = list(range(2,120))\n", "for i in range(2, 8):\n", " numbers = list(filter(lambda x:x==i or x%i, numbers))\n", "print(numbers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Use in NLP" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']\n", "[3, 5, 5, 3, 5, 4, 3, 4, 3]\n" ] } ], "source": [ "sentence = \"The quick brown fox jumps over the lazy dog\"\n", "words = sentence.split()\n", "print(words)\n", "lengths = list(map(lambda x:len(x), words))\n", "print(lengths)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### References\n", "\n", "+ http://www.secnetix.de/olli/Python/lambda_functions.hawk\n", "+ http://book.pythontips.com/en/latest/map_filter.html\n", "+ https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.5.2" } }, "nbformat": 4, "nbformat_minor": 2 }