|
14 | 14 | "## Table of Contents\n", |
15 | 15 | " - [References](#References)\n", |
16 | 16 | " - [Introduction](#Introduction)\n", |
17 | | - " - [Why Functional Programming](#Why-Functional-Programming)\n", |
| 17 | + " - [Why Functional Programming?](#Why-Functional-Programming?)\n", |
18 | 18 | " - [The basic principles of functional programming](#The-basic-principles-of-functional-programming)\n", |
19 | 19 | " - [Pure Functions (Purity)](#Pure-Functions-(Purity))\n", |
20 | | - " - [Example](#Example)\n", |
| 20 | + " - [Exercise on pure functions](#Exercise-on-pure-functions)\n", |
21 | 21 | " - [Quiz on pure functions](#Quiz-on-pure-functions)\n", |
22 | 22 | " - [Immutability](#Immutability)\n", |
23 | 23 | " - [Composition](#Composition)\n", |
24 | | - " - [Example](#Example)\n", |
| 24 | + " - [Exercise on composition](#Exercise-on-composition)\n", |
25 | 25 | " - [Higher Order Functions / Functions as Values](#Higher-Order-Functions-/-Functions-as-Values)\n", |
26 | 26 | " - [Referential transparency](#Referential-transparency)\n", |
27 | 27 | " - [Type systems](#Type-systems)\n", |
28 | 28 | " - [Mapping / Iteration ](#Mapping-/-Iteration)\n", |
29 | 29 | " - [Mapping](#Mapping)\n", |
30 | 30 | " - [Filtering](#Filtering)\n", |
31 | 31 | " - [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", |
33 | 33 | " - [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", |
35 | 35 | " - [Combinining and splitting iterators](#Combinining-and-splitting-iterators)\n", |
36 | 36 | " - [Exercises](#Exercises)\n", |
37 | 37 | " - [Exercise 1: Transposing a Matrix](#Exercise-1:-Transposing-a-Matrix)\n", |
|
269 | 269 | "%%ipytest\n", |
270 | 270 | "\n", |
271 | 271 | "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", |
273 | 273 | "\n", |
274 | 274 | " Args:\n", |
275 | 275 | " 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", |
277 | 277 | " Returns:\n", |
278 | 278 | " - the appended list\n", |
279 | 279 | " \"\"\"\n", |
|
311 | 311 | "## Immutability" |
312 | 312 | ] |
313 | 313 | }, |
| 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 | + }, |
314 | 336 | { |
315 | 337 | "cell_type": "markdown", |
316 | 338 | "metadata": { |
|
320 | 342 | "tags": [] |
321 | 343 | }, |
322 | 344 | "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:" |
326 | 348 | ] |
327 | 349 | }, |
328 | 350 | { |
|
621 | 643 | } |
622 | 644 | }, |
623 | 645 | "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", |
626 | 650 | "\n", |
627 | 651 | "In this case, this is true because the value of `x` is simply 1 and is invariant.\n", |
628 | 652 | "\n", |
|
1300 | 1324 | "\n", |
1301 | 1325 | "<div class=\"alert alert-block alert-warning\">\n", |
1302 | 1326 | " <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", |
1304 | 1328 | " <ul>\n", |
1305 | 1329 | " <li>\n", |
1306 | 1330 | " <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 | 1363 | " \"\"\"A function that keeps only the multiples of k from a given list.\n", |
1340 | 1364 | "\n", |
1341 | 1365 | " Args:\n", |
1342 | | - " l: the initial list\n", |
| 1366 | + " my_list: the initial list\n", |
1343 | 1367 | " k: the integer number\n", |
1344 | 1368 | " Returns:\n", |
1345 | 1369 | " - the filtered list\n", |
1346 | 1370 | " \"\"\"\n", |
1347 | 1371 | "\n", |
1348 | | - " return [a for a in my_list if a % k == 0]" |
| 1372 | + " return" |
1349 | 1373 | ] |
1350 | 1374 | }, |
1351 | 1375 | { |
|
0 commit comments