ch7s2_MapFilterReduce

Functional programming emphasizes **transformations over mutations**, **immutability**, and **declarative logic**.

Chapter 7: Functional Programming

Sub-Chapter: Map, Filter, and Reduce — Transform, Filter, and Aggregate with Elegance

Functional programming emphasizes transformations over mutations, immutability, and declarative logic.
In Python, the built-in functions map(), filter(), and reduce() (from functools) embody this philosophy — allowing data to flow through concise, composable transformations.


🧱 1. Functional Programming Mindset

Traditional loops mutate state:

result = []
for x in [1, 2, 3]:
    result.append(x * 2)

Functional programming transforms data without mutation:

result = list(map(lambda x: x * 2, [1, 2, 3]))

Advantages:


⚙️ 2. The map() Function — Transformation

map(func, iterable) applies a function to each element and returns an iterator.

numbers = [1, 2, 3, 4, 5]
squares = map(lambda x: x ** 2, numbers)
print(list(squares))  # [1, 4, 9, 16, 25]

Multiple Iterables

map() can handle multiple sequences — the function receives one element from each:

a = [1, 2, 3]
b = [4, 5, 6]
sums = list(map(lambda x, y: x + y, a, b))
print(sums)  # [5, 7, 9]

Using Built-in Functions

words = ["hello", "world", "python"]
uppercased = list(map(str.upper, words))
print(uppercased)

🔍 3. The filter() Function — Selection

filter(func, iterable) filters elements by truth value.
The function should return True for elements to keep.

numbers = range(10)
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(evens)  # [0, 2, 4, 6, 8]

Filtering by Condition

names = ["Alice", "Bob", "Charlie", "Dave"]
filtered = list(filter(lambda n: len(n) > 3, names))
print(filtered)  # ['Alice', 'Charlie', 'Dave']

Filtering Truthy Values

If you pass None as the first argument, filter() removes falsy elements:

values = ["", 0, None, "Python", [], 42]
truthy = list(filter(None, values))
print(truthy)  # ['Python', 42]

🔢 4. The reduce() Function — Aggregation

reduce(func, iterable) repeatedly applies a function to the first two items, then to the result and next item, and so on.
It “reduces” a sequence into a single value.

from functools import reduce

numbers = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x * y, numbers)
print(product)  # 120

With an Initial Value

total = reduce(lambda x, y: x + y, [1, 2, 3], 10)
print(total)  # 16 (starts with 10)

Using operator for Cleaner Code

import operator
from functools import reduce

numbers = [1, 2, 3, 4, 5]
sum_result = reduce(operator.add, numbers)
product = reduce(operator.mul, numbers)

✅ Prefer sum(), min(), and max() when possible — they’re optimized and clearer than custom reductions.


🧩 5. Combining map(), filter(), and reduce() — Functional Pipelines

These functions can be chained for expressive data transformations.

Example: Square Even Numbers and Sum Them

from functools import reduce

numbers = [1, 2, 3, 4, 5, 6]
result = reduce(
    lambda x, y: x + y,
    map(lambda n: n ** 2, filter(lambda n: n % 2 == 0, numbers))
)
print(result)  # 56 (2² + 4² + 6²)

Example: Clean, Capitalize, and Join Words

words = [" python  ", "MAP", " filter ", "REDUCE"]
result = " ".join(map(str.capitalize, map(str.strip, words)))
print(result)  # "Python Map Filter Reduce"

🧠 6. Comparing to Comprehensions

OperationFunctionalComprehension
Mappingmap(f, data)[f(x) for x in data]
Filteringfilter(cond, data)[x for x in data if cond(x)]
Bothmap(f, filter(cond, data))[f(x) for x in data if cond(x)]

Comprehensions are often more Pythonic for simple transformations, while functional pipelines shine in more complex or dynamic contexts.


🧮 7. Real-World Examples

Example 1 — Data Processing (JSON-like)

data = [
    {"name": "Alice", "age": 25},
    {"name": "Bob", "age": 17},
    {"name": "Charlie", "age": 30}
]

# Names of adults, uppercased
result = list(map(lambda d: d["name"].upper(), filter(lambda d: d["age"] >= 18, data)))
print(result)  # ['ALICE', 'CHARLIE']

Example 2 — Text Analytics

from functools import reduce

text = "Functional programming in Python is powerful and elegant".split()
length_sum = reduce(lambda x, y: x + len(y), filter(lambda w: len(w) > 3, text), 0)
print("Total length:", length_sum)

Example 3 — Product of Positive Numbers

nums = [-3, 4, 2, -1, 5]
positive_product = reduce(lambda a, b: a * b, filter(lambda n: n > 0, nums))
print(positive_product)  # 40

🧮 8. Functional Flow Visualization

   Input → filter() → map() → reduce()
     ↓         ↓         ↓         ↓
 [1..10]   [evens]   [squares]   [sum]

🧾 9. Best Practices

✅ Use map/filter/reduce for clear, chainable transformations.
✅ Convert results with list() or tuple() when iteration is required.
✅ Combine with lambda, operator, and functools for concise pipelines.
✅ Prefer comprehensions for readability when transformations are simple.
✅ Use reduce() sparingly — Python offers clearer built-ins (sum, any, all).
✅ Avoid side effects inside map/filter/reduce (pure functions only).


🧠 Summary

FunctionPurposeReturns
map(f, seq)Transform each itemIterable (map object)
filter(pred, seq)Keep items matching predicateIterable (filter object)
reduce(f, seq)Combine all items into oneSingle value

By mastering map, filter, and reduce, you move from procedural iteration to declarative transformation — writing cleaner, more expressive, and functional Python code.