{ "cells": [ { "cell_type": "markdown", "id": "358dfc51-7a21-4219-824b-108542c63648", "metadata": { "tags": [] }, "source": [ "Funcones anónimas (lambda) \n", "===\n", "\n", "* Duración de la lección: 4:27 min" ] }, { "cell_type": "code", "execution_count": 1, "id": "6e26d33d-7d53-4455-9f14-e4bf47ba829f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# Función creada con nombre\n", "# =============================================================================\n", "#\n", "def incr(x):\n", " return x + 1\n", "\n", "\n", "incr(1)" ] }, { "cell_type": "code", "execution_count": 2, "id": "e7701cd5-f29c-4573-8654-325ce673a4b7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "incr" ] }, { "cell_type": "code", "execution_count": 3, "id": "bc330ace-8477-49d1-bb4a-349a7c92dbec", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "copia_de_incr = incr\n", "\n", "copia_de_incr(1)" ] }, { "cell_type": "code", "execution_count": 4, "id": "1a7dbd46-a559-4978-9821-c8740d2610b5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# Función anónima o lambda\n", "# =============================================================================\n", "#\n", "incr_ = lambda x: x + 1\n", "incr_(1)" ] }, { "cell_type": "code", "execution_count": 5, "id": "6f552967-006d-4ad4-949b-ea30de478f27", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# Las funciones lambda se puede aplicar\n", "# directamente\n", "#\n", "(lambda x: x + 1)(2)" ] }, { "cell_type": "code", "execution_count": 6, "id": "0d0f0feb-5e75-4bb9-8c73-26bcfb60c7cc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "43" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# Creación de una función que devuelve otra\n", "#\n", "def make_incrementor(n):\n", " return lambda x: x + n\n", "\n", "\n", "f = make_incrementor(42)\n", "f(1)" ] }, { "cell_type": "code", "execution_count": 7, "id": "66c70ef9-f1a6-487b-bf94-a309ce14652f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# Función compuesta de varias subfunciones\n", "# Principio de responsabilidad simple:\n", "# cada función hace una tarea y solo una tarea\n", "#\n", "def complex_function(x):\n", " def constant():\n", " return 1\n", "\n", " def first():\n", " return x\n", "\n", " def second():\n", " return x ** 2\n", "\n", " result = constant() + first() + second()\n", " return result\n", "\n", "\n", "complex_function(2)" ] } ], "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.6.9" } }, "nbformat": 4, "nbformat_minor": 5 }