Skip to content

Instantly share code, notes, and snippets.

@pembeci
Last active May 12, 2017 13:04

Revisions

  1. pembeci revised this gist May 12, 2017. 3 changed files with 555 additions and 185 deletions.
    67 changes: 67 additions & 0 deletions closures.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    b = 8

    def do_something(a):
    global b
    b += 1
    return a + b


    print(do_something(40))
    print("b is now", b)
    print(do_something(40))
    print(do_something(40))


    import time

    def make_counter():
    print("IN make_counter")
    cnt = 0
    time.sleep(2)
    def count():
    nonlocal cnt
    print("IN count")
    cnt += 1
    print("COUNT=", cnt)
    print("RETURNING FROM make_counter")
    return count


    c1 = make_counter()
    c2 = make_counter()
    c3 = make_counter()


    c1()
    c1()
    c1()
    c2()
    c3()
    c2()
    c1()



    def make_counter2(start_val=0, incr=1):
    cnt = start_val

    def count():
    message = "COUNT="
    nonlocal cnt
    print("IN count")
    cnt += incr
    print(message, cnt)
    message = "NEW MESG="

    return count

    c4 = make_counter2() # 1, 2, 3, 4, ...
    c5 = make_counter2(100,5) # 105, 110, 115, 120, ...
    c6 = make_counter2(20) # 21, 22, 23, 24, ...







    20 changes: 20 additions & 0 deletions fib_memoization.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    def memoize(f):
    memo = {}
    def helper(x):
    if x not in memo:
    memo[x] = f(x)
    return memo[x]
    return helper


    def fib(n):
    if n == 0:
    return 0
    elif n == 1:
    return 1
    else:
    return fib(n-1) + fib(n-2)

    # fib = memoize(fib)
    # print(fib(40))

    653 changes: 468 additions & 185 deletions CENG2002 - Lecture 1.ipynb → lecture.ipynb
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 50,
    "execution_count": 1,
    "metadata": {
    "collapsed": true
    },
    @@ -30,7 +30,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 51,
    "execution_count": 2,
    "metadata": {},
    "outputs": [
    {
    @@ -39,7 +39,7 @@
    "25"
    ]
    },
    "execution_count": 51,
    "execution_count": 2,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -50,7 +50,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 52,
    "execution_count": 3,
    "metadata": {},
    "outputs": [
    {
    @@ -59,7 +59,7 @@
    "function"
    ]
    },
    "execution_count": 52,
    "execution_count": 3,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -70,7 +70,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 53,
    "execution_count": 4,
    "metadata": {},
    "outputs": [
    {
    @@ -79,7 +79,7 @@
    "int"
    ]
    },
    "execution_count": 53,
    "execution_count": 4,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -90,7 +90,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 54,
    "execution_count": 5,
    "metadata": {},
    "outputs": [
    {
    @@ -99,7 +99,7 @@
    "5"
    ]
    },
    "execution_count": 54,
    "execution_count": 5,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -112,7 +112,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 55,
    "execution_count": 6,
    "metadata": {
    "collapsed": true
    },
    @@ -123,7 +123,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 56,
    "execution_count": 7,
    "metadata": {},
    "outputs": [
    {
    @@ -132,7 +132,7 @@
    "25"
    ]
    },
    "execution_count": 56,
    "execution_count": 7,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -143,7 +143,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 57,
    "execution_count": 8,
    "metadata": {
    "collapsed": true
    },
    @@ -158,7 +158,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 58,
    "execution_count": 9,
    "metadata": {
    "collapsed": true
    },
    @@ -169,7 +169,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 59,
    "execution_count": 10,
    "metadata": {},
    "outputs": [
    {
    @@ -178,7 +178,7 @@
    "<function __main__.triple>"
    ]
    },
    "execution_count": 59,
    "execution_count": 10,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -189,7 +189,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 60,
    "execution_count": 11,
    "metadata": {},
    "outputs": [
    {
    @@ -198,7 +198,7 @@
    "21"
    ]
    },
    "execution_count": 60,
    "execution_count": 11,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -209,7 +209,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 61,
    "execution_count": 12,
    "metadata": {},
    "outputs": [
    {
    @@ -230,7 +230,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 62,
    "execution_count": 13,
    "metadata": {
    "collapsed": true
    },
    @@ -242,7 +242,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 63,
    "execution_count": 14,
    "metadata": {
    "collapsed": true
    },
    @@ -253,7 +253,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 64,
    "execution_count": 15,
    "metadata": {},
    "outputs": [
    {
    @@ -270,7 +270,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 65,
    "execution_count": 16,
    "metadata": {},
    "outputs": [
    {
    @@ -279,7 +279,7 @@
    "100"
    ]
    },
    "execution_count": 65,
    "execution_count": 16,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -298,7 +298,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 66,
    "execution_count": 17,
    "metadata": {
    "collapsed": true
    },
    @@ -310,7 +310,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 67,
    "execution_count": 18,
    "metadata": {},
    "outputs": [
    {
    @@ -319,7 +319,7 @@
    "81"
    ]
    },
    "execution_count": 67,
    "execution_count": 18,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -330,7 +330,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 68,
    "execution_count": 19,
    "metadata": {},
    "outputs": [
    {
    @@ -339,7 +339,7 @@
    "90"
    ]
    },
    "execution_count": 68,
    "execution_count": 19,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -350,16 +350,18 @@
    },
    {
    "cell_type": "code",
    "execution_count": 70,
    "metadata": {},
    "execution_count": 20,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "# apply_twice(len,\"abcd\")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 71,
    "execution_count": 21,
    "metadata": {
    "collapsed": true
    },
    @@ -371,7 +373,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 72,
    "execution_count": 22,
    "metadata": {},
    "outputs": [
    {
    @@ -380,7 +382,7 @@
    "'[cat]'"
    ]
    },
    "execution_count": 72,
    "execution_count": 22,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -391,7 +393,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 73,
    "execution_count": 23,
    "metadata": {},
    "outputs": [
    {
    @@ -400,7 +402,7 @@
    "'[[cat]]'"
    ]
    },
    "execution_count": 73,
    "execution_count": 23,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -411,7 +413,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 74,
    "execution_count": 24,
    "metadata": {
    "collapsed": true
    },
    @@ -427,7 +429,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 75,
    "execution_count": 25,
    "metadata": {},
    "outputs": [
    {
    @@ -436,7 +438,7 @@
    "27"
    ]
    },
    "execution_count": 75,
    "execution_count": 25,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -447,7 +449,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 76,
    "execution_count": 26,
    "metadata": {
    "collapsed": true
    },
    @@ -462,7 +464,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 77,
    "execution_count": 27,
    "metadata": {
    "collapsed": true
    },
    @@ -473,7 +475,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 78,
    "execution_count": 28,
    "metadata": {},
    "outputs": [
    {
    @@ -482,7 +484,7 @@
    "24"
    ]
    },
    "execution_count": 78,
    "execution_count": 28,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -493,7 +495,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 79,
    "execution_count": 29,
    "metadata": {},
    "outputs": [
    {
    @@ -502,7 +504,7 @@
    "34"
    ]
    },
    "execution_count": 79,
    "execution_count": 29,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -513,7 +515,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 80,
    "execution_count": 30,
    "metadata": {
    "collapsed": true
    },
    @@ -526,7 +528,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 81,
    "execution_count": 31,
    "metadata": {},
    "outputs": [
    {
    @@ -554,7 +556,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 82,
    "execution_count": 32,
    "metadata": {},
    "outputs": [
    {
    @@ -563,7 +565,7 @@
    "[1, 4, 9, 16, 25, 36, 49, 64, 81]"
    ]
    },
    "execution_count": 82,
    "execution_count": 32,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -578,7 +580,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 83,
    "execution_count": 33,
    "metadata": {},
    "outputs": [
    {
    @@ -587,7 +589,7 @@
    "[1, 4, 9, 16, 25, 36, 49, 64, 81]"
    ]
    },
    "execution_count": 83,
    "execution_count": 33,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -598,7 +600,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 84,
    "execution_count": 34,
    "metadata": {},
    "outputs": [
    {
    @@ -607,7 +609,7 @@
    "[5, 3, 2, 10]"
    ]
    },
    "execution_count": 84,
    "execution_count": 34,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -618,7 +620,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 85,
    "execution_count": 35,
    "metadata": {},
    "outputs": [
    {
    @@ -627,7 +629,7 @@
    "'Izzeet pembec\\xc4\\xb0'"
    ]
    },
    "execution_count": 85,
    "execution_count": 35,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -638,7 +640,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 86,
    "execution_count": 36,
    "metadata": {
    "collapsed": true
    },
    @@ -649,7 +651,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 87,
    "execution_count": 37,
    "metadata": {
    "collapsed": true
    },
    @@ -669,7 +671,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 88,
    "execution_count": 38,
    "metadata": {},
    "outputs": [
    {
    @@ -686,7 +688,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 89,
    "execution_count": 39,
    "metadata": {},
    "outputs": [
    {
    @@ -695,7 +697,7 @@
    "'Ali Can Kayaturk'"
    ]
    },
    "execution_count": 89,
    "execution_count": 39,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -706,7 +708,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 90,
    "execution_count": 40,
    "metadata": {},
    "outputs": [
    {
    @@ -715,7 +717,7 @@
    "['HEL', 'WOR', 'THI', 'IS', 'AN', 'EXA']"
    ]
    },
    "execution_count": 90,
    "execution_count": 40,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -743,7 +745,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 91,
    "execution_count": 41,
    "metadata": {},
    "outputs": [
    {
    @@ -752,7 +754,7 @@
    "['Hel', 'wor', 'Thi', 'is', 'an', 'exa']"
    ]
    },
    "execution_count": 91,
    "execution_count": 41,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -763,7 +765,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 92,
    "execution_count": 42,
    "metadata": {},
    "outputs": [
    {
    @@ -772,7 +774,7 @@
    "['HEL', 'WOR', 'THI', 'IS', 'AN', 'EXA']"
    ]
    },
    "execution_count": 92,
    "execution_count": 42,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -783,7 +785,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 93,
    "execution_count": 43,
    "metadata": {
    "collapsed": true
    },
    @@ -797,7 +799,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 94,
    "execution_count": 44,
    "metadata": {},
    "outputs": [
    {
    @@ -806,7 +808,7 @@
    "[15, 32, 36, 308, 540]"
    ]
    },
    "execution_count": 94,
    "execution_count": 44,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -817,7 +819,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 95,
    "execution_count": 45,
    "metadata": {},
    "outputs": [
    {
    @@ -826,7 +828,7 @@
    "[0, 2, 0, 0, 2, 5]"
    ]
    },
    "execution_count": 95,
    "execution_count": 45,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -837,7 +839,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 96,
    "execution_count": 46,
    "metadata": {},
    "outputs": [
    {
    @@ -846,7 +848,7 @@
    "['EEE', 'XXXX', 'AAAAA', 'MMMMMM']"
    ]
    },
    "execution_count": 96,
    "execution_count": 46,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -857,7 +859,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 97,
    "execution_count": 47,
    "metadata": {},
    "outputs": [
    {
    @@ -878,7 +880,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 98,
    "execution_count": 48,
    "metadata": {},
    "outputs": [
    {
    @@ -887,7 +889,7 @@
    "['E*', 'X+', 'A-', 'M/']"
    ]
    },
    "execution_count": 98,
    "execution_count": 48,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -905,7 +907,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 110,
    "execution_count": 49,
    "metadata": {
    "collapsed": true
    },
    @@ -925,7 +927,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 104,
    "execution_count": 50,
    "metadata": {},
    "outputs": [
    {
    @@ -935,6 +937,7 @@
    " False,\n",
    " True,\n",
    " False,\n",
    " False,\n",
    " True,\n",
    " False,\n",
    " True,\n",
    @@ -946,7 +949,7 @@
    " False]"
    ]
    },
    "execution_count": 104,
    "execution_count": 50,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -957,7 +960,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 111,
    "execution_count": 51,
    "metadata": {},
    "outputs": [
    {
    @@ -966,7 +969,7 @@
    "[1, 3, 5, 7, 9, 11, 13]"
    ]
    },
    "execution_count": 111,
    "execution_count": 51,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -977,7 +980,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 112,
    "execution_count": 52,
    "metadata": {},
    "outputs": [
    {
    @@ -986,7 +989,7 @@
    "[2, 4, 42, 6, 8, 24, 26]"
    ]
    },
    "execution_count": 112,
    "execution_count": 52,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -997,7 +1000,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 113,
    "execution_count": 53,
    "metadata": {},
    "outputs": [
    {
    @@ -1006,7 +1009,7 @@
    "[42, 24, 26]"
    ]
    },
    "execution_count": 113,
    "execution_count": 53,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -1017,7 +1020,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 114,
    "execution_count": 54,
    "metadata": {},
    "outputs": [
    {
    @@ -1026,7 +1029,7 @@
    "[11, 13]"
    ]
    },
    "execution_count": 114,
    "execution_count": 54,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -1037,7 +1040,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 115,
    "execution_count": 55,
    "metadata": {
    "collapsed": true
    },
    @@ -1055,7 +1058,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 120,
    "execution_count": 56,
    "metadata": {},
    "outputs": [
    {
    @@ -1077,7 +1080,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 122,
    "execution_count": 57,
    "metadata": {},
    "outputs": [
    {
    @@ -1086,7 +1089,7 @@
    "'Hello\\nworld.\\nThis\\nis\\nan\\nexample.'"
    ]
    },
    "execution_count": 122,
    "execution_count": 57,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -1097,7 +1100,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 123,
    "execution_count": 58,
    "metadata": {},
    "outputs": [
    {
    @@ -1119,7 +1122,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 128,
    "execution_count": 59,
    "metadata": {
    "collapsed": true
    },
    @@ -1130,16 +1133,17 @@
    },
    {
    "cell_type": "code",
    "execution_count": 127,
    "execution_count": 60,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[{'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3}]"
    "[{'gender': 'M', 'gpa': 3.1, 'name': 'Veli', 'year': 1},\n",
    " {'gender': 'F', 'gpa': 3.5, 'name': 'Dilek', 'year': 2}]"
    ]
    },
    "execution_count": 127,
    "execution_count": 60,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -1150,7 +1154,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 129,
    "execution_count": 61,
    "metadata": {},
    "outputs": [
    {
    @@ -1168,7 +1172,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 130,
    "execution_count": 62,
    "metadata": {},
    "outputs": [
    {
    @@ -1177,7 +1181,7 @@
    "[1, 2, 3, 7, 8, 9]"
    ]
    },
    "execution_count": 130,
    "execution_count": 62,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -1188,7 +1192,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 131,
    "execution_count": 63,
    "metadata": {
    "collapsed": true
    },
    @@ -1200,7 +1204,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 132,
    "execution_count": 64,
    "metadata": {},
    "outputs": [
    {
    @@ -1209,7 +1213,7 @@
    "[1, 2, 3, 7, 8, 9]"
    ]
    },
    "execution_count": 132,
    "execution_count": 64,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -1220,16 +1224,18 @@
    },
    {
    "cell_type": "code",
    "execution_count": 135,
    "metadata": {},
    "execution_count": 65,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "words.sort()"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 136,
    "execution_count": 66,
    "metadata": {},
    "outputs": [
    {
    @@ -1238,7 +1244,7 @@
    "['Hello', 'This', 'an', 'example.', 'is', 'world.']"
    ]
    },
    "execution_count": 136,
    "execution_count": 66,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -1249,7 +1255,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 137,
    "execution_count": 67,
    "metadata": {
    "collapsed": true
    },
    @@ -1260,7 +1266,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 138,
    "execution_count": 68,
    "metadata": {},
    "outputs": [
    {
    @@ -1274,7 +1280,7 @@
    " {'gender': 'M', 'gpa': 3.1, 'name': 'Veli', 'year': 1}]"
    ]
    },
    "execution_count": 138,
    "execution_count": 68,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -1285,7 +1291,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 139,
    "execution_count": 69,
    "metadata": {
    "collapsed": true
    },
    @@ -1296,7 +1302,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 141,
    "execution_count": 70,
    "metadata": {
    "collapsed": true
    },
    @@ -1307,7 +1313,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 142,
    "execution_count": 71,
    "metadata": {},
    "outputs": [
    {
    @@ -1321,7 +1327,7 @@
    " {'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3}]"
    ]
    },
    "execution_count": 142,
    "execution_count": 71,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -1332,7 +1338,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 144,
    "execution_count": 72,
    "metadata": {},
    "outputs": [
    {
    @@ -1346,7 +1352,7 @@
    " {'gender': 'F', 'gpa': 2.8, 'name': 'Zeynep', 'year': 3}]"
    ]
    },
    "execution_count": 144,
    "execution_count": 72,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -1357,7 +1363,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 145,
    "execution_count": 73,
    "metadata": {},
    "outputs": [
    {
    @@ -1366,12 +1372,12 @@
    "[{'gender': 'M', 'gpa': 3.1, 'name': 'Veli', 'year': 1},\n",
    " {'gender': 'F', 'gpa': 3.5, 'name': 'Dilek', 'year': 2},\n",
    " {'gender': 'F', 'gpa': 2.5, 'name': 'Oya', 'year': 2},\n",
    " {'gender': 'F', 'gpa': 2.8, 'name': 'Zeynep', 'year': 3},\n",
    " {'gender': 'M', 'gpa': 2.8, 'name': 'Ali', 'year': 3},\n",
    " {'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3},\n",
    " {'gender': 'F', 'gpa': 2.8, 'name': 'Zeynep', 'year': 3}]"
    " {'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3}]"
    ]
    },
    "execution_count": 145,
    "execution_count": 73,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -1382,7 +1388,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 148,
    "execution_count": 74,
    "metadata": {},
    "outputs": [
    {
    @@ -1391,12 +1397,12 @@
    "[{'gender': 'M', 'gpa': 3.1, 'name': 'Veli', 'year': 1},\n",
    " {'gender': 'F', 'gpa': 3.5, 'name': 'Dilek', 'year': 2},\n",
    " {'gender': 'F', 'gpa': 2.5, 'name': 'Oya', 'year': 2},\n",
    " {'gender': 'M', 'gpa': 2.8, 'name': 'Ali', 'year': 3},\n",
    " {'gender': 'F', 'gpa': 2.8, 'name': 'Zeynep', 'year': 3},\n",
    " {'gender': 'M', 'gpa': 2.8, 'name': 'Ali', 'year': 3},\n",
    " {'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3}]"
    ]
    },
    "execution_count": 148,
    "execution_count": 74,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -1414,8 +1420,10 @@
    },
    {
    "cell_type": "code",
    "execution_count": 152,
    "metadata": {},
    "execution_count": 75,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def add2(partial,next_val):\n",
    @@ -1425,7 +1433,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 153,
    "execution_count": 76,
    "metadata": {},
    "outputs": [
    {
    @@ -1445,7 +1453,7 @@
    "28"
    ]
    },
    "execution_count": 153,
    "execution_count": 76,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -1456,7 +1464,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 154,
    "execution_count": 77,
    "metadata": {},
    "outputs": [
    {
    @@ -1477,7 +1485,7 @@
    "28"
    ]
    },
    "execution_count": 154,
    "execution_count": 77,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -1488,7 +1496,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 158,
    "execution_count": 78,
    "metadata": {},
    "outputs": [
    {
    @@ -1497,7 +1505,7 @@
    "210"
    ]
    },
    "execution_count": 158,
    "execution_count": 78,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -1508,7 +1516,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 161,
    "execution_count": 79,
    "metadata": {
    "collapsed": true
    },
    @@ -1522,7 +1530,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 162,
    "execution_count": 80,
    "metadata": {},
    "outputs": [
    {
    @@ -1540,7 +1548,7 @@
    "210"
    ]
    },
    "execution_count": 162,
    "execution_count": 80,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -1551,7 +1559,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 164,
    "execution_count": 81,
    "metadata": {
    "collapsed": true
    },
    @@ -1568,7 +1576,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 165,
    "execution_count": 82,
    "metadata": {},
    "outputs": [
    {
    @@ -1594,7 +1602,7 @@
    "15"
    ]
    },
    "execution_count": 165,
    "execution_count": 82,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -1605,7 +1613,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 172,
    "execution_count": 83,
    "metadata": {
    "collapsed": true
    },
    @@ -1622,34 +1630,34 @@
    },
    {
    "cell_type": "code",
    "execution_count": 173,
    "execution_count": 84,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Partial= [] \tNext= 4 \tResult= []\n",
    "Partial= [] \tNext= 7 \tResult= [7]\n",
    "Partial= [7] \tNext= 5 \tResult= [7, 5]\n",
    "Partial= [7, 5] \tNext= 9 \tResult= [7, 5, 9]\n",
    "Partial= [7, 5, 9] \tNext= 3 \tResult= [7, 5, 9, 3]\n",
    "Partial= [7, 5, 9, 3] \tNext= 9 \tResult= [7, 5, 9, 3, 9]\n",
    "Partial= [7, 5, 9, 3, 9] \tNext= 5 \tResult= [7, 5, 9, 3, 9, 5]\n",
    "Partial= [7, 5, 9, 3, 9, 5] \tNext= 11 \tResult= [7, 5, 9, 3, 9, 5, 11]\n",
    "Partial= [7, 5, 9, 3, 9, 5, 11] \tNext= 8 \tResult= [7, 5, 9, 3, 9, 5, 11]\n",
    "Partial= [7, 5, 9, 3, 9, 5, 11] \tNext= 15 \tResult= [7, 5, 9, 3, 9, 5, 11, 15]\n",
    "Partial= [7, 5, 9, 3, 9, 5, 11, 15] \tNext= 3 \tResult= [7, 5, 9, 3, 9, 5, 11, 15, 3]\n",
    "Partial= [7, 5, 9, 3, 9, 5, 11, 15, 3] \tNext= 10 \tResult= [7, 5, 9, 3, 9, 5, 11, 15, 3]\n"
    "Partial= [] \tNext= 4 \tResult= [4]\n",
    "Partial= [4] \tNext= 7 \tResult= [4]\n",
    "Partial= [4] \tNext= 5 \tResult= [4]\n",
    "Partial= [4] \tNext= 9 \tResult= [4]\n",
    "Partial= [4] \tNext= 3 \tResult= [4]\n",
    "Partial= [4] \tNext= 9 \tResult= [4]\n",
    "Partial= [4] \tNext= 5 \tResult= [4]\n",
    "Partial= [4] \tNext= 11 \tResult= [4]\n",
    "Partial= [4] \tNext= 8 \tResult= [4, 8]\n",
    "Partial= [4, 8] \tNext= 15 \tResult= [4, 8]\n",
    "Partial= [4, 8] \tNext= 3 \tResult= [4, 8]\n",
    "Partial= [4, 8] \tNext= 10 \tResult= [4, 8, 10]\n"
    ]
    },
    {
    "data": {
    "text/plain": [
    "[7, 5, 9, 3, 9, 5, 11, 15, 3]"
    "[4, 8, 10]"
    ]
    },
    "execution_count": 173,
    "execution_count": 84,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -1660,7 +1668,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 175,
    "execution_count": 85,
    "metadata": {
    "collapsed": true
    },
    @@ -1678,7 +1686,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 176,
    "execution_count": 86,
    "metadata": {},
    "outputs": [
    {
    @@ -1711,7 +1719,7 @@
    "{'even': [4, 6, 8, 6, 4], 'odd': [3, 5, 7, 11]}"
    ]
    },
    "execution_count": 176,
    "execution_count": 86,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -1722,11 +1730,20 @@
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "execution_count": 87,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "'\\nvar teams = [\\n { name: \"HataySpor\", \"wins\": 3, \"draws\": 2, \"losses\": 1},\\n { name: \"HataySporrr\", \"wins\": 3, \"draws\": 2, \"losses\": 1},\\n { name: \"Karab\\xc3\\xbck\", \"wins\": 4, \"draws\": 0, \"losses\": 1},\\n { name: \"Mu\\xc4\\x9flaspor\", \"wins\": 7, \"draws\": 1, \"losses\": 0},\\n { name: \"UlaG\\xc3\\xbcc\\xc3\\xbc\", \"wins\": 1, \"draws\": 3, \"losses\": 0},\\n { name: \"K\\xc3\\xb6tekli\", \"wins\": 1, \"draws\": 7, \"losses\": 0},\\n { name: \"Dalaman\", \"wins\": 2, \"draws\": 0, \"losses\": 2}\\n]\\nteams.map(function(t) {\\n t.points = 2*t.wins+t.draws;\\n t.played=t.wins+t.draws+t.losses\\n})\\nteams\\n .filter(function(t) { return t.played >= 5;})\\n .sort(function (t1,t2) {\\n if (t1.points != t2.points) return t2.points - t1.points;\\n else if (t1.played != t2.played) return t1.played - t2.played;\\n else return t2.name.length - t1.name.length;\\n })\\n .forEach(function(t) {\\n t.flag = true; // will this affect teams[4] output below?\\n console.log(t.name + \": \" + t.points + \"-\" + t.played);\\n })\\nconsole.log(teams[4]);\\n'"
    ]
    },
    "execution_count": 87,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "\"\"\"\n",
    "var teams = [\n",
    @@ -1759,7 +1776,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 180,
    "execution_count": 88,
    "metadata": {
    "collapsed": true
    },
    @@ -1778,7 +1795,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 196,
    "execution_count": 89,
    "metadata": {},
    "outputs": [
    {
    @@ -1808,7 +1825,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 193,
    "execution_count": 90,
    "metadata": {
    "scrolled": true
    },
    @@ -1848,7 +1865,7 @@
    " 'wins': 1}]"
    ]
    },
    "execution_count": 193,
    "execution_count": 90,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -1859,39 +1876,305 @@
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": []
    "execution_count": 91,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "XHelloY\n",
    "<<Summer>>\n",
    "[EXAM]\n",
    "<<EXAM>>\n",
    "*EXAM*\n",
    "EXAM??EXAM\n",
    "EM\n"
    ]
    }
    ],
    "source": [
    "def marker_fact(b, e=None):\n",
    " if e == None: e=b\n",
    " \n",
    " def marker(w):\n",
    " return b + w + e\n",
    " \n",
    " return marker\n",
    "\n",
    "print marker_fact(\"X\", \"Y\")(\"Hello\")\n",
    "markers = [marker_fact(\"[\", \"]\"), marker_fact(\"<<\", \">>\"), \n",
    " marker_fact(\"*\")]\n",
    "print markers[1](\"Summer\")\n",
    "\n",
    "def reverse_marker(w):\n",
    " return w + \"??\" + w\n",
    "\n",
    "markers.append(reverse_marker)\n",
    "markers.append(lambda w: w[0]+w[-1]) \n",
    "for marker in markers:\n",
    " print marker(\"EXAM\")\n",
    " \n"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": []
    "execution_count": 92,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[<function __main__.marker>,\n",
    " <function __main__.marker>,\n",
    " <function __main__.marker>,\n",
    " <function __main__.reverse_marker>,\n",
    " <function __main__.<lambda>>]"
    ]
    },
    "execution_count": 92,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "markers"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": []
    "execution_count": 98,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "'*ABCDEFG*'"
    ]
    },
    "execution_count": 98,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "markers[2](\"ABCDEFG\")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "execution_count": 100,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "['*What*', '+the+', '|hell|']\n"
    ]
    }
    ],
    "source": [
    "new_markers = map(marker_fact, [\"*\", \"+\", \"|\"])\n",
    "print map(lambda f,w: f(w), new_markers, [\"What\", \"the\", \"hell\"])"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": []
    "source": [
    "### List Comprehensions"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 101,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[6, 16, 12, 2, 10, 18, 24, 90, 30, 8]"
    ]
    },
    "execution_count": 101,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "nums = [3,8,6,1,5,9,12,45,15,4]\n",
    "nums2 = map(lambda n: n*2, nums)\n",
    "nums2"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 102,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[6, 16, 12, 2, 10, 18, 24, 90, 30, 8]"
    ]
    },
    "execution_count": 102,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "nums3 = [2*num for num in nums]\n",
    "nums3"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 103,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[24, 90, 30]"
    ]
    },
    "execution_count": 103,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "nums4 = [2*num for num in nums if num>10]\n",
    "nums4"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 104,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[24, 90, 30]"
    ]
    },
    "execution_count": 104,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "nums5 =map(lambda n:n*2,filter(lambda n:n>10, nums))\n",
    "nums5"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 105,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[(1, 'A'),\n",
    " (1, 'B'),\n",
    " (1, 'C'),\n",
    " (1, 'D'),\n",
    " (2, 'A'),\n",
    " (2, 'B'),\n",
    " (2, 'C'),\n",
    " (2, 'D'),\n",
    " (3, 'A'),\n",
    " (3, 'B'),\n",
    " (3, 'C'),\n",
    " (3, 'D')]"
    ]
    },
    "execution_count": 105,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "[(a,b) for a in [1,2,3] for b in [\"A\", \"B\", \"C\", \"D\"]]"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 109,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "+\n",
    "+\n",
    "++\n",
    "+\n",
    "++\n",
    "+++\n",
    "+\n",
    "++\n",
    "+++\n",
    "++++\n",
    "+\n",
    "++\n",
    "+++\n",
    "++++\n",
    "+++++\n",
    "+\n",
    "++\n",
    "+++\n",
    "++++\n",
    "+++++\n",
    "++++++\n",
    "+\n",
    "++\n",
    "+++\n",
    "++++\n",
    "+++++\n",
    "++++++\n",
    "+++++++\n",
    "+\n",
    "++\n",
    "+++\n",
    "++++\n",
    "+++++\n",
    "++++++\n",
    "+++++++\n",
    "++++++++\n"
    ]
    }
    ],
    "source": [
    "print \"\\n\".join([b * \"+\" for a in range(1,10) for b in range(1,a)])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 111,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[102, 3, 104, 5, 106, 7, 108, 9]"
    ]
    },
    "execution_count": 111,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "[(w+100 if w%2==0 else w) for w in [2,3,4,5,6,7,8,9] ]"
    ]
    },
    {
    "cell_type": "code",
  2. pembeci revised this gist May 11, 2017. 1 changed file with 0 additions and 1945 deletions.
    1,945 changes: 0 additions & 1,945 deletions lecture1.ipynb
    Original file line number Diff line number Diff line change
    @@ -1,1945 +0,0 @@
    {
    "cells": [
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "# Functional Programming / Paradigm"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "## Functions as first class values"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 50,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "\n",
    "\n",
    "def double(x):\n",
    " return 2*x + 1"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 51,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "25"
    ]
    },
    "execution_count": 51,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "double(12)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 52,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "function"
    ]
    },
    "execution_count": 52,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "type(double)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 53,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "int"
    ]
    },
    "execution_count": 53,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "type(5)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 54,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "5"
    ]
    },
    "execution_count": 54,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "a = 5\n",
    "b = a\n",
    "b"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 55,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "new_variable = double"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 56,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "25"
    ]
    },
    "execution_count": 56,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "new_variable(12)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 57,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def triple(x):\n",
    " return 3 * x\n",
    "\n",
    "def square(x):\n",
    " return x*x"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 58,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "my_funcs = [double, triple, square, triple]"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 59,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "<function __main__.triple>"
    ]
    },
    "execution_count": 59,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "my_funcs[1]"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 60,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "21"
    ]
    },
    "execution_count": 60,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "my_funcs[1](7)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 61,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "11\n",
    "15\n",
    "25\n",
    "15\n"
    ]
    }
    ],
    "source": [
    "for fn in my_funcs:\n",
    " print fn(5)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 62,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def cat_talk():\n",
    " print \"Meaw\""
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 63,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "cat = { \"type\": \"animal\", \"max_age\": 15, \"talk\": cat_talk}"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 64,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Meaw\n"
    ]
    }
    ],
    "source": [
    "cat[\"talk\"]()"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 65,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "100"
    ]
    },
    "execution_count": 65,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "new_variable = square\n",
    "new_variable(10)"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "## Higher Order Functions"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 66,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def apply_twice(f,x):\n",
    " return f(f(x))"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 67,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "81"
    ]
    },
    "execution_count": 67,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "apply_twice(square, 3)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 68,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "90"
    ]
    },
    "execution_count": 68,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "apply_twice(triple, 10)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 70,
    "metadata": {},
    "outputs": [],
    "source": [
    "# apply_twice(len,\"abcd\")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 71,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def add_to_string(word):\n",
    " return \"[\" + word + \"]\"\n"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 72,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "'[cat]'"
    ]
    },
    "execution_count": 72,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "add_to_string(\"cat\")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 73,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "'[[cat]]'"
    ]
    },
    "execution_count": 73,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "apply_twice(add_to_string, \"cat\")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 74,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def f(x):\n",
    " c = 5\n",
    " d = 2\n",
    " def g(y):\n",
    " return y*10\n",
    " return c+d+g(x)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 75,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "27"
    ]
    },
    "execution_count": 75,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "f(2)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 76,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def add_fn_factory(x):\n",
    " x = x*2\n",
    " def add(y):\n",
    " return x+y\n",
    " return add"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 77,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "f = add_fn_factory(7)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 78,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "24"
    ]
    },
    "execution_count": 78,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "f(10)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 79,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "34"
    ]
    },
    "execution_count": 79,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "add_fn_factory(7)(20)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 80,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "add5 = add_fn_factory(5)\n",
    "add10 = add_fn_factory(10)\n",
    "add100 = add_fn_factory(100)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 81,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "(13, 23, 203)\n"
    ]
    }
    ],
    "source": [
    "print (add5(3), add10(3), add100(3))"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "## Some common higher order functions\n",
    "\n",
    "### map\n",
    "\n",
    "`map` is used to transform one list (collection) of values into another list by applying a function to every element in a list."
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 82,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[1, 4, 9, 16, 25, 36, 49, 64, 81]"
    ]
    },
    "execution_count": 82,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "nums = [1,2,3,4,5,6,7,8,9]\n",
    "new_nums = []\n",
    "for num in nums:\n",
    " new_nums.append(square(num))\n",
    "new_nums "
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 83,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[1, 4, 9, 16, 25, 36, 49, 64, 81]"
    ]
    },
    "execution_count": 83,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "map(square,nums)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 84,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[5, 3, 2, 10]"
    ]
    },
    "execution_count": 84,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "map(len,[\"abcde\", \"def\", \"gh\", \"asdsfsdvdf\"])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 85,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "'Izzeet pembec\\xc4\\xb0'"
    ]
    },
    "execution_count": 85,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "\"izZEet PEMBECİ\".capitalize()"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 86,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "# -*- coding: utf8 -*-"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 87,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def tr_capitalize(word):\n",
    " first_letter = word[0]\n",
    " others = word[1:]\n",
    " if first_letter == \"i\":\n",
    " return \"İ\" + others.lower()\n",
    " elif first_letter == \"ı\": \n",
    " return \"I\" + others.lower()\n",
    " else:\n",
    " return first_letter.upper() + others.lower()\n",
    " "
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 88,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Ali can kayaturk\n"
    ]
    }
    ],
    "source": [
    "print tr_capitalize(\"Ali can KAYATURK\")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 89,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "'Ali Can Kayaturk'"
    ]
    },
    "execution_count": 89,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "\" \".join(map(tr_capitalize,\"Ali can KAYATURK\".split()))"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 90,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['HEL', 'WOR', 'THI', 'IS', 'AN', 'EXA']"
    ]
    },
    "execution_count": 90,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "words = \"Hello world. This is an example. \".split()\n",
    "\n",
    "def first3(word):\n",
    " return word[:3]\n",
    "\n",
    "map(first3,words)\n",
    "\n",
    "def reverse(word):\n",
    " return word[:3].upper()\n",
    "\n",
    "map(reverse,words)"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "Lambda expressions are a syntactic sugar in Python so you can define anonymous functions easily."
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 91,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['Hel', 'wor', 'Thi', 'is', 'an', 'exa']"
    ]
    },
    "execution_count": 91,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "map(lambda w: w[:3], words)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 92,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['HEL', 'WOR', 'THI', 'IS', 'AN', 'EXA']"
    ]
    },
    "execution_count": 92,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "map(lambda word: word[:3].upper(), words)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 93,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "nums1 = [3,8,9,11,45]\n",
    "nums2 = [5,4,4,28,12]\n",
    "\n",
    "# nums3 = [8,12,13,39,57]"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 94,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[15, 32, 36, 308, 540]"
    ]
    },
    "execution_count": 94,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "map(lambda a,b:a*b, nums1, nums2)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 95,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[0, 2, 0, 0, 2, 5]"
    ]
    },
    "execution_count": 95,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "map(lambda a,b: a % b, [34,56,12,30,50,40], range(2,8))"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 96,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['EEE', 'XXXX', 'AAAAA', 'MMMMMM']"
    ]
    },
    "execution_count": 96,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "map(lambda a,b: a * b, \"EXAM\", range(3,7))"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 97,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "E\n",
    "X\n",
    "A\n",
    "M\n"
    ]
    }
    ],
    "source": [
    "for c in \"EXAM\":\n",
    " print c"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 98,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['E*', 'X+', 'A-', 'M/']"
    ]
    },
    "execution_count": 98,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "map(lambda a,b: a+b, \"EXAM\", [\"*\", \"+\", \"-\", \"/\"])"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "### filter"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 110,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "nums = [1,2,3,4,42,5,6,7,8,9,11,13,24,26]\n",
    "\n",
    "def is_odd(num):\n",
    " return num%2 == 1\n",
    "\n",
    "def is_even(num):\n",
    " return num%2 == 0\n",
    "\n",
    "def is_even_and_large(num):\n",
    " return num%2 == 0 and num > 15"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 104,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[True,\n",
    " False,\n",
    " True,\n",
    " False,\n",
    " True,\n",
    " False,\n",
    " True,\n",
    " False,\n",
    " True,\n",
    " True,\n",
    " True,\n",
    " False,\n",
    " False]"
    ]
    },
    "execution_count": 104,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "map(is_odd ,nums)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 111,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[1, 3, 5, 7, 9, 11, 13]"
    ]
    },
    "execution_count": 111,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "filter(is_odd ,nums)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 112,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[2, 4, 42, 6, 8, 24, 26]"
    ]
    },
    "execution_count": 112,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "filter(is_even ,nums)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 113,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[42, 24, 26]"
    ]
    },
    "execution_count": 113,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "filter(is_even_and_large ,nums)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 114,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[11, 13]"
    ]
    },
    "execution_count": 114,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "filter(lambda n: n>=10 and n<=15 ,nums)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 115,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "students = [ \n",
    " {\"name\": \"Ali\", \"year\": 3, \"gpa\": 2.8, \"gender\": \"M\"}, \n",
    " {\"name\": \"Veli\", \"year\": 1, \"gpa\": 3.1, \"gender\": \"M\"},\n",
    " {\"name\": \"Kaya\", \"year\": 3, \"gpa\": 2.1, \"gender\": \"M\"},\n",
    " {\"name\": \"Oya\", \"year\": 2, \"gpa\": 2.5, \"gender\": \"F\"},\n",
    " {\"name\": \"Zeynep\", \"year\": 3, \"gpa\": 2.8, \"gender\": \"F\"},\n",
    " {\"name\": \"Dilek\", \"year\": 2, \"gpa\": 3.5, \"gender\": \"F\"}, \n",
    " ]"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 120,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Ali: 2.8\n",
    "Veli: 3.1\n",
    "Kaya: 2.1\n",
    "Oya: 2.5\n",
    "Zeynep: 2.8\n",
    "Dilek: 3.5\n"
    ]
    }
    ],
    "source": [
    "print \"\\n\".join(map(lambda student: student[\"name\"] + \": \" + str(student[\"gpa\"]) ,students))"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 122,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "'Hello\\nworld.\\nThis\\nis\\nan\\nexample.'"
    ]
    },
    "execution_count": 122,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "\"\\n\".join(words)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 123,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Hello\n",
    "world.\n",
    "This\n",
    "is\n",
    "an\n",
    "example.\n"
    ]
    }
    ],
    "source": [
    "print \"\\n\".join(words)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 128,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "honor_students = filter(lambda student: student[\"gpa\"]>=3.0, students)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 127,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[{'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3}]"
    ]
    },
    "execution_count": 127,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "honor_students"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 129,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Veli: 3.1\n",
    "Dilek: 3.5\n"
    ]
    }
    ],
    "source": [
    "print \"\\n\".join(map(lambda student: student[\"name\"] + \": \" + str(student[\"gpa\"]) ,honor_students))"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 130,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[1, 2, 3, 7, 8, 9]"
    ]
    },
    "execution_count": 130,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "sorted([3,7,8,2,1,9])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 131,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "values = [3,7,8,2,1,9]\n",
    "values.sort()"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 132,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[1, 2, 3, 7, 8, 9]"
    ]
    },
    "execution_count": 132,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "values"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 135,
    "metadata": {},
    "outputs": [],
    "source": [
    "words.sort()"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 136,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['Hello', 'This', 'an', 'example.', 'is', 'world.']"
    ]
    },
    "execution_count": 136,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "words"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 137,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "students.sort()"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 138,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[{'gender': 'F', 'gpa': 2.5, 'name': 'Oya', 'year': 2},\n",
    " {'gender': 'F', 'gpa': 2.8, 'name': 'Zeynep', 'year': 3},\n",
    " {'gender': 'F', 'gpa': 3.5, 'name': 'Dilek', 'year': 2},\n",
    " {'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3},\n",
    " {'gender': 'M', 'gpa': 2.8, 'name': 'Ali', 'year': 3},\n",
    " {'gender': 'M', 'gpa': 3.1, 'name': 'Veli', 'year': 1}]"
    ]
    },
    "execution_count": 138,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "students"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 139,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "students.sort(key=lambda student: student[\"gpa\"])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 141,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "students.sort(key=lambda student: student[\"gpa\"], reverse=True)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 142,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[{'gender': 'F', 'gpa': 3.5, 'name': 'Dilek', 'year': 2},\n",
    " {'gender': 'M', 'gpa': 3.1, 'name': 'Veli', 'year': 1},\n",
    " {'gender': 'F', 'gpa': 2.8, 'name': 'Zeynep', 'year': 3},\n",
    " {'gender': 'M', 'gpa': 2.8, 'name': 'Ali', 'year': 3},\n",
    " {'gender': 'F', 'gpa': 2.5, 'name': 'Oya', 'year': 2},\n",
    " {'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3}]"
    ]
    },
    "execution_count": 142,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "students"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 144,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[{'gender': 'M', 'gpa': 2.8, 'name': 'Ali', 'year': 3},\n",
    " {'gender': 'F', 'gpa': 3.5, 'name': 'Dilek', 'year': 2},\n",
    " {'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3},\n",
    " {'gender': 'F', 'gpa': 2.5, 'name': 'Oya', 'year': 2},\n",
    " {'gender': 'M', 'gpa': 3.1, 'name': 'Veli', 'year': 1},\n",
    " {'gender': 'F', 'gpa': 2.8, 'name': 'Zeynep', 'year': 3}]"
    ]
    },
    "execution_count": 144,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "sorted(students, key=lambda student: student[\"name\"])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 145,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[{'gender': 'M', 'gpa': 3.1, 'name': 'Veli', 'year': 1},\n",
    " {'gender': 'F', 'gpa': 3.5, 'name': 'Dilek', 'year': 2},\n",
    " {'gender': 'F', 'gpa': 2.5, 'name': 'Oya', 'year': 2},\n",
    " {'gender': 'M', 'gpa': 2.8, 'name': 'Ali', 'year': 3},\n",
    " {'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3},\n",
    " {'gender': 'F', 'gpa': 2.8, 'name': 'Zeynep', 'year': 3}]"
    ]
    },
    "execution_count": 145,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "sorted(students, key=lambda student: student[\"year\"])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 148,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[{'gender': 'M', 'gpa': 3.1, 'name': 'Veli', 'year': 1},\n",
    " {'gender': 'F', 'gpa': 3.5, 'name': 'Dilek', 'year': 2},\n",
    " {'gender': 'F', 'gpa': 2.5, 'name': 'Oya', 'year': 2},\n",
    " {'gender': 'M', 'gpa': 2.8, 'name': 'Ali', 'year': 3},\n",
    " {'gender': 'F', 'gpa': 2.8, 'name': 'Zeynep', 'year': 3},\n",
    " {'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3}]"
    ]
    },
    "execution_count": 148,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "sorted(students, key=lambda student: (student[\"year\"], -student[\"gpa\"]))"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "### reduce"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 152,
    "metadata": {},
    "outputs": [],
    "source": [
    "def add2(partial,next_val):\n",
    " print \"Partial=\", partial, \" Next=\", next_val\n",
    " return partial+next_val"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 153,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Partial= 7 Next= 3\n",
    "Partial= 10 Next= 5\n",
    "Partial= 15 Next= 2\n",
    "Partial= 17 Next= 8\n",
    "Partial= 25 Next= 3\n"
    ]
    },
    {
    "data": {
    "text/plain": [
    "28"
    ]
    },
    "execution_count": 153,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "reduce(add2, [7,3,5,2,8,3])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 154,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Partial= 0 Next= 7\n",
    "Partial= 7 Next= 3\n",
    "Partial= 10 Next= 5\n",
    "Partial= 15 Next= 2\n",
    "Partial= 17 Next= 8\n",
    "Partial= 25 Next= 3\n"
    ]
    },
    {
    "data": {
    "text/plain": [
    "28"
    ]
    },
    "execution_count": 154,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "reduce(add2, [7,3,5,2,8,3], 0)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 158,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "210"
    ]
    },
    "execution_count": 158,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "reduce(lambda prev,new_val: prev*new_val, [7,3,5,2])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 161,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def mult2(partial,next_val):\n",
    " result = partial*next_val\n",
    " print \"Partial=\", partial, \" Next=\", next_val, \" Result=\", result\n",
    " return result"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 162,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Partial= 7 Next= 3 Result= 21\n",
    "Partial= 21 Next= 5 Result= 105\n",
    "Partial= 105 Next= 2 Result= 210\n"
    ]
    },
    {
    "data": {
    "text/plain": [
    "210"
    ]
    },
    "execution_count": 162,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "reduce(mult2, [7,3,5,2])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 164,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def larger(partial,next_val):\n",
    " if partial>next_val:\n",
    " result = partial\n",
    " else:\n",
    " result = next_val\n",
    " print \"Partial=\", partial, \"\\t\\tNext=\", next_val, \"\\t\\tResult=\", result\n",
    " return result"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 165,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Partial= 4 \t\tNext= 7 \t\tResult= 7\n",
    "Partial= 7 \t\tNext= 5 \t\tResult= 7\n",
    "Partial= 7 \t\tNext= 9 \t\tResult= 9\n",
    "Partial= 9 \t\tNext= 3 \t\tResult= 9\n",
    "Partial= 9 \t\tNext= 9 \t\tResult= 9\n",
    "Partial= 9 \t\tNext= 5 \t\tResult= 9\n",
    "Partial= 9 \t\tNext= 11 \t\tResult= 11\n",
    "Partial= 11 \t\tNext= 8 \t\tResult= 11\n",
    "Partial= 11 \t\tNext= 15 \t\tResult= 15\n",
    "Partial= 15 \t\tNext= 3 \t\tResult= 15\n",
    "Partial= 15 \t\tNext= 10 \t\tResult= 15\n"
    ]
    },
    {
    "data": {
    "text/plain": [
    "15"
    ]
    },
    "execution_count": 165,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "reduce(larger, [4,7,5,9,3,9,5,11,8,15,3,10])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 172,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def add_if_even(partial,next_val):\n",
    " if next_val%2 == 0:\n",
    " result = partial + [next_val]\n",
    " else:\n",
    " result = partial\n",
    " print \"Partial=\", partial, \"\\tNext=\", next_val, \"\\tResult=\", result\n",
    " return result"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 173,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Partial= [] \tNext= 4 \tResult= []\n",
    "Partial= [] \tNext= 7 \tResult= [7]\n",
    "Partial= [7] \tNext= 5 \tResult= [7, 5]\n",
    "Partial= [7, 5] \tNext= 9 \tResult= [7, 5, 9]\n",
    "Partial= [7, 5, 9] \tNext= 3 \tResult= [7, 5, 9, 3]\n",
    "Partial= [7, 5, 9, 3] \tNext= 9 \tResult= [7, 5, 9, 3, 9]\n",
    "Partial= [7, 5, 9, 3, 9] \tNext= 5 \tResult= [7, 5, 9, 3, 9, 5]\n",
    "Partial= [7, 5, 9, 3, 9, 5] \tNext= 11 \tResult= [7, 5, 9, 3, 9, 5, 11]\n",
    "Partial= [7, 5, 9, 3, 9, 5, 11] \tNext= 8 \tResult= [7, 5, 9, 3, 9, 5, 11]\n",
    "Partial= [7, 5, 9, 3, 9, 5, 11] \tNext= 15 \tResult= [7, 5, 9, 3, 9, 5, 11, 15]\n",
    "Partial= [7, 5, 9, 3, 9, 5, 11, 15] \tNext= 3 \tResult= [7, 5, 9, 3, 9, 5, 11, 15, 3]\n",
    "Partial= [7, 5, 9, 3, 9, 5, 11, 15, 3] \tNext= 10 \tResult= [7, 5, 9, 3, 9, 5, 11, 15, 3]\n"
    ]
    },
    {
    "data": {
    "text/plain": [
    "[7, 5, 9, 3, 9, 5, 11, 15, 3]"
    ]
    },
    "execution_count": 173,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "reduce(add_if_even, [4,7,5,9,3,9,5,11,8,15,3,10], [])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 175,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def partition(prev_result,next_val):\n",
    " print \"Prev=\", prev_result\n",
    " if next_val%2 == 0:\n",
    " prev_result[\"even\"].append(next_val)\n",
    " else:\n",
    " prev_result[\"odd\"].append(next_val)\n",
    " print \"Next=\", next_val, \"\\tNew=\", prev_result\n",
    " return prev_result"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 176,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Prev= {'even': [], 'odd': []}\n",
    "Next= 4 \tNew= {'even': [4], 'odd': []}\n",
    "Prev= {'even': [4], 'odd': []}\n",
    "Next= 6 \tNew= {'even': [4, 6], 'odd': []}\n",
    "Prev= {'even': [4, 6], 'odd': []}\n",
    "Next= 8 \tNew= {'even': [4, 6, 8], 'odd': []}\n",
    "Prev= {'even': [4, 6, 8], 'odd': []}\n",
    "Next= 3 \tNew= {'even': [4, 6, 8], 'odd': [3]}\n",
    "Prev= {'even': [4, 6, 8], 'odd': [3]}\n",
    "Next= 5 \tNew= {'even': [4, 6, 8], 'odd': [3, 5]}\n",
    "Prev= {'even': [4, 6, 8], 'odd': [3, 5]}\n",
    "Next= 6 \tNew= {'even': [4, 6, 8, 6], 'odd': [3, 5]}\n",
    "Prev= {'even': [4, 6, 8, 6], 'odd': [3, 5]}\n",
    "Next= 7 \tNew= {'even': [4, 6, 8, 6], 'odd': [3, 5, 7]}\n",
    "Prev= {'even': [4, 6, 8, 6], 'odd': [3, 5, 7]}\n",
    "Next= 4 \tNew= {'even': [4, 6, 8, 6, 4], 'odd': [3, 5, 7]}\n",
    "Prev= {'even': [4, 6, 8, 6, 4], 'odd': [3, 5, 7]}\n",
    "Next= 11 \tNew= {'even': [4, 6, 8, 6, 4], 'odd': [3, 5, 7, 11]}\n"
    ]
    },
    {
    "data": {
    "text/plain": [
    "{'even': [4, 6, 8, 6, 4], 'odd': [3, 5, 7, 11]}"
    ]
    },
    "execution_count": 176,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "reduce(partition, [4,6,8,3,5,6,7,4,11], {\"even\":[], \"odd\": []})"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "\"\"\"\n",
    "var teams = [\n",
    " { name: \"HataySpor\", \"wins\": 3, \"draws\": 2, \"losses\": 1},\n",
    " { name: \"HataySporrr\", \"wins\": 3, \"draws\": 2, \"losses\": 1},\n",
    " { name: \"Karabük\", \"wins\": 4, \"draws\": 0, \"losses\": 1},\n",
    " { name: \"Muğlaspor\", \"wins\": 7, \"draws\": 1, \"losses\": 0},\n",
    " { name: \"UlaGücü\", \"wins\": 1, \"draws\": 3, \"losses\": 0},\n",
    " { name: \"Kötekli\", \"wins\": 1, \"draws\": 7, \"losses\": 0},\n",
    " { name: \"Dalaman\", \"wins\": 2, \"draws\": 0, \"losses\": 2}\n",
    "]\n",
    "teams.map(function(t) {\n",
    " t.points = 2*t.wins+t.draws;\n",
    " t.played=t.wins+t.draws+t.losses\n",
    "})\n",
    "teams\n",
    " .filter(function(t) { return t.played >= 5;})\n",
    " .sort(function (t1,t2) {\n",
    " if (t1.points != t2.points) return t2.points - t1.points;\n",
    " else if (t1.played != t2.played) return t1.played - t2.played;\n",
    " else return t2.name.length - t1.name.length;\n",
    " })\n",
    " .forEach(function(t) {\n",
    " t.flag = true; // will this affect teams[4] output below?\n",
    " console.log(t.name + \": \" + t.points + \"-\" + t.played);\n",
    " })\n",
    "console.log(teams[4]);\n",
    "\"\"\""
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 180,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "teams = [\n",
    " { \"name\": \"HataySpor\", \"wins\": 3, \"draws\": 2, \"losses\": 1},\n",
    " { \"name\": \"HataySporrr\", \"wins\": 3, \"draws\": 2, \"losses\": 1},\n",
    " { \"name\": \"Karabuk\", \"wins\": 4, \"draws\": 0, \"losses\": 1},\n",
    " { \"name\": \"Muglaspor\", \"wins\": 7, \"draws\": 1, \"losses\": 0},\n",
    " { \"name\": \"UlaGucu\", \"wins\": 1, \"draws\": 3, \"losses\": 0},\n",
    " { \"name\": \"Kotekli\", \"wins\": 1, \"draws\": 7, \"losses\": 0},\n",
    " { \"name\": \"Dalaman\", \"wins\": 2, \"draws\": 0, \"losses\": 2}\n",
    "]"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 196,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    " Muglaspor 22 8\n",
    " Karabuk 12 5\n",
    " HataySporrr 11 6\n",
    " HataySpor 11 6\n",
    " Kotekli 10 8\n"
    ]
    }
    ],
    "source": [
    "def add_extra_stats(team):\n",
    " team[\"points\"] = team[\"wins\"] * 3 + team[\"draws\"] * 1\n",
    " team[\"games\"] = team[\"wins\"] + team[\"draws\"] + team[\"losses\"]\n",
    " return team\n",
    "teams = map(add_extra_stats, teams)\n",
    "teams = filter(lambda t: t[\"games\"] >= 5, teams)\n",
    "teams.sort(key=lambda t: (t[\"points\"], t[\"games\"], len(t[\"name\"])), reverse=True)\n",
    "\n",
    "for team in teams:\n",
    " print \"%15s %4d %4d\" % (team[\"name\"], team[\"points\"], team[\"games\"])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 193,
    "metadata": {
    "scrolled": true
    },
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[{'draws': 1,\n",
    " 'games': 8,\n",
    " 'losses': 0,\n",
    " 'name': 'Muglaspor',\n",
    " 'points': 22,\n",
    " 'wins': 7},\n",
    " {'draws': 0,\n",
    " 'games': 5,\n",
    " 'losses': 1,\n",
    " 'name': 'Karabuk',\n",
    " 'points': 12,\n",
    " 'wins': 4},\n",
    " {'draws': 2,\n",
    " 'games': 6,\n",
    " 'losses': 1,\n",
    " 'name': 'HataySporrr',\n",
    " 'points': 11,\n",
    " 'wins': 3},\n",
    " {'draws': 2,\n",
    " 'games': 6,\n",
    " 'losses': 1,\n",
    " 'name': 'HataySpor',\n",
    " 'points': 11,\n",
    " 'wins': 3},\n",
    " {'draws': 7,\n",
    " 'games': 8,\n",
    " 'losses': 0,\n",
    " 'name': 'Kotekli',\n",
    " 'points': 10,\n",
    " 'wins': 1}]"
    ]
    },
    "execution_count": 193,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "teams"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": []
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": []
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": []
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": []
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": []
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": []
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": []
    }
    ],
    "metadata": {
    "kernelspec": {
    "display_name": "Python 2",
    "language": "python",
    "name": "python2"
    },
    "language_info": {
    "codemirror_mode": {
    "name": "ipython",
    "version": 2
    },
    "file_extension": ".py",
    "mimetype": "text/x-python",
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython2",
    "version": "2.7.12"
    }
    },
    "nbformat": 4,
    "nbformat_minor": 2
    }
  3. pembeci revised this gist May 11, 2017. 2 changed files with 3106 additions and 335 deletions.
    1,945 changes: 1,945 additions & 0 deletions CENG2002 - Lecture 1.ipynb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,1945 @@
    {
    "cells": [
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "# Functional Programming / Paradigm"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "## Functions as first class values"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 50,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "\n",
    "\n",
    "def double(x):\n",
    " return 2*x + 1"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 51,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "25"
    ]
    },
    "execution_count": 51,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "double(12)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 52,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "function"
    ]
    },
    "execution_count": 52,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "type(double)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 53,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "int"
    ]
    },
    "execution_count": 53,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "type(5)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 54,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "5"
    ]
    },
    "execution_count": 54,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "a = 5\n",
    "b = a\n",
    "b"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 55,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "new_variable = double"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 56,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "25"
    ]
    },
    "execution_count": 56,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "new_variable(12)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 57,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def triple(x):\n",
    " return 3 * x\n",
    "\n",
    "def square(x):\n",
    " return x*x"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 58,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "my_funcs = [double, triple, square, triple]"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 59,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "<function __main__.triple>"
    ]
    },
    "execution_count": 59,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "my_funcs[1]"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 60,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "21"
    ]
    },
    "execution_count": 60,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "my_funcs[1](7)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 61,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "11\n",
    "15\n",
    "25\n",
    "15\n"
    ]
    }
    ],
    "source": [
    "for fn in my_funcs:\n",
    " print fn(5)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 62,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def cat_talk():\n",
    " print \"Meaw\""
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 63,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "cat = { \"type\": \"animal\", \"max_age\": 15, \"talk\": cat_talk}"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 64,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Meaw\n"
    ]
    }
    ],
    "source": [
    "cat[\"talk\"]()"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 65,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "100"
    ]
    },
    "execution_count": 65,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "new_variable = square\n",
    "new_variable(10)"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "## Higher Order Functions"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 66,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def apply_twice(f,x):\n",
    " return f(f(x))"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 67,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "81"
    ]
    },
    "execution_count": 67,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "apply_twice(square, 3)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 68,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "90"
    ]
    },
    "execution_count": 68,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "apply_twice(triple, 10)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 70,
    "metadata": {},
    "outputs": [],
    "source": [
    "# apply_twice(len,\"abcd\")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 71,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def add_to_string(word):\n",
    " return \"[\" + word + \"]\"\n"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 72,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "'[cat]'"
    ]
    },
    "execution_count": 72,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "add_to_string(\"cat\")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 73,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "'[[cat]]'"
    ]
    },
    "execution_count": 73,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "apply_twice(add_to_string, \"cat\")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 74,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def f(x):\n",
    " c = 5\n",
    " d = 2\n",
    " def g(y):\n",
    " return y*10\n",
    " return c+d+g(x)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 75,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "27"
    ]
    },
    "execution_count": 75,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "f(2)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 76,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def add_fn_factory(x):\n",
    " x = x*2\n",
    " def add(y):\n",
    " return x+y\n",
    " return add"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 77,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "f = add_fn_factory(7)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 78,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "24"
    ]
    },
    "execution_count": 78,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "f(10)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 79,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "34"
    ]
    },
    "execution_count": 79,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "add_fn_factory(7)(20)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 80,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "add5 = add_fn_factory(5)\n",
    "add10 = add_fn_factory(10)\n",
    "add100 = add_fn_factory(100)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 81,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "(13, 23, 203)\n"
    ]
    }
    ],
    "source": [
    "print (add5(3), add10(3), add100(3))"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "## Some common higher order functions\n",
    "\n",
    "### map\n",
    "\n",
    "`map` is used to transform one list (collection) of values into another list by applying a function to every element in a list."
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 82,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[1, 4, 9, 16, 25, 36, 49, 64, 81]"
    ]
    },
    "execution_count": 82,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "nums = [1,2,3,4,5,6,7,8,9]\n",
    "new_nums = []\n",
    "for num in nums:\n",
    " new_nums.append(square(num))\n",
    "new_nums "
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 83,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[1, 4, 9, 16, 25, 36, 49, 64, 81]"
    ]
    },
    "execution_count": 83,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "map(square,nums)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 84,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[5, 3, 2, 10]"
    ]
    },
    "execution_count": 84,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "map(len,[\"abcde\", \"def\", \"gh\", \"asdsfsdvdf\"])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 85,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "'Izzeet pembec\\xc4\\xb0'"
    ]
    },
    "execution_count": 85,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "\"izZEet PEMBECİ\".capitalize()"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 86,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "# -*- coding: utf8 -*-"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 87,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def tr_capitalize(word):\n",
    " first_letter = word[0]\n",
    " others = word[1:]\n",
    " if first_letter == \"i\":\n",
    " return \"İ\" + others.lower()\n",
    " elif first_letter == \"ı\": \n",
    " return \"I\" + others.lower()\n",
    " else:\n",
    " return first_letter.upper() + others.lower()\n",
    " "
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 88,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Ali can kayaturk\n"
    ]
    }
    ],
    "source": [
    "print tr_capitalize(\"Ali can KAYATURK\")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 89,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "'Ali Can Kayaturk'"
    ]
    },
    "execution_count": 89,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "\" \".join(map(tr_capitalize,\"Ali can KAYATURK\".split()))"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 90,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['HEL', 'WOR', 'THI', 'IS', 'AN', 'EXA']"
    ]
    },
    "execution_count": 90,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "words = \"Hello world. This is an example. \".split()\n",
    "\n",
    "def first3(word):\n",
    " return word[:3]\n",
    "\n",
    "map(first3,words)\n",
    "\n",
    "def reverse(word):\n",
    " return word[:3].upper()\n",
    "\n",
    "map(reverse,words)"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "Lambda expressions are a syntactic sugar in Python so you can define anonymous functions easily."
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 91,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['Hel', 'wor', 'Thi', 'is', 'an', 'exa']"
    ]
    },
    "execution_count": 91,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "map(lambda w: w[:3], words)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 92,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['HEL', 'WOR', 'THI', 'IS', 'AN', 'EXA']"
    ]
    },
    "execution_count": 92,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "map(lambda word: word[:3].upper(), words)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 93,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "nums1 = [3,8,9,11,45]\n",
    "nums2 = [5,4,4,28,12]\n",
    "\n",
    "# nums3 = [8,12,13,39,57]"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 94,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[15, 32, 36, 308, 540]"
    ]
    },
    "execution_count": 94,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "map(lambda a,b:a*b, nums1, nums2)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 95,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[0, 2, 0, 0, 2, 5]"
    ]
    },
    "execution_count": 95,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "map(lambda a,b: a % b, [34,56,12,30,50,40], range(2,8))"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 96,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['EEE', 'XXXX', 'AAAAA', 'MMMMMM']"
    ]
    },
    "execution_count": 96,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "map(lambda a,b: a * b, \"EXAM\", range(3,7))"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 97,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "E\n",
    "X\n",
    "A\n",
    "M\n"
    ]
    }
    ],
    "source": [
    "for c in \"EXAM\":\n",
    " print c"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 98,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['E*', 'X+', 'A-', 'M/']"
    ]
    },
    "execution_count": 98,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "map(lambda a,b: a+b, \"EXAM\", [\"*\", \"+\", \"-\", \"/\"])"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "### filter"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 110,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "nums = [1,2,3,4,42,5,6,7,8,9,11,13,24,26]\n",
    "\n",
    "def is_odd(num):\n",
    " return num%2 == 1\n",
    "\n",
    "def is_even(num):\n",
    " return num%2 == 0\n",
    "\n",
    "def is_even_and_large(num):\n",
    " return num%2 == 0 and num > 15"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 104,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[True,\n",
    " False,\n",
    " True,\n",
    " False,\n",
    " True,\n",
    " False,\n",
    " True,\n",
    " False,\n",
    " True,\n",
    " True,\n",
    " True,\n",
    " False,\n",
    " False]"
    ]
    },
    "execution_count": 104,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "map(is_odd ,nums)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 111,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[1, 3, 5, 7, 9, 11, 13]"
    ]
    },
    "execution_count": 111,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "filter(is_odd ,nums)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 112,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[2, 4, 42, 6, 8, 24, 26]"
    ]
    },
    "execution_count": 112,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "filter(is_even ,nums)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 113,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[42, 24, 26]"
    ]
    },
    "execution_count": 113,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "filter(is_even_and_large ,nums)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 114,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[11, 13]"
    ]
    },
    "execution_count": 114,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "filter(lambda n: n>=10 and n<=15 ,nums)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 115,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "students = [ \n",
    " {\"name\": \"Ali\", \"year\": 3, \"gpa\": 2.8, \"gender\": \"M\"}, \n",
    " {\"name\": \"Veli\", \"year\": 1, \"gpa\": 3.1, \"gender\": \"M\"},\n",
    " {\"name\": \"Kaya\", \"year\": 3, \"gpa\": 2.1, \"gender\": \"M\"},\n",
    " {\"name\": \"Oya\", \"year\": 2, \"gpa\": 2.5, \"gender\": \"F\"},\n",
    " {\"name\": \"Zeynep\", \"year\": 3, \"gpa\": 2.8, \"gender\": \"F\"},\n",
    " {\"name\": \"Dilek\", \"year\": 2, \"gpa\": 3.5, \"gender\": \"F\"}, \n",
    " ]"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 120,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Ali: 2.8\n",
    "Veli: 3.1\n",
    "Kaya: 2.1\n",
    "Oya: 2.5\n",
    "Zeynep: 2.8\n",
    "Dilek: 3.5\n"
    ]
    }
    ],
    "source": [
    "print \"\\n\".join(map(lambda student: student[\"name\"] + \": \" + str(student[\"gpa\"]) ,students))"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 122,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "'Hello\\nworld.\\nThis\\nis\\nan\\nexample.'"
    ]
    },
    "execution_count": 122,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "\"\\n\".join(words)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 123,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Hello\n",
    "world.\n",
    "This\n",
    "is\n",
    "an\n",
    "example.\n"
    ]
    }
    ],
    "source": [
    "print \"\\n\".join(words)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 128,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "honor_students = filter(lambda student: student[\"gpa\"]>=3.0, students)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 127,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[{'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3}]"
    ]
    },
    "execution_count": 127,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "honor_students"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 129,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Veli: 3.1\n",
    "Dilek: 3.5\n"
    ]
    }
    ],
    "source": [
    "print \"\\n\".join(map(lambda student: student[\"name\"] + \": \" + str(student[\"gpa\"]) ,honor_students))"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 130,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[1, 2, 3, 7, 8, 9]"
    ]
    },
    "execution_count": 130,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "sorted([3,7,8,2,1,9])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 131,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "values = [3,7,8,2,1,9]\n",
    "values.sort()"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 132,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[1, 2, 3, 7, 8, 9]"
    ]
    },
    "execution_count": 132,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "values"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 135,
    "metadata": {},
    "outputs": [],
    "source": [
    "words.sort()"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 136,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['Hello', 'This', 'an', 'example.', 'is', 'world.']"
    ]
    },
    "execution_count": 136,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "words"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 137,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "students.sort()"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 138,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[{'gender': 'F', 'gpa': 2.5, 'name': 'Oya', 'year': 2},\n",
    " {'gender': 'F', 'gpa': 2.8, 'name': 'Zeynep', 'year': 3},\n",
    " {'gender': 'F', 'gpa': 3.5, 'name': 'Dilek', 'year': 2},\n",
    " {'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3},\n",
    " {'gender': 'M', 'gpa': 2.8, 'name': 'Ali', 'year': 3},\n",
    " {'gender': 'M', 'gpa': 3.1, 'name': 'Veli', 'year': 1}]"
    ]
    },
    "execution_count": 138,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "students"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 139,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "students.sort(key=lambda student: student[\"gpa\"])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 141,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "students.sort(key=lambda student: student[\"gpa\"], reverse=True)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 142,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[{'gender': 'F', 'gpa': 3.5, 'name': 'Dilek', 'year': 2},\n",
    " {'gender': 'M', 'gpa': 3.1, 'name': 'Veli', 'year': 1},\n",
    " {'gender': 'F', 'gpa': 2.8, 'name': 'Zeynep', 'year': 3},\n",
    " {'gender': 'M', 'gpa': 2.8, 'name': 'Ali', 'year': 3},\n",
    " {'gender': 'F', 'gpa': 2.5, 'name': 'Oya', 'year': 2},\n",
    " {'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3}]"
    ]
    },
    "execution_count": 142,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "students"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 144,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[{'gender': 'M', 'gpa': 2.8, 'name': 'Ali', 'year': 3},\n",
    " {'gender': 'F', 'gpa': 3.5, 'name': 'Dilek', 'year': 2},\n",
    " {'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3},\n",
    " {'gender': 'F', 'gpa': 2.5, 'name': 'Oya', 'year': 2},\n",
    " {'gender': 'M', 'gpa': 3.1, 'name': 'Veli', 'year': 1},\n",
    " {'gender': 'F', 'gpa': 2.8, 'name': 'Zeynep', 'year': 3}]"
    ]
    },
    "execution_count": 144,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "sorted(students, key=lambda student: student[\"name\"])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 145,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[{'gender': 'M', 'gpa': 3.1, 'name': 'Veli', 'year': 1},\n",
    " {'gender': 'F', 'gpa': 3.5, 'name': 'Dilek', 'year': 2},\n",
    " {'gender': 'F', 'gpa': 2.5, 'name': 'Oya', 'year': 2},\n",
    " {'gender': 'M', 'gpa': 2.8, 'name': 'Ali', 'year': 3},\n",
    " {'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3},\n",
    " {'gender': 'F', 'gpa': 2.8, 'name': 'Zeynep', 'year': 3}]"
    ]
    },
    "execution_count": 145,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "sorted(students, key=lambda student: student[\"year\"])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 148,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[{'gender': 'M', 'gpa': 3.1, 'name': 'Veli', 'year': 1},\n",
    " {'gender': 'F', 'gpa': 3.5, 'name': 'Dilek', 'year': 2},\n",
    " {'gender': 'F', 'gpa': 2.5, 'name': 'Oya', 'year': 2},\n",
    " {'gender': 'M', 'gpa': 2.8, 'name': 'Ali', 'year': 3},\n",
    " {'gender': 'F', 'gpa': 2.8, 'name': 'Zeynep', 'year': 3},\n",
    " {'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3}]"
    ]
    },
    "execution_count": 148,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "sorted(students, key=lambda student: (student[\"year\"], -student[\"gpa\"]))"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "### reduce"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 152,
    "metadata": {},
    "outputs": [],
    "source": [
    "def add2(partial,next_val):\n",
    " print \"Partial=\", partial, \" Next=\", next_val\n",
    " return partial+next_val"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 153,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Partial= 7 Next= 3\n",
    "Partial= 10 Next= 5\n",
    "Partial= 15 Next= 2\n",
    "Partial= 17 Next= 8\n",
    "Partial= 25 Next= 3\n"
    ]
    },
    {
    "data": {
    "text/plain": [
    "28"
    ]
    },
    "execution_count": 153,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "reduce(add2, [7,3,5,2,8,3])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 154,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Partial= 0 Next= 7\n",
    "Partial= 7 Next= 3\n",
    "Partial= 10 Next= 5\n",
    "Partial= 15 Next= 2\n",
    "Partial= 17 Next= 8\n",
    "Partial= 25 Next= 3\n"
    ]
    },
    {
    "data": {
    "text/plain": [
    "28"
    ]
    },
    "execution_count": 154,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "reduce(add2, [7,3,5,2,8,3], 0)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 158,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "210"
    ]
    },
    "execution_count": 158,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "reduce(lambda prev,new_val: prev*new_val, [7,3,5,2])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 161,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def mult2(partial,next_val):\n",
    " result = partial*next_val\n",
    " print \"Partial=\", partial, \" Next=\", next_val, \" Result=\", result\n",
    " return result"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 162,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Partial= 7 Next= 3 Result= 21\n",
    "Partial= 21 Next= 5 Result= 105\n",
    "Partial= 105 Next= 2 Result= 210\n"
    ]
    },
    {
    "data": {
    "text/plain": [
    "210"
    ]
    },
    "execution_count": 162,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "reduce(mult2, [7,3,5,2])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 164,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def larger(partial,next_val):\n",
    " if partial>next_val:\n",
    " result = partial\n",
    " else:\n",
    " result = next_val\n",
    " print \"Partial=\", partial, \"\\t\\tNext=\", next_val, \"\\t\\tResult=\", result\n",
    " return result"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 165,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Partial= 4 \t\tNext= 7 \t\tResult= 7\n",
    "Partial= 7 \t\tNext= 5 \t\tResult= 7\n",
    "Partial= 7 \t\tNext= 9 \t\tResult= 9\n",
    "Partial= 9 \t\tNext= 3 \t\tResult= 9\n",
    "Partial= 9 \t\tNext= 9 \t\tResult= 9\n",
    "Partial= 9 \t\tNext= 5 \t\tResult= 9\n",
    "Partial= 9 \t\tNext= 11 \t\tResult= 11\n",
    "Partial= 11 \t\tNext= 8 \t\tResult= 11\n",
    "Partial= 11 \t\tNext= 15 \t\tResult= 15\n",
    "Partial= 15 \t\tNext= 3 \t\tResult= 15\n",
    "Partial= 15 \t\tNext= 10 \t\tResult= 15\n"
    ]
    },
    {
    "data": {
    "text/plain": [
    "15"
    ]
    },
    "execution_count": 165,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "reduce(larger, [4,7,5,9,3,9,5,11,8,15,3,10])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 172,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def add_if_even(partial,next_val):\n",
    " if next_val%2 == 0:\n",
    " result = partial + [next_val]\n",
    " else:\n",
    " result = partial\n",
    " print \"Partial=\", partial, \"\\tNext=\", next_val, \"\\tResult=\", result\n",
    " return result"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 173,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Partial= [] \tNext= 4 \tResult= []\n",
    "Partial= [] \tNext= 7 \tResult= [7]\n",
    "Partial= [7] \tNext= 5 \tResult= [7, 5]\n",
    "Partial= [7, 5] \tNext= 9 \tResult= [7, 5, 9]\n",
    "Partial= [7, 5, 9] \tNext= 3 \tResult= [7, 5, 9, 3]\n",
    "Partial= [7, 5, 9, 3] \tNext= 9 \tResult= [7, 5, 9, 3, 9]\n",
    "Partial= [7, 5, 9, 3, 9] \tNext= 5 \tResult= [7, 5, 9, 3, 9, 5]\n",
    "Partial= [7, 5, 9, 3, 9, 5] \tNext= 11 \tResult= [7, 5, 9, 3, 9, 5, 11]\n",
    "Partial= [7, 5, 9, 3, 9, 5, 11] \tNext= 8 \tResult= [7, 5, 9, 3, 9, 5, 11]\n",
    "Partial= [7, 5, 9, 3, 9, 5, 11] \tNext= 15 \tResult= [7, 5, 9, 3, 9, 5, 11, 15]\n",
    "Partial= [7, 5, 9, 3, 9, 5, 11, 15] \tNext= 3 \tResult= [7, 5, 9, 3, 9, 5, 11, 15, 3]\n",
    "Partial= [7, 5, 9, 3, 9, 5, 11, 15, 3] \tNext= 10 \tResult= [7, 5, 9, 3, 9, 5, 11, 15, 3]\n"
    ]
    },
    {
    "data": {
    "text/plain": [
    "[7, 5, 9, 3, 9, 5, 11, 15, 3]"
    ]
    },
    "execution_count": 173,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "reduce(add_if_even, [4,7,5,9,3,9,5,11,8,15,3,10], [])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 175,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def partition(prev_result,next_val):\n",
    " print \"Prev=\", prev_result\n",
    " if next_val%2 == 0:\n",
    " prev_result[\"even\"].append(next_val)\n",
    " else:\n",
    " prev_result[\"odd\"].append(next_val)\n",
    " print \"Next=\", next_val, \"\\tNew=\", prev_result\n",
    " return prev_result"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 176,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Prev= {'even': [], 'odd': []}\n",
    "Next= 4 \tNew= {'even': [4], 'odd': []}\n",
    "Prev= {'even': [4], 'odd': []}\n",
    "Next= 6 \tNew= {'even': [4, 6], 'odd': []}\n",
    "Prev= {'even': [4, 6], 'odd': []}\n",
    "Next= 8 \tNew= {'even': [4, 6, 8], 'odd': []}\n",
    "Prev= {'even': [4, 6, 8], 'odd': []}\n",
    "Next= 3 \tNew= {'even': [4, 6, 8], 'odd': [3]}\n",
    "Prev= {'even': [4, 6, 8], 'odd': [3]}\n",
    "Next= 5 \tNew= {'even': [4, 6, 8], 'odd': [3, 5]}\n",
    "Prev= {'even': [4, 6, 8], 'odd': [3, 5]}\n",
    "Next= 6 \tNew= {'even': [4, 6, 8, 6], 'odd': [3, 5]}\n",
    "Prev= {'even': [4, 6, 8, 6], 'odd': [3, 5]}\n",
    "Next= 7 \tNew= {'even': [4, 6, 8, 6], 'odd': [3, 5, 7]}\n",
    "Prev= {'even': [4, 6, 8, 6], 'odd': [3, 5, 7]}\n",
    "Next= 4 \tNew= {'even': [4, 6, 8, 6, 4], 'odd': [3, 5, 7]}\n",
    "Prev= {'even': [4, 6, 8, 6, 4], 'odd': [3, 5, 7]}\n",
    "Next= 11 \tNew= {'even': [4, 6, 8, 6, 4], 'odd': [3, 5, 7, 11]}\n"
    ]
    },
    {
    "data": {
    "text/plain": [
    "{'even': [4, 6, 8, 6, 4], 'odd': [3, 5, 7, 11]}"
    ]
    },
    "execution_count": 176,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "reduce(partition, [4,6,8,3,5,6,7,4,11], {\"even\":[], \"odd\": []})"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "\"\"\"\n",
    "var teams = [\n",
    " { name: \"HataySpor\", \"wins\": 3, \"draws\": 2, \"losses\": 1},\n",
    " { name: \"HataySporrr\", \"wins\": 3, \"draws\": 2, \"losses\": 1},\n",
    " { name: \"Karabük\", \"wins\": 4, \"draws\": 0, \"losses\": 1},\n",
    " { name: \"Muğlaspor\", \"wins\": 7, \"draws\": 1, \"losses\": 0},\n",
    " { name: \"UlaGücü\", \"wins\": 1, \"draws\": 3, \"losses\": 0},\n",
    " { name: \"Kötekli\", \"wins\": 1, \"draws\": 7, \"losses\": 0},\n",
    " { name: \"Dalaman\", \"wins\": 2, \"draws\": 0, \"losses\": 2}\n",
    "]\n",
    "teams.map(function(t) {\n",
    " t.points = 2*t.wins+t.draws;\n",
    " t.played=t.wins+t.draws+t.losses\n",
    "})\n",
    "teams\n",
    " .filter(function(t) { return t.played >= 5;})\n",
    " .sort(function (t1,t2) {\n",
    " if (t1.points != t2.points) return t2.points - t1.points;\n",
    " else if (t1.played != t2.played) return t1.played - t2.played;\n",
    " else return t2.name.length - t1.name.length;\n",
    " })\n",
    " .forEach(function(t) {\n",
    " t.flag = true; // will this affect teams[4] output below?\n",
    " console.log(t.name + \": \" + t.points + \"-\" + t.played);\n",
    " })\n",
    "console.log(teams[4]);\n",
    "\"\"\""
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 180,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "teams = [\n",
    " { \"name\": \"HataySpor\", \"wins\": 3, \"draws\": 2, \"losses\": 1},\n",
    " { \"name\": \"HataySporrr\", \"wins\": 3, \"draws\": 2, \"losses\": 1},\n",
    " { \"name\": \"Karabuk\", \"wins\": 4, \"draws\": 0, \"losses\": 1},\n",
    " { \"name\": \"Muglaspor\", \"wins\": 7, \"draws\": 1, \"losses\": 0},\n",
    " { \"name\": \"UlaGucu\", \"wins\": 1, \"draws\": 3, \"losses\": 0},\n",
    " { \"name\": \"Kotekli\", \"wins\": 1, \"draws\": 7, \"losses\": 0},\n",
    " { \"name\": \"Dalaman\", \"wins\": 2, \"draws\": 0, \"losses\": 2}\n",
    "]"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 196,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    " Muglaspor 22 8\n",
    " Karabuk 12 5\n",
    " HataySporrr 11 6\n",
    " HataySpor 11 6\n",
    " Kotekli 10 8\n"
    ]
    }
    ],
    "source": [
    "def add_extra_stats(team):\n",
    " team[\"points\"] = team[\"wins\"] * 3 + team[\"draws\"] * 1\n",
    " team[\"games\"] = team[\"wins\"] + team[\"draws\"] + team[\"losses\"]\n",
    " return team\n",
    "teams = map(add_extra_stats, teams)\n",
    "teams = filter(lambda t: t[\"games\"] >= 5, teams)\n",
    "teams.sort(key=lambda t: (t[\"points\"], t[\"games\"], len(t[\"name\"])), reverse=True)\n",
    "\n",
    "for team in teams:\n",
    " print \"%15s %4d %4d\" % (team[\"name\"], team[\"points\"], team[\"games\"])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 193,
    "metadata": {
    "scrolled": true
    },
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[{'draws': 1,\n",
    " 'games': 8,\n",
    " 'losses': 0,\n",
    " 'name': 'Muglaspor',\n",
    " 'points': 22,\n",
    " 'wins': 7},\n",
    " {'draws': 0,\n",
    " 'games': 5,\n",
    " 'losses': 1,\n",
    " 'name': 'Karabuk',\n",
    " 'points': 12,\n",
    " 'wins': 4},\n",
    " {'draws': 2,\n",
    " 'games': 6,\n",
    " 'losses': 1,\n",
    " 'name': 'HataySporrr',\n",
    " 'points': 11,\n",
    " 'wins': 3},\n",
    " {'draws': 2,\n",
    " 'games': 6,\n",
    " 'losses': 1,\n",
    " 'name': 'HataySpor',\n",
    " 'points': 11,\n",
    " 'wins': 3},\n",
    " {'draws': 7,\n",
    " 'games': 8,\n",
    " 'losses': 0,\n",
    " 'name': 'Kotekli',\n",
    " 'points': 10,\n",
    " 'wins': 1}]"
    ]
    },
    "execution_count": 193,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "teams"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": []
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": []
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": []
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": []
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": []
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": []
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": []
    }
    ],
    "metadata": {
    "kernelspec": {
    "display_name": "Python 2",
    "language": "python",
    "name": "python2"
    },
    "language_info": {
    "codemirror_mode": {
    "name": "ipython",
    "version": 2
    },
    "file_extension": ".py",
    "mimetype": "text/x-python",
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython2",
    "version": "2.7.12"
    }
    },
    "nbformat": 4,
    "nbformat_minor": 2
    }
    1,496 changes: 1,161 additions & 335 deletions lecture1.ipynb
    Original file line number Diff line number Diff line change
    @@ -2,54 +2,44 @@
    "cells": [
    {
    "cell_type": "markdown",
    "metadata": {
    "deletable": true,
    "editable": true
    },
    "metadata": {},
    "source": [
    "# Functional Programming / Paradigm"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "deletable": true,
    "editable": true
    },
    "metadata": {},
    "source": [
    "## Functions as first class values"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 1,
    "execution_count": 50,
    "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "\n",
    "\n",
    "def double(x):\n",
    " return 2*x + 1"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 2,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 51,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "25"
    ]
    },
    "execution_count": 2,
    "execution_count": 51,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -60,20 +50,16 @@
    },
    {
    "cell_type": "code",
    "execution_count": 3,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 52,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "function"
    ]
    },
    "execution_count": 3,
    "execution_count": 52,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -84,20 +70,16 @@
    },
    {
    "cell_type": "code",
    "execution_count": 4,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 53,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "int"
    ]
    },
    "execution_count": 4,
    "execution_count": 53,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -108,20 +90,16 @@
    },
    {
    "cell_type": "code",
    "execution_count": 5,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 54,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "5"
    ]
    },
    "execution_count": 5,
    "execution_count": 54,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -134,11 +112,9 @@
    },
    {
    "cell_type": "code",
    "execution_count": 6,
    "execution_count": 55,
    "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
    "collapsed": true
    },
    "outputs": [],
    "source": [
    @@ -147,20 +123,16 @@
    },
    {
    "cell_type": "code",
    "execution_count": 7,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 56,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "25"
    ]
    },
    "execution_count": 7,
    "execution_count": 56,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -171,11 +143,9 @@
    },
    {
    "cell_type": "code",
    "execution_count": 8,
    "execution_count": 57,
    "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
    "collapsed": true
    },
    "outputs": [],
    "source": [
    @@ -188,11 +158,9 @@
    },
    {
    "cell_type": "code",
    "execution_count": 9,
    "execution_count": 58,
    "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
    "collapsed": true
    },
    "outputs": [],
    "source": [
    @@ -201,20 +169,16 @@
    },
    {
    "cell_type": "code",
    "execution_count": 10,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 59,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "<function __main__.triple>"
    ]
    },
    "execution_count": 10,
    "execution_count": 59,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -225,20 +189,16 @@
    },
    {
    "cell_type": "code",
    "execution_count": 11,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 60,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "21"
    ]
    },
    "execution_count": 11,
    "execution_count": 60,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -249,12 +209,8 @@
    },
    {
    "cell_type": "code",
    "execution_count": 12,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 61,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    @@ -269,16 +225,14 @@
    ],
    "source": [
    "for fn in my_funcs:\n",
    " print(fn(5))"
    " print fn(5)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 13,
    "execution_count": 62,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    "collapsed": true
    },
    "outputs": [],
    "source": [
    @@ -288,11 +242,9 @@
    },
    {
    "cell_type": "code",
    "execution_count": 14,
    "execution_count": 63,
    "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
    "collapsed": true
    },
    "outputs": [],
    "source": [
    @@ -301,12 +253,8 @@
    },
    {
    "cell_type": "code",
    "execution_count": 15,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 64,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    @@ -322,20 +270,16 @@
    },
    {
    "cell_type": "code",
    "execution_count": 16,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 65,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "100"
    ]
    },
    "execution_count": 16,
    "execution_count": 65,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -347,21 +291,16 @@
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "deletable": true,
    "editable": true
    },
    "metadata": {},
    "source": [
    "## Higher Order Functions"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 17,
    "execution_count": 66,
    "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
    "collapsed": true
    },
    "outputs": [],
    "source": [
    @@ -371,20 +310,16 @@
    },
    {
    "cell_type": "code",
    "execution_count": 18,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 67,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "81"
    ]
    },
    "execution_count": 18,
    "execution_count": 67,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -395,20 +330,16 @@
    },
    {
    "cell_type": "code",
    "execution_count": 19,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 68,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "90"
    ]
    },
    "execution_count": 19,
    "execution_count": 68,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -419,37 +350,18 @@
    },
    {
    "cell_type": "code",
    "execution_count": 20,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "ename": "TypeError",
    "evalue": "object of type 'int' has no len()",
    "output_type": "error",
    "traceback": [
    "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
    "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
    "\u001b[0;32m<ipython-input-20-6258c08cf641>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mapply_twice\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"abcd\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
    "\u001b[0;32m<ipython-input-17-362271ea4f05>\u001b[0m in \u001b[0;36mapply_twice\u001b[0;34m(f, x)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mapply_twice\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
    "\u001b[0;31mTypeError\u001b[0m: object of type 'int' has no len()"
    ]
    }
    ],
    "execution_count": 70,
    "metadata": {},
    "outputs": [],
    "source": [
    "apply_twice(len,\"abcd\")"
    "# apply_twice(len,\"abcd\")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 21,
    "execution_count": 71,
    "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
    "collapsed": true
    },
    "outputs": [],
    "source": [
    @@ -459,20 +371,16 @@
    },
    {
    "cell_type": "code",
    "execution_count": 22,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 72,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "'[cat]'"
    ]
    },
    "execution_count": 22,
    "execution_count": 72,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -483,20 +391,16 @@
    },
    {
    "cell_type": "code",
    "execution_count": 23,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 73,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "'[[cat]]'"
    ]
    },
    "execution_count": 23,
    "execution_count": 73,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -507,11 +411,9 @@
    },
    {
    "cell_type": "code",
    "execution_count": 24,
    "execution_count": 74,
    "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
    "collapsed": true
    },
    "outputs": [],
    "source": [
    @@ -525,20 +427,16 @@
    },
    {
    "cell_type": "code",
    "execution_count": 25,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 75,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "27"
    ]
    },
    "execution_count": 25,
    "execution_count": 75,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -549,11 +447,9 @@
    },
    {
    "cell_type": "code",
    "execution_count": 26,
    "execution_count": 76,
    "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
    "collapsed": true
    },
    "outputs": [],
    "source": [
    @@ -566,11 +462,9 @@
    },
    {
    "cell_type": "code",
    "execution_count": 27,
    "execution_count": 77,
    "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
    "collapsed": true
    },
    "outputs": [],
    "source": [
    @@ -579,20 +473,16 @@
    },
    {
    "cell_type": "code",
    "execution_count": 28,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 78,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "24"
    ]
    },
    "execution_count": 28,
    "execution_count": 78,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -603,20 +493,16 @@
    },
    {
    "cell_type": "code",
    "execution_count": 29,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 79,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "34"
    ]
    },
    "execution_count": 29,
    "execution_count": 79,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -627,11 +513,9 @@
    },
    {
    "cell_type": "code",
    "execution_count": 30,
    "execution_count": 80,
    "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
    "collapsed": true
    },
    "outputs": [],
    "source": [
    @@ -642,12 +526,8 @@
    },
    {
    "cell_type": "code",
    "execution_count": 31,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 81,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    @@ -663,10 +543,7 @@
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "deletable": true,
    "editable": true
    },
    "metadata": {},
    "source": [
    "## Some common higher order functions\n",
    "\n",
    @@ -677,20 +554,16 @@
    },
    {
    "cell_type": "code",
    "execution_count": 32,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 82,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[1, 4, 9, 16, 25, 36, 49, 64, 81]"
    ]
    },
    "execution_count": 32,
    "execution_count": 82,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -705,20 +578,16 @@
    },
    {
    "cell_type": "code",
    "execution_count": 33,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 83,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[1, 4, 9, 16, 25, 36, 49, 64, 81]"
    ]
    },
    "execution_count": 33,
    "execution_count": 83,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -729,20 +598,16 @@
    },
    {
    "cell_type": "code",
    "execution_count": 34,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 84,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[5, 3, 2, 10]"
    ]
    },
    "execution_count": 34,
    "execution_count": 84,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -753,20 +618,16 @@
    },
    {
    "cell_type": "code",
    "execution_count": 35,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 85,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "'Izzeet pembec\\xc4\\xb0'"
    ]
    },
    "execution_count": 35,
    "execution_count": 85,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -777,11 +638,9 @@
    },
    {
    "cell_type": "code",
    "execution_count": 36,
    "execution_count": 86,
    "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
    "collapsed": true
    },
    "outputs": [],
    "source": [
    @@ -790,11 +649,9 @@
    },
    {
    "cell_type": "code",
    "execution_count": 37,
    "execution_count": 87,
    "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
    "collapsed": true
    },
    "outputs": [],
    "source": [
    @@ -803,7 +660,7 @@
    " others = word[1:]\n",
    " if first_letter == \"i\":\n",
    " return \"İ\" + others.lower()\n",
    " elif first_letter == u\"ı\": \n",
    " elif first_letter == \"ı\": \n",
    " return \"I\" + others.lower()\n",
    " else:\n",
    " return first_letter.upper() + others.lower()\n",
    @@ -812,44 +669,33 @@
    },
    {
    "cell_type": "code",
    "execution_count": 38,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 88,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "u'Ali can kayaturk \\u0131\\u015f\\u0131k'"
    ]
    },
    "execution_count": 38,
    "metadata": {},
    "output_type": "execute_result"
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Ali can kayaturk\n"
    ]
    }
    ],
    "source": [
    "tr_capitalize(u\"Ali can KAYATURK ışık\")"
    "print tr_capitalize(\"Ali can KAYATURK\")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 39,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 89,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "'Ali Can Kayaturk'"
    ]
    },
    "execution_count": 39,
    "execution_count": 89,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -860,20 +706,16 @@
    },
    {
    "cell_type": "code",
    "execution_count": 40,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 90,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['HEL', 'WOR', 'THI', 'IS', 'AN', 'EXA']"
    ]
    },
    "execution_count": 40,
    "execution_count": 90,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -894,30 +736,23 @@
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "deletable": true,
    "editable": true
    },
    "metadata": {},
    "source": [
    "Lambda expressions are a syntactic sugar in Python so you can define anonymous functions easily."
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 41,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 91,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['Hel', 'wor', 'Thi', 'is', 'an', 'exa']"
    ]
    },
    "execution_count": 41,
    "execution_count": 91,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -928,20 +763,16 @@
    },
    {
    "cell_type": "code",
    "execution_count": 42,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 92,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['HEL', 'WOR', 'THI', 'IS', 'AN', 'EXA']"
    ]
    },
    "execution_count": 42,
    "execution_count": 92,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -952,11 +783,9 @@
    },
    {
    "cell_type": "code",
    "execution_count": 43,
    "execution_count": 93,
    "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
    "collapsed": true
    },
    "outputs": [],
    "source": [
    @@ -968,20 +797,16 @@
    },
    {
    "cell_type": "code",
    "execution_count": 44,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 94,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[15, 32, 36, 308, 540]"
    ]
    },
    "execution_count": 44,
    "execution_count": 94,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -992,20 +817,16 @@
    },
    {
    "cell_type": "code",
    "execution_count": 45,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 95,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[0, 2, 0, 0, 2, 5]"
    ]
    },
    "execution_count": 45,
    "execution_count": 95,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -1016,20 +837,16 @@
    },
    {
    "cell_type": "code",
    "execution_count": 46,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 96,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['EEE', 'XXXX', 'AAAAA', 'MMMMMM']"
    ]
    },
    "execution_count": 46,
    "execution_count": 96,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -1040,12 +857,8 @@
    },
    {
    "cell_type": "code",
    "execution_count": 47,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "execution_count": 97,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    @@ -1063,33 +876,1046 @@
    " print c"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 98,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['E*', 'X+', 'A-', 'M/']"
    ]
    },
    "execution_count": 98,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "map(lambda a,b: a+b, \"EXAM\", [\"*\", \"+\", \"-\", \"/\"])"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "### filter"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 110,
    "metadata": {
    "deletable": true,
    "editable": true
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "### filter"
    "nums = [1,2,3,4,42,5,6,7,8,9,11,13,24,26]\n",
    "\n",
    "def is_odd(num):\n",
    " return num%2 == 1\n",
    "\n",
    "def is_even(num):\n",
    " return num%2 == 0\n",
    "\n",
    "def is_even_and_large(num):\n",
    " return num%2 == 0 and num > 15"
    ]
    },
    {
    "cell_type": "markdown",
    "cell_type": "code",
    "execution_count": 104,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[True,\n",
    " False,\n",
    " True,\n",
    " False,\n",
    " True,\n",
    " False,\n",
    " True,\n",
    " False,\n",
    " True,\n",
    " True,\n",
    " True,\n",
    " False,\n",
    " False]"
    ]
    },
    "execution_count": 104,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "map(is_odd ,nums)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 111,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[1, 3, 5, 7, 9, 11, 13]"
    ]
    },
    "execution_count": 111,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "filter(is_odd ,nums)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 112,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[2, 4, 42, 6, 8, 24, 26]"
    ]
    },
    "execution_count": 112,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "filter(is_even ,nums)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 113,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[42, 24, 26]"
    ]
    },
    "execution_count": 113,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "filter(is_even_and_large ,nums)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 114,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[11, 13]"
    ]
    },
    "execution_count": 114,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "filter(lambda n: n>=10 and n<=15 ,nums)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 115,
    "metadata": {
    "deletable": true,
    "editable": true
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "### reduce"
    "students = [ \n",
    " {\"name\": \"Ali\", \"year\": 3, \"gpa\": 2.8, \"gender\": \"M\"}, \n",
    " {\"name\": \"Veli\", \"year\": 1, \"gpa\": 3.1, \"gender\": \"M\"},\n",
    " {\"name\": \"Kaya\", \"year\": 3, \"gpa\": 2.1, \"gender\": \"M\"},\n",
    " {\"name\": \"Oya\", \"year\": 2, \"gpa\": 2.5, \"gender\": \"F\"},\n",
    " {\"name\": \"Zeynep\", \"year\": 3, \"gpa\": 2.8, \"gender\": \"F\"},\n",
    " {\"name\": \"Dilek\", \"year\": 2, \"gpa\": 3.5, \"gender\": \"F\"}, \n",
    " ]"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 120,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Ali: 2.8\n",
    "Veli: 3.1\n",
    "Kaya: 2.1\n",
    "Oya: 2.5\n",
    "Zeynep: 2.8\n",
    "Dilek: 3.5\n"
    ]
    }
    ],
    "source": [
    "print \"\\n\".join(map(lambda student: student[\"name\"] + \": \" + str(student[\"gpa\"]) ,students))"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 122,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "'Hello\\nworld.\\nThis\\nis\\nan\\nexample.'"
    ]
    },
    "execution_count": 122,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "\"\\n\".join(words)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 123,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Hello\n",
    "world.\n",
    "This\n",
    "is\n",
    "an\n",
    "example.\n"
    ]
    }
    ],
    "source": [
    "print \"\\n\".join(words)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 128,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "honor_students = filter(lambda student: student[\"gpa\"]>=3.0, students)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 127,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[{'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3}]"
    ]
    },
    "execution_count": 127,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "honor_students"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 129,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Veli: 3.1\n",
    "Dilek: 3.5\n"
    ]
    }
    ],
    "source": [
    "print \"\\n\".join(map(lambda student: student[\"name\"] + \": \" + str(student[\"gpa\"]) ,honor_students))"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 130,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[1, 2, 3, 7, 8, 9]"
    ]
    },
    "execution_count": 130,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "sorted([3,7,8,2,1,9])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 131,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "values = [3,7,8,2,1,9]\n",
    "values.sort()"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 132,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[1, 2, 3, 7, 8, 9]"
    ]
    },
    "execution_count": 132,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "values"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 135,
    "metadata": {},
    "outputs": [],
    "source": [
    "words.sort()"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 136,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['Hello', 'This', 'an', 'example.', 'is', 'world.']"
    ]
    },
    "execution_count": 136,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "words"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 137,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "students.sort()"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 138,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[{'gender': 'F', 'gpa': 2.5, 'name': 'Oya', 'year': 2},\n",
    " {'gender': 'F', 'gpa': 2.8, 'name': 'Zeynep', 'year': 3},\n",
    " {'gender': 'F', 'gpa': 3.5, 'name': 'Dilek', 'year': 2},\n",
    " {'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3},\n",
    " {'gender': 'M', 'gpa': 2.8, 'name': 'Ali', 'year': 3},\n",
    " {'gender': 'M', 'gpa': 3.1, 'name': 'Veli', 'year': 1}]"
    ]
    },
    "execution_count": 138,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "students"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 139,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "students.sort(key=lambda student: student[\"gpa\"])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 141,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "students.sort(key=lambda student: student[\"gpa\"], reverse=True)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 142,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[{'gender': 'F', 'gpa': 3.5, 'name': 'Dilek', 'year': 2},\n",
    " {'gender': 'M', 'gpa': 3.1, 'name': 'Veli', 'year': 1},\n",
    " {'gender': 'F', 'gpa': 2.8, 'name': 'Zeynep', 'year': 3},\n",
    " {'gender': 'M', 'gpa': 2.8, 'name': 'Ali', 'year': 3},\n",
    " {'gender': 'F', 'gpa': 2.5, 'name': 'Oya', 'year': 2},\n",
    " {'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3}]"
    ]
    },
    "execution_count": 142,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "students"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 144,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[{'gender': 'M', 'gpa': 2.8, 'name': 'Ali', 'year': 3},\n",
    " {'gender': 'F', 'gpa': 3.5, 'name': 'Dilek', 'year': 2},\n",
    " {'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3},\n",
    " {'gender': 'F', 'gpa': 2.5, 'name': 'Oya', 'year': 2},\n",
    " {'gender': 'M', 'gpa': 3.1, 'name': 'Veli', 'year': 1},\n",
    " {'gender': 'F', 'gpa': 2.8, 'name': 'Zeynep', 'year': 3}]"
    ]
    },
    "execution_count": 144,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "sorted(students, key=lambda student: student[\"name\"])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 145,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[{'gender': 'M', 'gpa': 3.1, 'name': 'Veli', 'year': 1},\n",
    " {'gender': 'F', 'gpa': 3.5, 'name': 'Dilek', 'year': 2},\n",
    " {'gender': 'F', 'gpa': 2.5, 'name': 'Oya', 'year': 2},\n",
    " {'gender': 'M', 'gpa': 2.8, 'name': 'Ali', 'year': 3},\n",
    " {'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3},\n",
    " {'gender': 'F', 'gpa': 2.8, 'name': 'Zeynep', 'year': 3}]"
    ]
    },
    "execution_count": 145,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "sorted(students, key=lambda student: student[\"year\"])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 148,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[{'gender': 'M', 'gpa': 3.1, 'name': 'Veli', 'year': 1},\n",
    " {'gender': 'F', 'gpa': 3.5, 'name': 'Dilek', 'year': 2},\n",
    " {'gender': 'F', 'gpa': 2.5, 'name': 'Oya', 'year': 2},\n",
    " {'gender': 'M', 'gpa': 2.8, 'name': 'Ali', 'year': 3},\n",
    " {'gender': 'F', 'gpa': 2.8, 'name': 'Zeynep', 'year': 3},\n",
    " {'gender': 'M', 'gpa': 2.1, 'name': 'Kaya', 'year': 3}]"
    ]
    },
    "execution_count": 148,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "sorted(students, key=lambda student: (student[\"year\"], -student[\"gpa\"]))"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "### reduce"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 152,
    "metadata": {},
    "outputs": [],
    "source": [
    "def add2(partial,next_val):\n",
    " print \"Partial=\", partial, \" Next=\", next_val\n",
    " return partial+next_val"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 153,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Partial= 7 Next= 3\n",
    "Partial= 10 Next= 5\n",
    "Partial= 15 Next= 2\n",
    "Partial= 17 Next= 8\n",
    "Partial= 25 Next= 3\n"
    ]
    },
    {
    "data": {
    "text/plain": [
    "28"
    ]
    },
    "execution_count": 153,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "reduce(add2, [7,3,5,2,8,3])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 154,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Partial= 0 Next= 7\n",
    "Partial= 7 Next= 3\n",
    "Partial= 10 Next= 5\n",
    "Partial= 15 Next= 2\n",
    "Partial= 17 Next= 8\n",
    "Partial= 25 Next= 3\n"
    ]
    },
    {
    "data": {
    "text/plain": [
    "28"
    ]
    },
    "execution_count": 154,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "reduce(add2, [7,3,5,2,8,3], 0)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 158,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "210"
    ]
    },
    "execution_count": 158,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "reduce(lambda prev,new_val: prev*new_val, [7,3,5,2])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 161,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def mult2(partial,next_val):\n",
    " result = partial*next_val\n",
    " print \"Partial=\", partial, \" Next=\", next_val, \" Result=\", result\n",
    " return result"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 162,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Partial= 7 Next= 3 Result= 21\n",
    "Partial= 21 Next= 5 Result= 105\n",
    "Partial= 105 Next= 2 Result= 210\n"
    ]
    },
    {
    "data": {
    "text/plain": [
    "210"
    ]
    },
    "execution_count": 162,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "reduce(mult2, [7,3,5,2])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 164,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def larger(partial,next_val):\n",
    " if partial>next_val:\n",
    " result = partial\n",
    " else:\n",
    " result = next_val\n",
    " print \"Partial=\", partial, \"\\t\\tNext=\", next_val, \"\\t\\tResult=\", result\n",
    " return result"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 165,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Partial= 4 \t\tNext= 7 \t\tResult= 7\n",
    "Partial= 7 \t\tNext= 5 \t\tResult= 7\n",
    "Partial= 7 \t\tNext= 9 \t\tResult= 9\n",
    "Partial= 9 \t\tNext= 3 \t\tResult= 9\n",
    "Partial= 9 \t\tNext= 9 \t\tResult= 9\n",
    "Partial= 9 \t\tNext= 5 \t\tResult= 9\n",
    "Partial= 9 \t\tNext= 11 \t\tResult= 11\n",
    "Partial= 11 \t\tNext= 8 \t\tResult= 11\n",
    "Partial= 11 \t\tNext= 15 \t\tResult= 15\n",
    "Partial= 15 \t\tNext= 3 \t\tResult= 15\n",
    "Partial= 15 \t\tNext= 10 \t\tResult= 15\n"
    ]
    },
    {
    "data": {
    "text/plain": [
    "15"
    ]
    },
    "execution_count": 165,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "reduce(larger, [4,7,5,9,3,9,5,11,8,15,3,10])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 172,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def add_if_even(partial,next_val):\n",
    " if next_val%2 == 0:\n",
    " result = partial + [next_val]\n",
    " else:\n",
    " result = partial\n",
    " print \"Partial=\", partial, \"\\tNext=\", next_val, \"\\tResult=\", result\n",
    " return result"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 173,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Partial= [] \tNext= 4 \tResult= []\n",
    "Partial= [] \tNext= 7 \tResult= [7]\n",
    "Partial= [7] \tNext= 5 \tResult= [7, 5]\n",
    "Partial= [7, 5] \tNext= 9 \tResult= [7, 5, 9]\n",
    "Partial= [7, 5, 9] \tNext= 3 \tResult= [7, 5, 9, 3]\n",
    "Partial= [7, 5, 9, 3] \tNext= 9 \tResult= [7, 5, 9, 3, 9]\n",
    "Partial= [7, 5, 9, 3, 9] \tNext= 5 \tResult= [7, 5, 9, 3, 9, 5]\n",
    "Partial= [7, 5, 9, 3, 9, 5] \tNext= 11 \tResult= [7, 5, 9, 3, 9, 5, 11]\n",
    "Partial= [7, 5, 9, 3, 9, 5, 11] \tNext= 8 \tResult= [7, 5, 9, 3, 9, 5, 11]\n",
    "Partial= [7, 5, 9, 3, 9, 5, 11] \tNext= 15 \tResult= [7, 5, 9, 3, 9, 5, 11, 15]\n",
    "Partial= [7, 5, 9, 3, 9, 5, 11, 15] \tNext= 3 \tResult= [7, 5, 9, 3, 9, 5, 11, 15, 3]\n",
    "Partial= [7, 5, 9, 3, 9, 5, 11, 15, 3] \tNext= 10 \tResult= [7, 5, 9, 3, 9, 5, 11, 15, 3]\n"
    ]
    },
    {
    "data": {
    "text/plain": [
    "[7, 5, 9, 3, 9, 5, 11, 15, 3]"
    ]
    },
    "execution_count": 173,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "reduce(add_if_even, [4,7,5,9,3,9,5,11,8,15,3,10], [])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 175,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def partition(prev_result,next_val):\n",
    " print \"Prev=\", prev_result\n",
    " if next_val%2 == 0:\n",
    " prev_result[\"even\"].append(next_val)\n",
    " else:\n",
    " prev_result[\"odd\"].append(next_val)\n",
    " print \"Next=\", next_val, \"\\tNew=\", prev_result\n",
    " return prev_result"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 176,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Prev= {'even': [], 'odd': []}\n",
    "Next= 4 \tNew= {'even': [4], 'odd': []}\n",
    "Prev= {'even': [4], 'odd': []}\n",
    "Next= 6 \tNew= {'even': [4, 6], 'odd': []}\n",
    "Prev= {'even': [4, 6], 'odd': []}\n",
    "Next= 8 \tNew= {'even': [4, 6, 8], 'odd': []}\n",
    "Prev= {'even': [4, 6, 8], 'odd': []}\n",
    "Next= 3 \tNew= {'even': [4, 6, 8], 'odd': [3]}\n",
    "Prev= {'even': [4, 6, 8], 'odd': [3]}\n",
    "Next= 5 \tNew= {'even': [4, 6, 8], 'odd': [3, 5]}\n",
    "Prev= {'even': [4, 6, 8], 'odd': [3, 5]}\n",
    "Next= 6 \tNew= {'even': [4, 6, 8, 6], 'odd': [3, 5]}\n",
    "Prev= {'even': [4, 6, 8, 6], 'odd': [3, 5]}\n",
    "Next= 7 \tNew= {'even': [4, 6, 8, 6], 'odd': [3, 5, 7]}\n",
    "Prev= {'even': [4, 6, 8, 6], 'odd': [3, 5, 7]}\n",
    "Next= 4 \tNew= {'even': [4, 6, 8, 6, 4], 'odd': [3, 5, 7]}\n",
    "Prev= {'even': [4, 6, 8, 6, 4], 'odd': [3, 5, 7]}\n",
    "Next= 11 \tNew= {'even': [4, 6, 8, 6, 4], 'odd': [3, 5, 7, 11]}\n"
    ]
    },
    {
    "data": {
    "text/plain": [
    "{'even': [4, 6, 8, 6, 4], 'odd': [3, 5, 7, 11]}"
    ]
    },
    "execution_count": 176,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "reduce(partition, [4,6,8,3,5,6,7,4,11], {\"even\":[], \"odd\": []})"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "\"\"\"\n",
    "var teams = [\n",
    " { name: \"HataySpor\", \"wins\": 3, \"draws\": 2, \"losses\": 1},\n",
    " { name: \"HataySporrr\", \"wins\": 3, \"draws\": 2, \"losses\": 1},\n",
    " { name: \"Karabük\", \"wins\": 4, \"draws\": 0, \"losses\": 1},\n",
    " { name: \"Muğlaspor\", \"wins\": 7, \"draws\": 1, \"losses\": 0},\n",
    " { name: \"UlaGücü\", \"wins\": 1, \"draws\": 3, \"losses\": 0},\n",
    " { name: \"Kötekli\", \"wins\": 1, \"draws\": 7, \"losses\": 0},\n",
    " { name: \"Dalaman\", \"wins\": 2, \"draws\": 0, \"losses\": 2}\n",
    "]\n",
    "teams.map(function(t) {\n",
    " t.points = 2*t.wins+t.draws;\n",
    " t.played=t.wins+t.draws+t.losses\n",
    "})\n",
    "teams\n",
    " .filter(function(t) { return t.played >= 5;})\n",
    " .sort(function (t1,t2) {\n",
    " if (t1.points != t2.points) return t2.points - t1.points;\n",
    " else if (t1.played != t2.played) return t1.played - t2.played;\n",
    " else return t2.name.length - t1.name.length;\n",
    " })\n",
    " .forEach(function(t) {\n",
    " t.flag = true; // will this affect teams[4] output below?\n",
    " console.log(t.name + \": \" + t.points + \"-\" + t.played);\n",
    " })\n",
    "console.log(teams[4]);\n",
    "\"\"\""
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 180,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "teams = [\n",
    " { \"name\": \"HataySpor\", \"wins\": 3, \"draws\": 2, \"losses\": 1},\n",
    " { \"name\": \"HataySporrr\", \"wins\": 3, \"draws\": 2, \"losses\": 1},\n",
    " { \"name\": \"Karabuk\", \"wins\": 4, \"draws\": 0, \"losses\": 1},\n",
    " { \"name\": \"Muglaspor\", \"wins\": 7, \"draws\": 1, \"losses\": 0},\n",
    " { \"name\": \"UlaGucu\", \"wins\": 1, \"draws\": 3, \"losses\": 0},\n",
    " { \"name\": \"Kotekli\", \"wins\": 1, \"draws\": 7, \"losses\": 0},\n",
    " { \"name\": \"Dalaman\", \"wins\": 2, \"draws\": 0, \"losses\": 2}\n",
    "]"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 196,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    " Muglaspor 22 8\n",
    " Karabuk 12 5\n",
    " HataySporrr 11 6\n",
    " HataySpor 11 6\n",
    " Kotekli 10 8\n"
    ]
    }
    ],
    "source": [
    "def add_extra_stats(team):\n",
    " team[\"points\"] = team[\"wins\"] * 3 + team[\"draws\"] * 1\n",
    " team[\"games\"] = team[\"wins\"] + team[\"draws\"] + team[\"losses\"]\n",
    " return team\n",
    "teams = map(add_extra_stats, teams)\n",
    "teams = filter(lambda t: t[\"games\"] >= 5, teams)\n",
    "teams.sort(key=lambda t: (t[\"points\"], t[\"games\"], len(t[\"name\"])), reverse=True)\n",
    "\n",
    "for team in teams:\n",
    " print \"%15s %4d %4d\" % (team[\"name\"], team[\"points\"], team[\"games\"])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 193,
    "metadata": {
    "scrolled": true
    },
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[{'draws': 1,\n",
    " 'games': 8,\n",
    " 'losses': 0,\n",
    " 'name': 'Muglaspor',\n",
    " 'points': 22,\n",
    " 'wins': 7},\n",
    " {'draws': 0,\n",
    " 'games': 5,\n",
    " 'losses': 1,\n",
    " 'name': 'Karabuk',\n",
    " 'points': 12,\n",
    " 'wins': 4},\n",
    " {'draws': 2,\n",
    " 'games': 6,\n",
    " 'losses': 1,\n",
    " 'name': 'HataySporrr',\n",
    " 'points': 11,\n",
    " 'wins': 3},\n",
    " {'draws': 2,\n",
    " 'games': 6,\n",
    " 'losses': 1,\n",
    " 'name': 'HataySpor',\n",
    " 'points': 11,\n",
    " 'wins': 3},\n",
    " {'draws': 7,\n",
    " 'games': 8,\n",
    " 'losses': 0,\n",
    " 'name': 'Kotekli',\n",
    " 'points': 10,\n",
    " 'wins': 1}]"
    ]
    },
    "execution_count": 193,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "teams"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": []
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": []
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": []
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": []
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": []
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": []
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
    "collapsed": true
    },
    "outputs": [],
    "source": []
    @@ -1111,7 +1937,7 @@
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython2",
    "version": "2.7.13"
    "version": "2.7.12"
    }
    },
    "nbformat": 4,
  4. pembeci revised this gist May 4, 2017. 2 changed files with 399 additions and 110 deletions.
    Empty file removed league.json
    Empty file.
    509 changes: 399 additions & 110 deletions lecture1.ipynb
    Original file line number Diff line number Diff line change
    @@ -2,14 +2,20 @@
    "cells": [
    {
    "cell_type": "markdown",
    "metadata": {},
    "metadata": {
    "deletable": true,
    "editable": true
    },
    "source": [
    "# Functional Programming / Paradigm"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "metadata": {
    "deletable": true,
    "editable": true
    },
    "source": [
    "## Functions as first class values"
    ]
    @@ -18,20 +24,24 @@
    "cell_type": "code",
    "execution_count": 1,
    "metadata": {
    "collapsed": true
    "collapsed": true,
    "deletable": true,
    "editable": true
    },
    "outputs": [],
    "source": [
    "\n",
    "\n",
    "def double(x):\n",
    " return 2*x + 1"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 2,
    "metadata": {},
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "data": {
    @@ -51,7 +61,11 @@
    {
    "cell_type": "code",
    "execution_count": 3,
    "metadata": {},
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "data": {
    @@ -71,7 +85,11 @@
    {
    "cell_type": "code",
    "execution_count": 4,
    "metadata": {},
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "data": {
    @@ -91,7 +109,11 @@
    {
    "cell_type": "code",
    "execution_count": 5,
    "metadata": {},
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "data": {
    @@ -114,7 +136,9 @@
    "cell_type": "code",
    "execution_count": 6,
    "metadata": {
    "collapsed": true
    "collapsed": true,
    "deletable": true,
    "editable": true
    },
    "outputs": [],
    "source": [
    @@ -124,7 +148,11 @@
    {
    "cell_type": "code",
    "execution_count": 7,
    "metadata": {},
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "data": {
    @@ -145,7 +173,9 @@
    "cell_type": "code",
    "execution_count": 8,
    "metadata": {
    "collapsed": true
    "collapsed": true,
    "deletable": true,
    "editable": true
    },
    "outputs": [],
    "source": [
    @@ -160,7 +190,9 @@
    "cell_type": "code",
    "execution_count": 9,
    "metadata": {
    "collapsed": true
    "collapsed": true,
    "deletable": true,
    "editable": true
    },
    "outputs": [],
    "source": [
    @@ -170,7 +202,11 @@
    {
    "cell_type": "code",
    "execution_count": 10,
    "metadata": {},
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "data": {
    @@ -190,7 +226,11 @@
    {
    "cell_type": "code",
    "execution_count": 11,
    "metadata": {},
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "data": {
    @@ -210,7 +250,11 @@
    {
    "cell_type": "code",
    "execution_count": 12,
    "metadata": {},
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "name": "stdout",
    @@ -225,14 +269,16 @@
    ],
    "source": [
    "for fn in my_funcs:\n",
    " print fn(5)"
    " print(fn(5))"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 13,
    "metadata": {
    "collapsed": true
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [],
    "source": [
    @@ -244,7 +290,9 @@
    "cell_type": "code",
    "execution_count": 14,
    "metadata": {
    "collapsed": true
    "collapsed": true,
    "deletable": true,
    "editable": true
    },
    "outputs": [],
    "source": [
    @@ -254,7 +302,11 @@
    {
    "cell_type": "code",
    "execution_count": 15,
    "metadata": {},
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "name": "stdout",
    @@ -271,7 +323,11 @@
    {
    "cell_type": "code",
    "execution_count": 16,
    "metadata": {},
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "data": {
    @@ -291,7 +347,10 @@
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "metadata": {
    "deletable": true,
    "editable": true
    },
    "source": [
    "## Higher Order Functions"
    ]
    @@ -300,7 +359,9 @@
    "cell_type": "code",
    "execution_count": 17,
    "metadata": {
    "collapsed": true
    "collapsed": true,
    "deletable": true,
    "editable": true
    },
    "outputs": [],
    "source": [
    @@ -311,7 +372,11 @@
    {
    "cell_type": "code",
    "execution_count": 18,
    "metadata": {},
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "data": {
    @@ -331,7 +396,11 @@
    {
    "cell_type": "code",
    "execution_count": 19,
    "metadata": {},
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "data": {
    @@ -351,7 +420,11 @@
    {
    "cell_type": "code",
    "execution_count": 20,
    "metadata": {},
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "ename": "TypeError",
    @@ -372,9 +445,11 @@
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "execution_count": 21,
    "metadata": {
    "collapsed": true
    "collapsed": true,
    "deletable": true,
    "editable": true
    },
    "outputs": [],
    "source": [
    @@ -384,27 +459,59 @@
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "execution_count": 22,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "data": {
    "text/plain": [
    "'[cat]'"
    ]
    },
    "execution_count": 22,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "add_to_string(\"cat\")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "execution_count": 23,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "data": {
    "text/plain": [
    "'[[cat]]'"
    ]
    },
    "execution_count": 23,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "apply_twice(add_to_string, \"cat\")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "execution_count": 24,
    "metadata": {
    "collapsed": true
    "collapsed": true,
    "deletable": true,
    "editable": true
    },
    "outputs": [],
    "source": [
    @@ -418,18 +525,35 @@
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "execution_count": 25,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "data": {
    "text/plain": [
    "27"
    ]
    },
    "execution_count": 25,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "f(2)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "execution_count": 26,
    "metadata": {
    "collapsed": true
    "collapsed": true,
    "deletable": true,
    "editable": true
    },
    "outputs": [],
    "source": [
    @@ -442,9 +566,11 @@
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "execution_count": 27,
    "metadata": {
    "collapsed": true
    "collapsed": true,
    "deletable": true,
    "editable": true
    },
    "outputs": [],
    "source": [
    @@ -453,27 +579,59 @@
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "execution_count": 28,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "data": {
    "text/plain": [
    "24"
    ]
    },
    "execution_count": 28,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "f(10)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "execution_count": 29,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "data": {
    "text/plain": [
    "34"
    ]
    },
    "execution_count": 29,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "add_fn_factory(7)(20)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "execution_count": 30,
    "metadata": {
    "collapsed": true
    "collapsed": true,
    "deletable": true,
    "editable": true
    },
    "outputs": [],
    "source": [
    @@ -484,16 +642,31 @@
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "execution_count": 31,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "(13, 23, 203)\n"
    ]
    }
    ],
    "source": [
    "print (add5(3), add10(3), add100(3))"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "metadata": {
    "deletable": true,
    "editable": true
    },
    "source": [
    "## Some common higher order functions\n",
    "\n",
    @@ -504,9 +677,24 @@
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "execution_count": 32,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[1, 4, 9, 16, 25, 36, 49, 64, 81]"
    ]
    },
    "execution_count": 32,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "nums = [1,2,3,4,5,6,7,8,9]\n",
    "new_nums = []\n",
    @@ -517,36 +705,83 @@
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "execution_count": 33,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[1, 4, 9, 16, 25, 36, 49, 64, 81]"
    ]
    },
    "execution_count": 33,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "map(square,nums)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "execution_count": 34,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[5, 3, 2, 10]"
    ]
    },
    "execution_count": 34,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "map(len,[\"abcde\", \"def\", \"gh\", \"asdsfsdvdf\"])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "execution_count": 35,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "data": {
    "text/plain": [
    "'Izzeet pembec\\xc4\\xb0'"
    ]
    },
    "execution_count": 35,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "\"izZEet PEMBECİ\".capitalize()"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 21,
    "execution_count": 36,
    "metadata": {
    "collapsed": true
    "collapsed": true,
    "deletable": true,
    "editable": true
    },
    "outputs": [],
    "source": [
    @@ -555,9 +790,11 @@
    },
    {
    "cell_type": "code",
    "execution_count": 24,
    "execution_count": 37,
    "metadata": {
    "collapsed": true
    "collapsed": true,
    "deletable": true,
    "editable": true
    },
    "outputs": [],
    "source": [
    @@ -566,7 +803,7 @@
    " others = word[1:]\n",
    " if first_letter == \"i\":\n",
    " return \"İ\" + others.lower()\n",
    " elif first_letter == \"ı\": \n",
    " elif first_letter == u\"ı\": \n",
    " return \"I\" + others.lower()\n",
    " else:\n",
    " return first_letter.upper() + others.lower()\n",
    @@ -575,33 +812,44 @@
    },
    {
    "cell_type": "code",
    "execution_count": 26,
    "metadata": {},
    "execution_count": 38,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Ali can kayaturk\n"
    ]
    "data": {
    "text/plain": [
    "u'Ali can kayaturk \\u0131\\u015f\\u0131k'"
    ]
    },
    "execution_count": 38,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "print tr_capitalize(\"Ali can KAYATURK\")"
    "tr_capitalize(u\"Ali can KAYATURK ışık\")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 29,
    "metadata": {},
    "execution_count": 39,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "data": {
    "text/plain": [
    "'Ali Can Kayaturk'"
    ]
    },
    "execution_count": 29,
    "execution_count": 39,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -612,16 +860,20 @@
    },
    {
    "cell_type": "code",
    "execution_count": 35,
    "metadata": {},
    "execution_count": 40,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['HEL', 'WOR', 'THI', 'IS', 'AN', 'EXA']"
    ]
    },
    "execution_count": 35,
    "execution_count": 40,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -642,23 +894,30 @@
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "metadata": {
    "deletable": true,
    "editable": true
    },
    "source": [
    "Lambda expressions are a syntactic sugar in Python so you can define anonymous functions easily."
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 36,
    "metadata": {},
    "execution_count": 41,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['Hel', 'wor', 'Thi', 'is', 'an', 'exa']"
    ]
    },
    "execution_count": 36,
    "execution_count": 41,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -669,16 +928,20 @@
    },
    {
    "cell_type": "code",
    "execution_count": 37,
    "metadata": {},
    "execution_count": 42,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['HEL', 'WOR', 'THI', 'IS', 'AN', 'EXA']"
    ]
    },
    "execution_count": 37,
    "execution_count": 42,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -689,9 +952,11 @@
    },
    {
    "cell_type": "code",
    "execution_count": 38,
    "execution_count": 43,
    "metadata": {
    "collapsed": true
    "collapsed": true,
    "deletable": true,
    "editable": true
    },
    "outputs": [],
    "source": [
    @@ -703,16 +968,20 @@
    },
    {
    "cell_type": "code",
    "execution_count": 43,
    "metadata": {},
    "execution_count": 44,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[15, 32, 36, 308, 540]"
    ]
    },
    "execution_count": 43,
    "execution_count": 44,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -723,16 +992,20 @@
    },
    {
    "cell_type": "code",
    "execution_count": 44,
    "metadata": {},
    "execution_count": 45,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[0, 2, 0, 0, 2, 5]"
    ]
    },
    "execution_count": 44,
    "execution_count": 45,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -743,16 +1016,20 @@
    },
    {
    "cell_type": "code",
    "execution_count": 49,
    "metadata": {},
    "execution_count": 46,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['EEE', 'XXXX', 'AAAAA', 'MMMMMM']"
    ]
    },
    "execution_count": 49,
    "execution_count": 46,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -763,8 +1040,12 @@
    },
    {
    "cell_type": "code",
    "execution_count": 46,
    "metadata": {},
    "execution_count": 47,
    "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
    },
    "outputs": [
    {
    "name": "stdout",
    @@ -784,14 +1065,20 @@
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "metadata": {
    "deletable": true,
    "editable": true
    },
    "source": [
    "### filter"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "metadata": {
    "deletable": true,
    "editable": true
    },
    "source": [
    "### reduce"
    ]
    @@ -800,7 +1087,9 @@
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    "collapsed": true,
    "deletable": true,
    "editable": true
    },
    "outputs": [],
    "source": []
    @@ -822,7 +1111,7 @@
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython2",
    "version": "2.7.12"
    "version": "2.7.13"
    }
    },
    "nbformat": 4,
  5. pembeci revised this gist May 4, 2017. 2 changed files with 0 additions and 0 deletions.
    Empty file added league.json
    Empty file.
    File renamed without changes.
  6. pembeci revised this gist May 4, 2017. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion league.json
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    [{"id":394,"caption":"1. Bundesliga 2015/16","league":"BL1","year":"2015","currentMatchday":34,"numberOfMatchdays":34,"numberOfTeams":18,"numberOfGames":306,"lastUpdated":"2016-06-15T08:09:51Z"},{"id":395,"caption":"2. Bundesliga 2015/16","league":"BL2","year":"2015","currentMatchday":34,"numberOfMatchdays":34,"numberOfTeams":18,"numberOfGames":306,"lastUpdated":"2016-05-15T16:00:18Z"},{"id":396,"caption":"Ligue 1 2015/16","league":"FL1","year":"2015","currentMatchday":38,"numberOfMatchdays":38,"numberOfTeams":20,"numberOfGames":380,"lastUpdated":"2016-05-13T07:00:02Z"},{"id":397,"caption":"Ligue 2 2015/16","league":"FL2","year":"2015","currentMatchday":38,"numberOfMatchdays":38,"numberOfTeams":20,"numberOfGames":380,"lastUpdated":"2016-05-14T05:57:07Z"},{"id":398,"caption":"Premier League 2015/16","league":"PL","year":"2015","currentMatchday":38,"numberOfMatchdays":38,"numberOfTeams":20,"numberOfGames":380,"lastUpdated":"2016-05-19T15:12:55Z"},{"id":399,"caption":"Primera Division 2015/16","league":"PD","year":"2015","currentMatchday":38,"numberOfMatchdays":38,"numberOfTeams":20,"numberOfGames":380,"lastUpdated":"2016-05-16T07:16:11Z"},{"id":400,"caption":"Segunda Division 2015/16","league":"SD","year":"2015","currentMatchday":42,"numberOfMatchdays":42,"numberOfTeams":22,"numberOfGames":462,"lastUpdated":"2016-07-04T21:35:12Z"},{"id":401,"caption":"Serie A 2015/16","league":"SA","year":"2015","currentMatchday":38,"numberOfMatchdays":38,"numberOfTeams":20,"numberOfGames":380,"lastUpdated":"2016-05-16T07:15:29Z"},{"id":402,"caption":"Primeira Liga 2015/16","league":"PPL","year":"2015","currentMatchday":34,"numberOfMatchdays":34,"numberOfTeams":18,"numberOfGames":306,"lastUpdated":"2016-05-15T19:23:33Z"},{"id":403,"caption":"3. Bundesliga 2015/16","league":"BL3","year":"2015","currentMatchday":38,"numberOfMatchdays":38,"numberOfTeams":20,"numberOfGames":380,"lastUpdated":"2016-05-14T14:15:13Z"},{"id":404,"caption":"Eredivisie 2015/16","league":"DED","year":"2015","currentMatchday":34,"numberOfMatchdays":34,"numberOfTeams":18,"numberOfGames":306,"lastUpdated":"2016-05-08T18:39:50Z"},{"id":405,"caption":"Champions League 2015/16","league":"CL","year":"2015","currentMatchday":10,"numberOfMatchdays":62,"numberOfTeams":32,"numberOfGames":125,"lastUpdated":"2016-06-06T10:11:53Z"},{"id":425,"caption":"League One 2015/16","league":"EL1","year":"2015","currentMatchday":16,"numberOfMatchdays":46,"numberOfTeams":24,"numberOfGames":552,"lastUpdated":"2016-05-19T19:00:12Z"}]
  7. pembeci created this gist May 4, 2017.
    1 change: 1 addition & 0 deletions league.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    [{"id":394,"caption":"1. Bundesliga 2015/16","league":"BL1","year":"2015","currentMatchday":34,"numberOfMatchdays":34,"numberOfTeams":18,"numberOfGames":306,"lastUpdated":"2016-06-15T08:09:51Z"},{"id":395,"caption":"2. Bundesliga 2015/16","league":"BL2","year":"2015","currentMatchday":34,"numberOfMatchdays":34,"numberOfTeams":18,"numberOfGames":306,"lastUpdated":"2016-05-15T16:00:18Z"},{"id":396,"caption":"Ligue 1 2015/16","league":"FL1","year":"2015","currentMatchday":38,"numberOfMatchdays":38,"numberOfTeams":20,"numberOfGames":380,"lastUpdated":"2016-05-13T07:00:02Z"},{"id":397,"caption":"Ligue 2 2015/16","league":"FL2","year":"2015","currentMatchday":38,"numberOfMatchdays":38,"numberOfTeams":20,"numberOfGames":380,"lastUpdated":"2016-05-14T05:57:07Z"},{"id":398,"caption":"Premier League 2015/16","league":"PL","year":"2015","currentMatchday":38,"numberOfMatchdays":38,"numberOfTeams":20,"numberOfGames":380,"lastUpdated":"2016-05-19T15:12:55Z"},{"id":399,"caption":"Primera Division 2015/16","league":"PD","year":"2015","currentMatchday":38,"numberOfMatchdays":38,"numberOfTeams":20,"numberOfGames":380,"lastUpdated":"2016-05-16T07:16:11Z"},{"id":400,"caption":"Segunda Division 2015/16","league":"SD","year":"2015","currentMatchday":42,"numberOfMatchdays":42,"numberOfTeams":22,"numberOfGames":462,"lastUpdated":"2016-07-04T21:35:12Z"},{"id":401,"caption":"Serie A 2015/16","league":"SA","year":"2015","currentMatchday":38,"numberOfMatchdays":38,"numberOfTeams":20,"numberOfGames":380,"lastUpdated":"2016-05-16T07:15:29Z"},{"id":402,"caption":"Primeira Liga 2015/16","league":"PPL","year":"2015","currentMatchday":34,"numberOfMatchdays":34,"numberOfTeams":18,"numberOfGames":306,"lastUpdated":"2016-05-15T19:23:33Z"},{"id":403,"caption":"3. Bundesliga 2015/16","league":"BL3","year":"2015","currentMatchday":38,"numberOfMatchdays":38,"numberOfTeams":20,"numberOfGames":380,"lastUpdated":"2016-05-14T14:15:13Z"},{"id":404,"caption":"Eredivisie 2015/16","league":"DED","year":"2015","currentMatchday":34,"numberOfMatchdays":34,"numberOfTeams":18,"numberOfGames":306,"lastUpdated":"2016-05-08T18:39:50Z"},{"id":405,"caption":"Champions League 2015/16","league":"CL","year":"2015","currentMatchday":10,"numberOfMatchdays":62,"numberOfTeams":32,"numberOfGames":125,"lastUpdated":"2016-06-06T10:11:53Z"},{"id":425,"caption":"League One 2015/16","league":"EL1","year":"2015","currentMatchday":16,"numberOfMatchdays":46,"numberOfTeams":24,"numberOfGames":552,"lastUpdated":"2016-05-19T19:00:12Z"}]
    830 changes: 830 additions & 0 deletions lecture1.ipytnb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,830 @@
    {
    "cells": [
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "# Functional Programming / Paradigm"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "## Functions as first class values"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 1,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "\n",
    "\n",
    "def double(x):\n",
    " return 2*x + 1"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 2,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "25"
    ]
    },
    "execution_count": 2,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "double(12)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 3,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "function"
    ]
    },
    "execution_count": 3,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "type(double)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 4,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "int"
    ]
    },
    "execution_count": 4,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "type(5)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 5,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "5"
    ]
    },
    "execution_count": 5,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "a = 5\n",
    "b = a\n",
    "b"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 6,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "new_variable = double"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 7,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "25"
    ]
    },
    "execution_count": 7,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "new_variable(12)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 8,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def triple(x):\n",
    " return 3 * x\n",
    "\n",
    "def square(x):\n",
    " return x*x"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 9,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "my_funcs = [double, triple, square, triple]"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 10,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "<function __main__.triple>"
    ]
    },
    "execution_count": 10,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "my_funcs[1]"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 11,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "21"
    ]
    },
    "execution_count": 11,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "my_funcs[1](7)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 12,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "11\n",
    "15\n",
    "25\n",
    "15\n"
    ]
    }
    ],
    "source": [
    "for fn in my_funcs:\n",
    " print fn(5)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 13,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def cat_talk():\n",
    " print \"Meaw\""
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 14,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "cat = { \"type\": \"animal\", \"max_age\": 15, \"talk\": cat_talk}"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 15,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Meaw\n"
    ]
    }
    ],
    "source": [
    "cat[\"talk\"]()"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 16,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "100"
    ]
    },
    "execution_count": 16,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "new_variable = square\n",
    "new_variable(10)"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "## Higher Order Functions"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 17,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def apply_twice(f,x):\n",
    " return f(f(x))"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 18,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "81"
    ]
    },
    "execution_count": 18,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "apply_twice(square, 3)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 19,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "90"
    ]
    },
    "execution_count": 19,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "apply_twice(triple, 10)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 20,
    "metadata": {},
    "outputs": [
    {
    "ename": "TypeError",
    "evalue": "object of type 'int' has no len()",
    "output_type": "error",
    "traceback": [
    "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
    "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
    "\u001b[0;32m<ipython-input-20-6258c08cf641>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mapply_twice\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"abcd\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
    "\u001b[0;32m<ipython-input-17-362271ea4f05>\u001b[0m in \u001b[0;36mapply_twice\u001b[0;34m(f, x)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mapply_twice\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
    "\u001b[0;31mTypeError\u001b[0m: object of type 'int' has no len()"
    ]
    }
    ],
    "source": [
    "apply_twice(len,\"abcd\")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def add_to_string(word):\n",
    " return \"[\" + word + \"]\"\n"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "add_to_string(\"cat\")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "apply_twice(add_to_string, \"cat\")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def f(x):\n",
    " c = 5\n",
    " d = 2\n",
    " def g(y):\n",
    " return y*10\n",
    " return c+d+g(x)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "f(2)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def add_fn_factory(x):\n",
    " x = x*2\n",
    " def add(y):\n",
    " return x+y\n",
    " return add"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "f = add_fn_factory(7)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "f(10)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "add_fn_factory(7)(20)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "add5 = add_fn_factory(5)\n",
    "add10 = add_fn_factory(10)\n",
    "add100 = add_fn_factory(100)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "print (add5(3), add10(3), add100(3))"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "## Some common higher order functions\n",
    "\n",
    "### map\n",
    "\n",
    "`map` is used to transform one list (collection) of values into another list by applying a function to every element in a list."
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "nums = [1,2,3,4,5,6,7,8,9]\n",
    "new_nums = []\n",
    "for num in nums:\n",
    " new_nums.append(square(num))\n",
    "new_nums "
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "map(square,nums)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "map(len,[\"abcde\", \"def\", \"gh\", \"asdsfsdvdf\"])"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    "\"izZEet PEMBECİ\".capitalize()"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 21,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "# -*- coding: utf8 -*-"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 24,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def tr_capitalize(word):\n",
    " first_letter = word[0]\n",
    " others = word[1:]\n",
    " if first_letter == \"i\":\n",
    " return \"İ\" + others.lower()\n",
    " elif first_letter == \"ı\": \n",
    " return \"I\" + others.lower()\n",
    " else:\n",
    " return first_letter.upper() + others.lower()\n",
    " "
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 26,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Ali can kayaturk\n"
    ]
    }
    ],
    "source": [
    "print tr_capitalize(\"Ali can KAYATURK\")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 29,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "'Ali Can Kayaturk'"
    ]
    },
    "execution_count": 29,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "\" \".join(map(tr_capitalize,\"Ali can KAYATURK\".split()))"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 35,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['HEL', 'WOR', 'THI', 'IS', 'AN', 'EXA']"
    ]
    },
    "execution_count": 35,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "words = \"Hello world. This is an example. \".split()\n",
    "\n",
    "def first3(word):\n",
    " return word[:3]\n",
    "\n",
    "map(first3,words)\n",
    "\n",
    "def reverse(word):\n",
    " return word[:3].upper()\n",
    "\n",
    "map(reverse,words)"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "Lambda expressions are a syntactic sugar in Python so you can define anonymous functions easily."
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 36,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['Hel', 'wor', 'Thi', 'is', 'an', 'exa']"
    ]
    },
    "execution_count": 36,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "map(lambda w: w[:3], words)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 37,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['HEL', 'WOR', 'THI', 'IS', 'AN', 'EXA']"
    ]
    },
    "execution_count": 37,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "map(lambda word: word[:3].upper(), words)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 38,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "nums1 = [3,8,9,11,45]\n",
    "nums2 = [5,4,4,28,12]\n",
    "\n",
    "# nums3 = [8,12,13,39,57]"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 43,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[15, 32, 36, 308, 540]"
    ]
    },
    "execution_count": 43,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "map(lambda a,b:a*b, nums1, nums2)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 44,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[0, 2, 0, 0, 2, 5]"
    ]
    },
    "execution_count": 44,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "map(lambda a,b: a % b, [34,56,12,30,50,40], range(2,8))"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 49,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['EEE', 'XXXX', 'AAAAA', 'MMMMMM']"
    ]
    },
    "execution_count": 49,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "map(lambda a,b: a * b, \"EXAM\", range(3,7))"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 46,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "E\n",
    "X\n",
    "A\n",
    "M\n"
    ]
    }
    ],
    "source": [
    "for c in \"EXAM\":\n",
    " print c"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "### filter"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "### reduce"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": []
    }
    ],
    "metadata": {
    "kernelspec": {
    "display_name": "Python 2",
    "language": "python",
    "name": "python2"
    },
    "language_info": {
    "codemirror_mode": {
    "name": "ipython",
    "version": 2
    },
    "file_extension": ".py",
    "mimetype": "text/x-python",
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython2",
    "version": "2.7.12"
    }
    },
    "nbformat": 4,
    "nbformat_minor": 2
    }