From a4194fbb1b66bc93d6709822e4d1337fb177cd1e Mon Sep 17 00:00:00 2001 From: Pietro Brunetti Date: Tue, 18 Jul 2017 16:36:45 +0200 Subject: [PATCH] added partial of HOF, memoization and iter --- fp_toolbox.ipynb | 293 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 270 insertions(+), 23 deletions(-) diff --git a/fp_toolbox.ipynb b/fp_toolbox.ipynb index 8543674..e054439 100644 --- a/fp_toolbox.ipynb +++ b/fp_toolbox.ipynb @@ -321,7 +321,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "100000 loops, best of 3: 4.81 µs per loop\n" + "100000 loops, best of 3: 4.77 µs per loop\n" ] } ], @@ -368,7 +368,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "1000000 loops, best of 3: 979 ns per loop\n" + "The slowest run took 4.12 times longer than the fastest. This could mean that an intermediate result is being cached.\n", + "1000000 loops, best of 3: 998 ns per loop\n" ] } ], @@ -524,7 +525,7 @@ { "data": { "text/plain": [ - " at 0x7fd6b40577d8>" + " at 0x7fcee51d60a0>" ] }, "execution_count": 16, @@ -625,7 +626,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "100000 loops, best of 3: 7.5 µs per loop\n" + "100000 loops, best of 3: 7.24 µs per loop\n" ] } ], @@ -1007,6 +1008,103 @@ "[triple(x) for x in range(7)]" ] }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "`partial` può utilizzare una HOF, con risultati molto divertenti." + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "collapsed": false, + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 3, 6, 9, 12, 15, 18]" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "triple_each = partial(map, triple)\n", + "list(triple_each(range(7)))" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": { + "collapsed": false, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "-0.026020276905610557" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from math import sin\n", + "\n", + "def x_sin_y(x, y):\n", + " return x * sin(y)\n", + "\n", + "sinner = partial(reduce, x_sin_y)\n", + "sinner(range(1, 7))" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "collapsed": false, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 4, 5, 7, 8, 10, 11]" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def is_not_multiple_by_3(x):\n", + " return bool(x % 3)\n", + "\n", + "filtering_out_multiple_by_3 = partial(filter, is_not_multiple_by_3)\n", + "list(filtering_out_multiple_by_3(range(13)))" + ] + }, { "cell_type": "markdown", "metadata": { @@ -1022,7 +1120,7 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 32, "metadata": { "collapsed": true, "slideshow": { @@ -1040,7 +1138,7 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 33, "metadata": { "collapsed": false, "slideshow": { @@ -1054,7 +1152,7 @@ "[(1, 1), (1, 2), (2, 4), (3, 7), (5, 12), (8, 20), (13, 33)]" ] }, - "execution_count": 41, + "execution_count": 33, "metadata": {}, "output_type": "execute_result" } @@ -1068,7 +1166,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 34, "metadata": { "collapsed": false, "slideshow": { @@ -1082,7 +1180,7 @@ "[0, 1, 2, 0, 1, 2, 3, 4, 0, 1]" ] }, - "execution_count": 42, + "execution_count": 34, "metadata": {}, "output_type": "execute_result" } @@ -1095,7 +1193,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 35, "metadata": { "collapsed": false, "slideshow": { @@ -1109,7 +1207,7 @@ "[0, 1, 2, 'A', 'B', 'C', 'D', 0, 1, 2]" ] }, - "execution_count": 45, + "execution_count": 35, "metadata": {}, "output_type": "execute_result" } @@ -1125,7 +1223,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 36, "metadata": { "collapsed": false, "slideshow": { @@ -1156,7 +1254,7 @@ " 'zip_longest']" ] }, - "execution_count": 46, + "execution_count": 36, "metadata": {}, "output_type": "execute_result" } @@ -1169,28 +1267,177 @@ { "cell_type": "markdown", "metadata": { - "collapsed": true, "slideshow": { "slide_type": "slide" } }, "source": [ - "# Bibliografia\n", + "## Memoinizzazione\n", + "Le funzioni ricorsive possono conserverare i precedenti valori in una cache, così da non doverli ricalcolare ogni volta.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": { + "collapsed": false, + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CPU times: user 0 ns, sys: 0 ns, total: 0 ns\n", + "Wall time: 230 µs\n" + ] + }, + { + "data": { + "text/plain": [ + "280571172992510140037611932413038677189525" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from functools import lru_cache\n", "\n", - "Gli esempi sono stati presi soprattutto dai seguenti volumi:\n", - " - Luciano Ramalho - Fluent Python - O'Reilly\n", - " - David Mertz - Functional Programming in Python - O'Reilly\n", - " - David Beazley - Generator Tricks for Systems Programmers - Talk al PyCon UK 2008" + "@lru_cache()\n", + "def fibonacci(n):\n", + " if n < 2:\n", + " return n\n", + " return fibonacci(n-2) + fibonacci(n-1)\n", + " \n", + "%time fibonacci(200)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": { - "collapsed": true + "collapsed": false, + "slideshow": { + "slide_type": "fragment" + } }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CPU times: user 0 ns, sys: 0 ns, total: 0 ns\n", + "Wall time: 18.8 µs\n" + ] + }, + { + "data": { + "text/plain": [ + "4244200115309993198876969489421897548446236915" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "%time fibonacci(220)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## iter\n", + "\n", + "iter può anche ricevere un valore sentinella opzionale, si solleva `StopIteration` se c'è lo `yield` di quel valore." + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": { + "collapsed": false, + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from random import randint\n", + "\n", + "def d6():\n", + " return randint(1, 6)\n", + "\n", + "d6_iter = iter(d6, 1)\n", + "d6_iter\n" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": { + "collapsed": false, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6\n", + "3\n", + "3\n", + "2\n", + "3\n" + ] + } + ], + "source": [ + "for roll in d6_iter:\n", + " print(roll)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Bibliografia\n", + "\n", + "Gli esempi sono stati presi soprattutto dai seguenti volumi:\n", + " - Luciano Ramalho - Fluent Python - O'Reilly\n", + " - David Mertz - Functional Programming in Python - O'Reilly\n", + " - David Beazley - Generator Tricks for Systems Programmers - Talk al PyCon UK 2008\n", + " - Joel Grus - Data Science from scratch - 0'Reilly" + ] } ], "metadata": {