From 9e87f05f43e525a5714fa5e981e96b5a13fa8273 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Mon, 26 Jan 2015 15:43:27 -0500 Subject: [PATCH] Added lambda snippet. --- core/functions.ipynb | 47 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/core/functions.ipynb b/core/functions.ipynb index 39e14da..c86eb17 100644 --- a/core/functions.ipynb +++ b/core/functions.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:93f08d982c73b7b50d2c679984dc25a00cc198f32a1a82269c2c119da5d0e26e" + "signature": "sha256:a37a234cd37895919493dabe7ab645bb2b5e24d9b304ab564e18b59eac4f7a0a" }, "nbformat": 3, "nbformat_minor": 0, @@ -20,7 +20,7 @@ "metadata": {}, "source": [ "* Functions as Objects\n", - "* Lambdas\n", + "* Lambda Functions\n", "* Closures\n", "* \\*args, \\*\\*kwargs\n", "* Currying\n", @@ -157,6 +157,49 @@ } ], "prompt_number": 3 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Lambda Functions" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Lambda functions are anonymous function and are convenient for data analysis, as data transformation functions take functions as arguments." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Sort a sequence of strings by the number of letters" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "strings = ['foo', 'bar,', 'baz', 'f', 'fo', 'b', 'ba']\n", + "strings.sort(key=lambda x: len(list(x)))\n", + "strings" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 7, + "text": [ + "['f', 'b', 'fo', 'ba', 'foo', 'baz', 'bar,']" + ] + } + ], + "prompt_number": 7 } ], "metadata": {}