Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Bio-IT
EPUG
Commits
cb7d81c6
Commit
cb7d81c6
authored
Jun 22, 2021
by
Sudeep Sahadevan
Browse files
update session 12
parent
e5547ae0
Changes
2
Hide whitespace changes
Inline
Side-by-side
meetings/2021/session_12_functional_programming_with_toolz/Functional_programming_tools_introduction.ipynb
View file @
cb7d81c6
...
...
@@ -30,8 +30,7 @@
"#### Why ?\n",
"* Code becomes concise and simple to understand\n",
"* Supports lazy evaluation\n",
"* Easier debugging and testing\n",
"* Easier parallelism/concurrency"
"* Easier debugging and testing"
]
},
{
...
...
@@ -105,7 +104,7 @@
},
{
"cell_type": "code",
"execution_count":
7
,
"execution_count":
5
,
"id": "214bcf3c",
"metadata": {},
"outputs": [],
...
...
@@ -126,7 +125,7 @@
},
{
"cell_type": "code",
"execution_count":
8
,
"execution_count":
6
,
"id": "b57bfc8b",
"metadata": {},
"outputs": [
...
...
@@ -135,8 +134,8 @@
"output_type": "stream",
"text": [
"adding : 41\n",
"adding counter (first call): 41 1\n",
"adding counter (second call): 41 2\n",
"adding counter (first call): 41
counter value:
1\n",
"adding counter (second call): 41
counter value:
2\n",
"power (p = 1): 1\n",
"power (p = 2): 32\n"
]
...
...
@@ -146,8 +145,8 @@
"# sum --> pure\n",
"print('adding : ',add(18,23))\n",
"# sum_counter --> impure\n",
"print('adding counter (first call): ',add_counter(18,23),counter)\n",
"print('adding counter (second call): ',add_counter(18,23),counter)\n",
"print('adding counter (first call): ',add_counter(18,23),
'counter value: ',
counter)\n",
"print('adding counter (second call): ',add_counter(18,23),
'counter value: ',
counter)\n",
"# power --> impure\n",
"print('power (p = 1):',power(5))\n",
"p = 2\n",
...
...
@@ -170,7 +169,7 @@
},
{
"cell_type": "code",
"execution_count":
9
,
"execution_count":
7
,
"id": "a15d07ad",
"metadata": {},
"outputs": [
...
...
@@ -210,7 +209,7 @@
},
{
"cell_type": "code",
"execution_count":
10
,
"execution_count":
8
,
"id": "46349b39",
"metadata": {},
"outputs": [],
...
...
@@ -239,7 +238,7 @@
},
{
"cell_type": "code",
"execution_count":
11
,
"execution_count":
9
,
"id": "da4efbfa",
"metadata": {},
"outputs": [
...
...
@@ -280,7 +279,7 @@
},
{
"cell_type": "code",
"execution_count": 1
2
,
"execution_count": 1
0
,
"id": "0aa49f71",
"metadata": {},
"outputs": [
...
...
@@ -326,7 +325,7 @@
},
{
"cell_type": "code",
"execution_count": 1
3
,
"execution_count": 1
1
,
"id": "085a88c1",
"metadata": {},
"outputs": [],
...
...
@@ -346,7 +345,7 @@
},
{
"cell_type": "code",
"execution_count": 1
4
,
"execution_count": 1
2
,
"id": "717f703d",
"metadata": {},
"outputs": [],
...
...
@@ -367,7 +366,7 @@
},
{
"cell_type": "code",
"execution_count": 1
5
,
"execution_count": 1
3
,
"id": "74da56c1",
"metadata": {},
"outputs": [
...
...
@@ -377,7 +376,7 @@
"[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]"
]
},
"execution_count": 1
5
,
"execution_count": 1
3
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -388,7 +387,7 @@
},
{
"cell_type": "code",
"execution_count": 1
6
,
"execution_count": 1
4
,
"id": "f3f60f05",
"metadata": {},
"outputs": [
...
...
@@ -398,7 +397,7 @@
"[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]"
]
},
"execution_count": 1
6
,
"execution_count": 1
4
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -409,7 +408,7 @@
},
{
"cell_type": "code",
"execution_count": 1
7
,
"execution_count": 1
5
,
"id": "e82d677a",
"metadata": {},
"outputs": [
...
...
@@ -419,7 +418,7 @@
"['This', 'Is', 'Some', 'Text']"
]
},
"execution_count": 1
7
,
"execution_count": 1
5
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -438,7 +437,7 @@
},
{
"cell_type": "code",
"execution_count": 1
8
,
"execution_count": 1
6
,
"id": "a9a99457",
"metadata": {},
"outputs": [
...
...
@@ -448,7 +447,7 @@
"[2, 4, 6, 8, 10]"
]
},
"execution_count": 1
8
,
"execution_count": 1
6
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -459,7 +458,7 @@
},
{
"cell_type": "code",
"execution_count": 1
9
,
"execution_count": 1
7
,
"id": "8c8bf214",
"metadata": {},
"outputs": [
...
...
@@ -469,7 +468,7 @@
"[2, 4, 6, 8, 10]"
]
},
"execution_count": 1
9
,
"execution_count": 1
7
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -480,7 +479,7 @@
},
{
"cell_type": "code",
"execution_count":
20
,
"execution_count":
18
,
"id": "6d35fb18",
"metadata": {},
"outputs": [
...
...
@@ -490,7 +489,7 @@
"['this', 'Some', 'TEXT']"
]
},
"execution_count":
20
,
"execution_count":
18
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -518,7 +517,7 @@
},
{
"cell_type": "code",
"execution_count":
2
1,
"execution_count": 1
9
,
"id": "ebb6f7ef",
"metadata": {},
"outputs": [
...
...
@@ -528,7 +527,7 @@
"<function __main__.<lambda>(x)>"
]
},
"execution_count":
2
1,
"execution_count": 1
9
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -539,7 +538,7 @@
},
{
"cell_type": "code",
"execution_count": 2
2
,
"execution_count": 2
0
,
"id": "5203c823",
"metadata": {},
"outputs": [
...
...
@@ -549,7 +548,7 @@
"4"
]
},
"execution_count": 2
2
,
"execution_count": 2
0
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -568,7 +567,7 @@
},
{
"cell_type": "code",
"execution_count": 2
3
,
"execution_count": 2
1
,
"id": "0cc74352",
"metadata": {},
"outputs": [
...
...
@@ -578,7 +577,7 @@
"[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]"
]
},
"execution_count": 2
3
,
"execution_count": 2
1
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -589,7 +588,7 @@
},
{
"cell_type": "code",
"execution_count": 2
4
,
"execution_count": 2
2
,
"id": "2360f9ad",
"metadata": {},
"outputs": [
...
...
@@ -599,7 +598,7 @@
"['this', 'Some', 'TEXT']"
]
},
"execution_count": 2
4
,
"execution_count": 2
2
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -625,7 +624,7 @@
},
{
"cell_type": "code",
"execution_count": 2
5
,
"execution_count": 2
3
,
"id": "fd96efc0",
"metadata": {},
"outputs": [],
...
...
@@ -635,7 +634,7 @@
},
{
"cell_type": "code",
"execution_count": 2
6
,
"execution_count": 2
4
,
"id": "f3e79aed",
"metadata": {},
"outputs": [
...
...
@@ -645,7 +644,7 @@
"113"
]
},
"execution_count": 2
6
,
"execution_count": 2
4
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -697,7 +696,7 @@
},
{
"cell_type": "code",
"execution_count": 2
7
,
"execution_count": 2
5
,
"id": "e252174a",
"metadata": {},
"outputs": [],
...
...
@@ -720,7 +719,7 @@
},
{
"cell_type": "code",
"execution_count": 2
8
,
"execution_count": 2
6
,
"id": "80222b27",
"metadata": {
"scrolled": false
...
...
@@ -753,7 +752,7 @@
},
{
"cell_type": "code",
"execution_count": 2
9
,
"execution_count": 2
7
,
"id": "f352e1c3",
"metadata": {},
"outputs": [
...
...
@@ -784,7 +783,7 @@
},
{
"cell_type": "code",
"execution_count":
30
,
"execution_count":
28
,
"id": "ef9c95fc",
"metadata": {},
"outputs": [
...
...
@@ -817,7 +816,7 @@
},
{
"cell_type": "code",
"execution_count":
31
,
"execution_count":
29
,
"id": "0aa9e0f7",
"metadata": {},
"outputs": [],
...
...
@@ -827,7 +826,7 @@
},
{
"cell_type": "code",
"execution_count": 3
2
,
"execution_count": 3
0
,
"id": "ef5df16a",
"metadata": {},
"outputs": [
...
...
@@ -837,7 +836,7 @@
"[1, 3, 5, 7, 9]"
]
},
"execution_count": 3
2
,
"execution_count": 3
0
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -859,7 +858,7 @@
},
{
"cell_type": "code",
"execution_count": 3
3
,
"execution_count": 3
1
,
"id": "f814f0b5",
"metadata": {},
"outputs": [],
...
...
@@ -869,7 +868,7 @@
},
{
"cell_type": "code",
"execution_count": 3
4
,
"execution_count": 3
2
,
"id": "005781ab",
"metadata": {},
"outputs": [
...
...
@@ -879,7 +878,7 @@
"[2, 4, 6, 8, 10]"
]
},
"execution_count": 3
4
,
"execution_count": 3
2
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -890,64 +889,64 @@
},
{
"cell_type": "markdown",
"id": "
a4077325
",
"id": "
42f768c7
",
"metadata": {},
"source": [
"**tool
z
groupby**"
"**
iter
tool
s
groupby**"
]
},
{
"cell_type": "code",
"execution_count": 3
5
,
"id": "
4baefdd1
",
"execution_count": 3
3
,
"id": "
1d272af7
",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{False: [1, 3, 5, 7, 9], True: [2, 4, 6, 8, 10]}\n"
"False [1]\n",
"True [2]\n",
"False [3]\n",
"True [4]\n",
"False [5]\n",
"True [6]\n",
"False [7]\n",
"True [8]\n",
"False [9]\n",
"True [10]\n"
]
}
],
"source": [
"print(groupby(iseven,data))"
"for key,group in itertools.groupby(data,iseven):\n",
" print(key,list(group))"
]
},
{
"cell_type": "markdown",
"id": "
42f768c7
",
"id": "
a4077325
",
"metadata": {},
"source": [
"**
iter
tool
s
groupby**"
"**tool
z
groupby**"
]
},
{
"cell_type": "code",
"execution_count": 3
6
,
"id": "
1d272af7
",
"execution_count": 3
4
,
"id": "
4baefdd1
",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False [1]\n",
"True [2]\n",
"False [3]\n",
"True [4]\n",
"False [5]\n",
"True [6]\n",
"False [7]\n",
"True [8]\n",
"False [9]\n",
"True [10]\n"
"{False: [1, 3, 5, 7, 9], True: [2, 4, 6, 8, 10]}\n"
]
}
],
"source": [
"for key,group in itertools.groupby(data,iseven):\n",
" print(key,list(group))"
"print(groupby(iseven,data))"
]
},
{
...
...
@@ -960,7 +959,7 @@
},
{
"cell_type": "code",
"execution_count": 3
7
,
"execution_count": 3
5
,
"id": "12ce1dac",
"metadata": {},
"outputs": [
...
...
@@ -970,7 +969,7 @@
"{True: ['this', 'Some', 'TEXT'], False: ['is']}"
]
},
"execution_count": 3
7
,
"execution_count": 3
5
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -982,7 +981,7 @@
},
{
"cell_type": "code",
"execution_count": 3
8
,
"execution_count": 3
6
,
"id": "2f7812c3",
"metadata": {},
"outputs": [],
...
...
@@ -993,7 +992,7 @@
},
{
"cell_type": "code",
"execution_count": 3
9
,
"execution_count": 3
7
,
"id": "f292e668",
"metadata": {},
"outputs": [
...
...
@@ -1003,7 +1002,7 @@
"{4: ['this', 'Some', 'TEXT'], 2: ['is']}"
]
},
"execution_count": 3
9
,
"execution_count": 3
7
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -1024,7 +1023,7 @@
},
{
"cell_type": "code",
"execution_count":
40
,
"execution_count":
38
,
"id": "598fb73e",
"metadata": {},
"outputs": [],
...
...
@@ -1034,7 +1033,7 @@
},
{
"cell_type": "code",
"execution_count":
41
,
"execution_count":
39
,
"id": "51e1dbe6",
"metadata": {},
"outputs": [
...
...
@@ -1044,7 +1043,7 @@
"{False: 5, True: 5}"
]
},
"execution_count":
41
,
"execution_count":
39
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -1055,7 +1054,7 @@
},
{
"cell_type": "code",
"execution_count": 4
2
,
"execution_count": 4
0
,
"id": "dc95d58a",
"metadata": {},
"outputs": [
...
...
@@ -1065,7 +1064,7 @@
"{4: 3, 2: 1}"
]
},
"execution_count": 4
2
,
"execution_count": 4
0
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -1093,7 +1092,7 @@
},
{
"cell_type": "code",
"execution_count": 4
3
,
"execution_count": 4
1
,
"id": "45f4b438",
"metadata": {},
"outputs": [],
...
...
@@ -1111,7 +1110,7 @@
},
{
"cell_type": "code",
"execution_count": 4
4
,
"execution_count": 4
2
,
"id": "a5c11408",
"metadata": {},
"outputs": [
...
...
@@ -1121,7 +1120,7 @@
"935"
]
},
"execution_count": 4
4
,
"execution_count": 4
2
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -1132,7 +1131,7 @@
},
{
"cell_type": "code",
"execution_count": 4
5
,
"execution_count": 4
3
,
"id": "dcf87ac8",
"metadata": {},
"outputs": [
...
...
@@ -1142,7 +1141,7 @@
"935"
]
},
"execution_count": 4
5
,
"execution_count": 4
3
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -1167,7 +1166,7 @@
},
{
"cell_type": "code",
"execution_count": 4
6
,
"execution_count": 4
4
,
"id": "bbba4124",
"metadata": {},
"outputs": [],
...
...
@@ -1177,7 +1176,7 @@
},
{
"cell_type": "code",
"execution_count": 4
7
,
"execution_count": 4
5
,
"id": "e0cafff7",
"metadata": {},
"outputs": [
...
...
@@ -1187,7 +1186,7 @@
"935"
]
},
"execution_count": 4
7
,
"execution_count": 4
5
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -1215,13 +1214,12 @@