Functional Programming
Currying and Partial Application
Transforming multi-argument functions into chains of single-argument functions, and freezing arguments with partial application.
Interview: Advanced functional concept — shows understanding of function transformation and composability.
Currying transforms a function that takes multiple arguments into a chain of functions, each taking one argument. Partial application freezes some arguments of a function, producing a new function with fewer parameters. Both enable function composition and code reuse.
Currying vs Partial Application
- Currying:
f(a, b, c)becomesf(a)(b)(c)— always one argument at a time - Partial application:
f(a, b, c)withafixed →f(b, c)— any number of args frozen - Currying produces a chain of single-arg functions; partial produces a function with remaining args
- Python supports partial natively via
functools.partial, currying requires manual implementation
Why Use Them
- Function composition: Single-arg functions compose naturally
- Configuration: Freeze config parameters, reuse the specialized function
- Callbacks: Pre-fill arguments for event handlers
- Point-free style: Define functions without naming intermediate variables
Interview Insight
Know the difference between currying and partial application. In Python, partial is more common than full currying. Be able to implement a curry decorator as a coding exercise.
Use Cases
Configuration — freeze common parameters (currency format, logging level)
Event handlers — pre-fill context for callbacks
Function pipelines — compose single-argument functions
DSL building — create readable APIs with curried builder functions
Mathematical functions — specialize general functions for specific cases
Common Mistakes
Over-currying in Python — partial is usually more Pythonic than full currying
Forgetting that partial freezes positional args left-to-right — use keyword args for flexibility
Not handling *args/**kwargs properly in curry implementations
Creating unreadable chains — currying should improve clarity, not hurt it
Confusing currying (always one arg) with partial (any number frozen)