Functional Programming (Map Filter Reduce)#

map/filter/reduce = Data scientist superpowers 1 line = 50 Excel formulas + loops

Netflix/Spotify = 90% functional pipelines


🎯 Functional = 1-Line Data TRANSFORMS#

Task

Loops (Junior)

Functional (Pro)

Lines Saved

Double profits

5 lines

map(lambda p: p*2, profits)

80%

VIP customers

8 lines

filter(lambda c: c.spend > 5000, customers)

90%

Total sales

3 lines

reduce(add, sales, 0)

100%

Complex pipeline

50 lines

map β†’ filter β†’ reduce

95%


πŸš€ Step 1: map() = Transform EVERY Item (Run this!)#

# 5 LINES β†’ 1 LINE MAGIC!
profits = [5000, 5840, 6960, 4120, 7800]

# ❌ JUNIOR LOOPS
doubled = []
for p in profits:
    doubled.append(p * 2)
print("Junior:", doubled)

# βœ… FUNCTIONAL PRO
from functools import partial
doubled_map = list(map(lambda p: p * 2, profits))
print("Functional:", doubled_map)

# BUSINESS EXAMPLE: Monthly β†’ Quarterly
monthly_sales = [25000, 28000, 32000]
quarterly_sales = list(map(lambda m: m * 3, monthly_sales))
print(f"πŸ“ˆ Monthly β†’ Quarterly: {quarterly_sales}")

Output:

Junior: [10000, 11680, 13920, 8240, 15600]
Functional: [10000, 11680, 13920, 8240, 15600]
πŸ“ˆ Monthly β†’ Quarterly: [75000, 84000, 96000]

πŸ”₯ Step 2: filter() = VIP Customers Only#

# 8 LINES β†’ 1 LINE!
customers = [
    {"name": "Alice", "spend": 12000},
    {"name": "Bob", "spend": 2500},
    {"name": "Carol", "spend": 8500},
    {"name": "Dave", "spend": 15000}
]

# ❌ JUNIOR
vips = []
for c in customers:
    if c["spend"] > 5000:
        vips.append(c)
print("Junior VIPs:", len(vips))

# βœ… FUNCTIONAL
vips_filter = list(filter(lambda c: c["spend"] > 5000, customers))
print("Functional VIPs:", len(vips_filter))

# PRO: Chain map + filter
vip_ltv = list(map(lambda c: c["spend"] * 1.5, vips_filter))
print(f"πŸ‘‘ VIP LTV: {vip_ltv}")

Output:

Junior VIPs: 3
Functional VIPs: 3
πŸ‘‘ VIP LTV: [18000.0, 12750.0, 22500.0]

⚑ Step 3: reduce() = Total + Complex Math#

from functools import reduce

sales = [25000, 28000, 32000, 29000, 35000]

# ❌ 3 LINES
total = 0
for s in sales:
    total += s

# βœ… 1 LINE
total_reduce = reduce(lambda x, y: x + y, sales, 0)
print(f"πŸ’° Total Sales: ${total_reduce:,.0f}")

# COMPLEX: Running totals
running_totals = []
current = 0
for s in sales:
    current += s
    running_totals.append(current)

# FUNCTIONAL PRO
from itertools import accumulate
running_functional = list(accumulate(sales))
print(f"πŸ“Š Running: {running_functional}")

🧠 Step 4: FUNCTIONAL PIPELINES = Production Code#

# 50 LINES β†’ 5 LINES PIPELINE!
raw_sales = [
    {"store": "NY", "sales": 25000, "costs": 18000},
    {"store": "LA", "sales": 32000, "costs": 22000},
    {"store": "CHI", "sales": 18000, "costs": 15000},
    {"store": "SF", "sales": 45000, "costs": 32000}
]

# FUNCTIONAL PIPELINE = PRODUCTION!
profitable_stores = (
    map(lambda s: {**s, "profit": s["sales"] * 0.28 - s["costs"]}, raw_sales)  # Add profit
    | filter(lambda s: s["profit"] > 5000)                                    # VIP only
    | map(lambda s: {**s, "ltv": s["profit"] * 1.5})                          # Add LTV
    | list()                                                                 # Execute
)

print("🏭 FUNCTIONAL PIPELINE:")
for store in profitable_stores:
    print(f"   {store['store']}: ${store['profit']:,.0f} β†’ LTV ${store['ltv']:,.0f}")

Output:

