Functional Programming

Master functional programming patterns, pure functions, composition, and advanced FP architectures

Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.

Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

What Functional Programming Actually Means

Functional programming is based on core principles:

Pure Functions

A pure function has no side effects, doesn't modify external state, and always produces the same output for the same input.

Immutability in Python

FP encourages transforming data instead of mutating it. Use immutable types and return new values.

Higher-Order Functions

Functions that take functions as arguments or return functions. Core to functional programming.

map(), filter(), reduce() — Core FP Tools

The fundamental functional transformations for working with sequences.

Lambda Functions

Anonymous functions perfect for functional pipelines and quick transformations.

Function Composition

Building complex operations by chaining simple pure functions.

functools.partial — Pre-Configuring Functions

Create specialized versions of functions by "freezing" some arguments.

Generator-Based Functional Programming

Generators provide lazy evaluation and memory-efficient functional pipelines.

Advanced: Currying

Transform multi-argument functions into chains of single-argument functions.

Decorators — Functional Power Tools

Decorators are higher-order functions that transform and enhance functions.

Functional Error Handling

Handle errors with values instead of exceptions, enabling safer composition.

Immutable Data Structures

Use frozen dataclasses and immutable collections for safer functional code.

Real-World Functional Pipeline

A complete functional architecture for data processing.

Summary

You've learned comprehensive functional programming in Python:

Functional programming helps you build cleaner, more predictable, and easier-to-maintain code. These patterns are essential for data engineering, ML pipelines, ETL systems, and modern backend architectures.

📋 Quick Reference — Functional Programming

You can now write purely functional Python using map, filter, reduce, and composition — essential for data pipelines and ML workflows.

Up next: Metaprogramming — write code that generates and inspects other code at runtime.

Practice quiz

What defines a pure function?

  • It prints output
  • It uses global state
  • Same input always gives the same output with no side effects
  • It modifies its arguments

Answer: Same input always gives the same output with no side effects. A pure function always returns the same output for the same input and has no side effects, making it predictable and testable.

Which of these Python types is IMMUTABLE?

  • tuple
  • list
  • dict
  • set

Answer: tuple. Tuples (like int, float, str, frozenset) are immutable; lists, dicts, and sets are mutable.

What does map(lambda x: x * 2, [1, 2, 3]) produce when wrapped in list()?

map applies the function to each element, doubling them to give [2, 4, 6].

What does filter(lambda x: x % 2 == 0, nums) keep?

  • Odd numbers
  • Even numbers (where the function returns True)
  • All numbers
  • Nothing

Answer: Even numbers (where the function returns True). filter keeps only elements for which the function returns True; here that means the even numbers.

What does reduce(lambda a, b: a + b, [1, 2, 3, 4, 5]) return?

  • 15
  • 120

Answer: 15. reduce combines the list into a single value by repeated addition: 1+2+3+4+5 = 15.

What is a higher-order function?

  • A function with many lines
  • A recursive function
  • A function that takes or returns other functions
  • A function defined at module top level

Answer: A function that takes or returns other functions. A higher-order function takes functions as arguments and/or returns functions — core to functional programming.

What does a lambda expression create?

  • A class
  • An anonymous (unnamed) inline function
  • A generator
  • A decorator

Answer: An anonymous (unnamed) inline function. lambda creates a small anonymous function, handy for sorting keys, callbacks, and inline transformations.

What advantage do generators give in functional pipelines?

  • They sort data
  • They run in parallel automatically
  • They validate types
  • Lazy evaluation — memory-efficient, one item at a time

Answer: Lazy evaluation — memory-efficient, one item at a time. Generators yield items lazily, so a chained generator pipeline processes data one element at a time without building large lists in memory.

What does functools.partial do?

  • Splits a function in half
  • Creates a specialized function by pre-filling (freezing) some arguments
  • Runs a function partially
  • Caches results

Answer: Creates a specialized function by pre-filling (freezing) some arguments. partial pre-fills some arguments to produce a new, specialized callable, e.g. square = partial(power, exp=2).

What is currying?

  • Caching function results
  • Running functions concurrently
  • Transforming f(a, b, c) into a chain f(a)(b)(c) of single-argument calls
  • Adding type hints

Answer: Transforming f(a, b, c) into a chain f(a)(b)(c) of single-argument calls. Currying turns a multi-argument function into a sequence of single-argument functions, called like multiply(2)(3)(4).