{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ch10-title",
   "metadata": {},
   "source": [
    "# Functional Programming"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ch10-overview",
   "metadata": {},
   "source": [
    "Data science work is mostly transformation: clean raw records, convert text to numbers, keep the rows that matter, and rank or summarize the result. When these steps change shared data in place or depend on hidden state, a result becomes hard to reproduce and a bug becomes hard to trace. Functional programming organizes the same work as transformations from inputs to outputs, so each step can be tested on its own and rerun with the same result. In Chapter 9, objects managed state; this chapter adds the complementary tools for calculations.\n",
    "\n",
    "- Pure functions and side effects\n",
    "- Immutable data and returned values\n",
    "- Functions as values\n",
    "- Comprehensions, lambdas, and higher-order functions\n",
    "- Decorators, recursion, context managers, and `functools`\n",
    "\n",
    "Python is not a purely functional language, but it includes functional tools that help you write concise, reusable, and testable code.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1000-video",
   "metadata": {},
   "source": [
    "<h2>Chapter Video</h2>\n",
    "\n",
    "This 15-minute video introduces the idea that functions are values that can be assigned, passed, and returned.\n",
    "\n",
    "```{raw} html\n",
    "<iframe width=\"100%\" height=\"400\" src=\"https://www.youtube-nocookie.com/embed/kr0mpwqttM0\" title=\"Programming Terms: First-Class Functions\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen referrerpolicy=\"strict-origin-when-cross-origin\"></iframe>\n",
    "```\n",
    "\n",
    "> *[Programming Terms: First-Class Functions (15:28)](https://youtube.com/watch?v=kr0mpwqttM0) by Corey Schafer*\n",
    ">\n",
    "> Hosted on YouTube; property of its original creator, embedded here for educational reference only. As you watch, notice where a function is passed as an argument, and compare it with the `map()` and `sorted()` examples in 1001.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ch10-goals",
   "metadata": {},
   "source": [
    "<h2>Learning Objectives</h2>\n",
    "\n",
    "By the end of this chapter, you will be able to:\n",
    "\n",
    "1. Classify functions as pure or impure, and rewrite functions that mutate their inputs so that they return new values\n",
    "2. Pass, return, and wrap functions with lambda expressions, `map()`, `filter()`, `sorted()`, `reduce()`, closures, and decorators\n",
    "3. Rewrite `map()` and `filter()` pipelines as comprehensions, choose between the two styles, and refactor a loop into a pipeline of small pure steps\n",
    "4. Compare recursion with iteration, and write recursive functions for nested data\n",
    "5. Write context managers with `@contextmanager`, and use `partial()` and `lru_cache()` from `functools` where they are safe\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ch10-flow",
   "metadata": {},
   "source": [
    "<h2>Chapter Flow</h2>\n",
    "\n",
    "```{container} chapter-flow\n",
    "1. {doc}`1001-func-prog`\n",
    "2. {doc}`1002-func-practice`\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ch10-glossary",
   "metadata": {},
   "source": [
    "<h2>Glossary</h2>\n",
    "\n",
    "| # | Term | Explanation |\n",
    "|---:|---|---|\n",
    "| 1 | Functional programming | A programming style that emphasizes functions, expressions, and limited mutation. |\n",
    "| 2 | Pure function | A function whose output depends only on its inputs and that has no side effects. |\n",
    "| 3 | Side effect | A change or interaction outside a function's returned value, such as printing or mutating data. |\n",
    "| 4 | Immutability | The practice of using values that are not changed after creation. |\n",
    "| 5 | First-class function | A function that can be stored, passed as an argument, or returned like other values. |\n",
    "| 6 | Higher-order function | A function that accepts another function as an argument or returns a function. |\n",
    "| 7 | Closure | A function that remembers values from the scope where it was created, even after that scope has finished running. |\n",
    "| 8 | Lambda function | A small anonymous function written with the `lambda` keyword. |\n",
    "| 9 | Map | A pattern that transforms each item in a collection. |\n",
    "| 10 | Filter | A pattern that keeps only items that satisfy a condition. |\n",
    "| 11 | Reduce | A pattern that combines many values into one result. |\n",
    "| 12 | `reduce()` initializer | The optional starting value of the accumulator in `reduce()`, needed when the items differ from the result type or the sequence may be empty. |\n",
    "| 13 | Decorator | A function that wraps another function to extend or change its behavior. |\n",
    "| 14 | Comprehension | A compact expression for building a collection from an iterable. |\n",
    "| 15 | Recursion | A technique where a function calls itself on a smaller version of a problem. |\n",
    "| 16 | Base case | The stopping condition in a recursive function. |\n",
    "| 17 | Recursive case | The part of a recursive function that calls the function again. |\n",
    "| 18 | Context manager | An object that manages setup and cleanup around a `with` block. |\n",
    "| 19 | `functools` | A standard-library module with tools for functional programming patterns. |\n",
    "| 20 | Partial function | A function with some arguments pre-filled, often created with `functools.partial`. |\n",
    "| 21 | Memoization | Saving a function's results by argument so that a repeated call returns the saved result instead of recomputing it. |\n",
    "| 22 | Cached result | A saved function result returned immediately when the same arguments are used again. |\n",
    "| 23 | `lru_cache` | A `functools` decorator that memoizes recent function calls. |\n",
    ""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1000-slides",
   "metadata": {},
   "source": [
    "<h2><a href=\"overview.html\" target=\"_blank\" style=\"color: var(--pst-color-link, #176de8);\">Chapter Overview Slides</a></h2>"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