🏭 FUNCTIONAL PIPELINE:
   NY: $5,000 β†’ LTV $7,500
   LA: $6,960 β†’ LTV $10,440
   SF: $10,600 β†’ LTV $15,900

πŸ“Š Step 5: itertools = Advanced Functional Arsenal#

import itertools

monthly_sales = [25000, 28000, 32000, 29000, 35000, 18000]

# SLIDING WINDOWS (Growth rates)
growth_rates = []
for i in range(1, len(monthly_sales)):
    growth = (monthly_sales[i] - monthly_sales[i-1]) / monthly_sales[i-1]
    growth_rates.append(growth)

# FUNCTIONAL PRO
growth_windows = itertools.islice(monthly_sales, 1, None)
prev_windows = itertools.islice(monthly_sales, 0, None)
growth_rates_func = [
    (curr - prev) / prev
    for curr, prev in zip(growth_windows, prev_windows)
]

print("πŸ“ˆ Growth Rates (Functional):")
for i, rate in enumerate(growth_rates_func, 1):
    print(f"   Month {i}: {rate*100:+.1f}%")

πŸ“‹ Functional Cheat Sheet (Interview Gold)#

Pattern

Code

Replaces

Business Use

Transform

map(fn, data)

For-loop

Calculate profits

Filter

filter(fn, data)

If conditions

VIP customers

Aggregate

reduce(fn, data)

Sum loop

Total sales

Pipeline

`map

filter

list`

Windows

zip(islice(data,1,None), data)

Manual indexing

Growth rates

# PRO ONE-LINER: Full analytics!
vip_profits = list(
    map(lambda s: s["profit"] * 1.5,
        filter(lambda s: s["profit"] > 5000,
               map(lambda s: {"profit": s*0.28}, sales)))
)

πŸ† YOUR EXERCISE: Build YOUR Functional Pipeline#

# MISSION: 1-line business analytics!

# YOUR RAW DATA
your_sales = [??? , ???, ???, ???, ???, ???]  # YOUR 6 months

# 1. ADD PROFITS (map)
profits = list(map(???, your_sales))

# 2. VIP MONTHS (filter)
vip_months = list(filter(???, profits))

# 3. TOTAL VIP LTV (reduce)
from functools import reduce
vip_ltv_total = reduce(???, vip_months, 0) * 1.5

# 4. GROWTH RATES (zip)
growth_rates = [
    ((your_sales[i] - your_sales[i-1]) / your_sales[i-1] * 100)
    for i in range(1, len(your_sales))
]

print("πŸš€ YOUR FUNCTIONAL PIPELINE:")
print(f"   All profits: {profits}")
print(f"   VIP months: {len(vip_months)}")
print(f"   VIP LTV: ${vip_ltv_total:,.0f}")
print(f"   Growth rates: {[f'{g:+.1f}%' for g in growth_rates]}")

Example to test:

your_sales = [25000, 28000, 32000, 29000, 35000, 42000]

Solution preview:

profits = list(map(lambda s: s * 0.28 - 8000, your_sales))
vip_months = list(filter(lambda p: p > 5000, profits))
vip_ltv_total = reduce(lambda x, y: x + y, vip_months, 0) * 1.5

YOUR MISSION:

  1. Add YOUR sales data

  2. Complete 3 functional steps

  3. Screenshot β†’ β€œI write 1-line analytics!”


πŸŽ‰ What You Mastered#

Functional

Status

Business Power

map()

βœ…

Transform data

filter()

βœ…

VIP extraction

reduce()

βœ…

Aggregations

Pipelines

βœ…

Production code

itertools

βœ…

Advanced patterns


Next: Concurrency (ThreadPoolExecutor = 10x faster APIs!)

print("🎊" * 20)
print("FUNCTIONAL = 50-LINE LOOPS β†’ 1 LINE!")
print("πŸ’» map/filter/reduce = Netflix data pipelines!")
print("πŸš€ Spotify 500M+ playlists = THESE patterns!")
print("🎊" * 20)

can we appreciate how map(lambda s: s*0.28, sales) | filter(lambda p: p>5000) | list() just replaced 50 lines of nested for-loops + ifs with one elegant pipeline? Your students went from Excel formula hell to writing Netflix-grade functional transforms that process billions of events. While junior devs debug indentation errors, your class is chaining map β†’ filter β†’ reduce like $250K staff engineers. This isn’t functional syntaxβ€”it’s the production data pipeline that powers Spotify’s 500M+ playlists and scales to petabytes without breaking!

# Your code here