Skip to content

Commit 1ec11c3

Browse files
committed
update TOC, fix details
1 parent d82a638 commit 1ec11c3

File tree

1 file changed

+39
-15
lines changed

1 file changed

+39
-15
lines changed

11_functional_programming.ipynb

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@
1414
"## Table of Contents\n",
1515
" - [References](#References)\n",
1616
" - [Introduction](#Introduction)\n",
17-
" - [Why Functional Programming](#Why-Functional-Programming)\n",
17+
" - [Why Functional Programming?](#Why-Functional-Programming?)\n",
1818
" - [The basic principles of functional programming](#The-basic-principles-of-functional-programming)\n",
1919
" - [Pure Functions (Purity)](#Pure-Functions-(Purity))\n",
20-
" - [Example](#Example)\n",
20+
" - [Exercise on pure functions](#Exercise-on-pure-functions)\n",
2121
" - [Quiz on pure functions](#Quiz-on-pure-functions)\n",
2222
" - [Immutability](#Immutability)\n",
2323
" - [Composition](#Composition)\n",
24-
" - [Example](#Example)\n",
24+
" - [Exercise on composition](#Exercise-on-composition)\n",
2525
" - [Higher Order Functions / Functions as Values](#Higher-Order-Functions-/-Functions-as-Values)\n",
2626
" - [Referential transparency](#Referential-transparency)\n",
2727
" - [Type systems](#Type-systems)\n",
2828
" - [Mapping / Iteration ](#Mapping-/-Iteration)\n",
2929
" - [Mapping](#Mapping)\n",
3030
" - [Filtering](#Filtering)\n",
3131
" - [Reducing](#Reducing)\n",
32-
" - [Examples: Iteration and Mapping](#Examples:-Iteration-and-Mapping)\n",
32+
" - [Exercises on Iteration and Mapping](#Exercises-on-Iteration-and-Mapping)\n",
3333
" - [List comprehensions](#List-comprehensions)\n",
34-
" - [Example: Keeping only multiples of n](#Example:-Keeping-only-multiples-of-n)\n",
34+
" - [Exercise: Keeping only multiples of `n`](#Exercise:-Keeping-only-multiples-of-n)\n",
3535
" - [Combinining and splitting iterators](#Combinining-and-splitting-iterators)\n",
3636
" - [Exercises](#Exercises)\n",
3737
" - [Exercise 1: Transposing a Matrix](#Exercise-1:-Transposing-a-Matrix)\n",
@@ -269,11 +269,11 @@
269269
"%%ipytest\n",
270270
"\n",
271271
"def solution_pure_function(array: list, new_element: str):\n",
272-
" \"\"\"A function that appends a given array with a given element.\n",
272+
" \"\"\"A pure function that appends a given array with a given element.\n",
273273
"\n",
274274
" Args:\n",
275275
" array: the initial list\n",
276-
" new_element: the element to be added to the initial list\n",
276+
" new_element: the element to be added to the list\n",
277277
" Returns:\n",
278278
" - the appended list\n",
279279
" \"\"\"\n",
@@ -311,6 +311,28 @@
311311
"## Immutability"
312312
]
313313
},
314+
{
315+
"cell_type": "markdown",
316+
"metadata": {},
317+
"source": [
318+
"Previously, we saw this example:"
319+
]
320+
},
321+
{
322+
"cell_type": "code",
323+
"execution_count": null,
324+
"metadata": {},
325+
"outputs": [],
326+
"source": [
327+
"x = [\"short\", \"list\"] \n",
328+
"def do_something(y: str) -> None:\n",
329+
" x.append(y)\n",
330+
"\n",
331+
"print(x)\n",
332+
"do_something(\"a\")\n",
333+
"print(x)"
334+
]
335+
},
314336
{
315337
"cell_type": "markdown",
316338
"metadata": {
@@ -320,9 +342,9 @@
320342
"tags": []
321343
},
322344
"source": [
323-
"When writing programs in functional style, we usually avoid functions like `do_something`.\n",
324-
"Instead of modifying existing data (*mutation*), you write functions that transform your data and return new objects.\n",
325-
"Another way to rewrite the function above could be:"
345+
"But when writing programs in functional style, we prefer to avoid functions like `do_something`.\n",
346+
"Instead of modifying existing data (*mutation*), we write functions that transform the data and return new objects.\n",
347+
"So, another way to rewrite the function above could be:"
326348
]
327349
},
328350
{
@@ -621,8 +643,10 @@
621643
}
622644
},
623645
"source": [
624-
"If `transparent_function` is referentially transparent, we can replace its value in the expression `y1 = transparent_function(1) + 5` and we obtain the same value.\n",
625-
"In other words, the value of `y` does not change if we execute it again, as shown in the code above: we have two expressions `y1` and `y2` that use the same code and because `x` is referentially transparent, their values are equal.\n",
646+
"If `transparent_function` is referentially transparent, we can replace the function call `transparent_function(1)` with its result without changing the meaning of the expression.\n",
647+
"For example, in `y1 = transparent_function(1) + 5`, the result of `y1` will always be the same every time the code runs.\n",
648+
"This is demonstrated by two expressions, `y1` and `y2`, that both use `transparent_function(1)`, and because the function is referentially transparent, `y1` and `y2` will have equal values.\n",
649+
"\n",
626650
"\n",
627651
"In this case, this is true because the value of `x` is simply 1 and is invariant.\n",
628652
"\n",
@@ -1300,7 +1324,7 @@
13001324
"\n",
13011325
"<div class=\"alert alert-block alert-warning\">\n",
13021326
" <h4><b>Question</b></h4>\n",
1303-
" Given a list <code>L</code> of integers, write a function that only keeps the numbers that are multiples of a given constant <code>k</code>.\n",
1327+
" Given a list of integers, write a function that only keeps the numbers that are multiples of a given constant <code>k</code>.\n",
13041328
" <ul>\n",
13051329
" <li>\n",
13061330
" <strong>Example 1</strong>: given <code>nums = [1, 2, 3, 4, 5]</code>, and <code>k = 2</code>, the result must be <code>[2, 4]</code>\n",
@@ -1339,13 +1363,13 @@
13391363
" \"\"\"A function that keeps only the multiples of k from a given list.\n",
13401364
"\n",
13411365
" Args:\n",
1342-
" l: the initial list\n",
1366+
" my_list: the initial list\n",
13431367
" k: the integer number\n",
13441368
" Returns:\n",
13451369
" - the filtered list\n",
13461370
" \"\"\"\n",
13471371
"\n",
1348-
" return [a for a in my_list if a % k == 0]"
1372+
" return"
13491373
]
13501374
},
13511375
{

0 commit comments

Comments
 (0)