Part 4 of 7 · 7-minute read. My learning notes on the main programming paradigms, with Dart examples.
Concept
Functional programming treats computation as the evaluation of mathematical functions. It emphasizes immutability (data that doesn't change) and avoids side effects (changes to state outside the function).
Real-Life Analogy
Think of a vending machine for drinks. You put in money and a selection code, and you get a drink out. The machine doesn't remember your previous purchases or change its behavior based on past interactions. Each transaction is independent:
- Input: money + selection
- Output: drink
- No side effects: The machine's core function doesn't change based on history
Dart Example
void main() {
// Functional approach to calculate sum of squares
List<int> numbers = [1, 2, 3, 4, 5];
// Using functional methods: map and reduce
int sum = numbers
.map((n) => n * n) // Transform each number to its square
.reduce((a, b) => a + b); // Combine all squares into sum
// Note: reduce() throws on an empty list – use .fold(0, (a, b) => a + b) when the list may be empty
print('Sum of squares: $sum'); // Output: 55
// Pure function example
int multiply(int a, int b) {
return a * b; // Always returns same output for same input
}
// Higher-order function (function that takes functions as parameters)
List<int> applyOperation(List<int> numbers, int Function(int) operation) {
return numbers.map(operation).toList();
}
List<int> doubled = applyOperation(numbers, (n) => n * 2);
print('Doubled: $doubled'); // [2, 4, 6, 8, 10]
// Immutability example
final originalList = [1, 2, 3];
final newList = [...originalList, 4]; // Create new list instead of modifying
print('Original: $originalList'); // [1, 2, 3] - unchanged
print('New: $newList'); // [1, 2, 3, 4]
// Note: `final` only prevents reassignment – the list itself can still be changed.
// For a truly immutable list use a const list or List.unmodifiable(...).
}
// Pure function - no side effects
int calculateDiscount(int price, double discountRate) {
return (price * (1 - discountRate)).round();
}
// Impure function - has side effects (modifies external state)
int counter = 0;
void incrementCounter() {
counter++; // Side effect: modifies external variable
}
Functional Programming Pipeline
╔═══════════════════════════════════════════════════════════╗
║ FUNCTIONAL TRANSFORMATION PIPELINE ║
╚═══════════════════════════════════════════════════════════╝
Input Data: [1, 2, 3, 4, 5]
│
│ map(n => n²)
▼
┌─────────────────────┐
│ TRANSFORM │
│ ─────────────── │
│ [1, 4, 9, 16, 25] │ ← Each element squared
└──────────┬──────────┘
│
│ reduce((a, b) => a + b)
▼
┌─────────────────────┐
│ AGGREGATE │
│ ─────────────── │
│ 1 + 4 = 5 │
│ 5 + 9 = 14 │
│ 14 + 16 = 30 │
│ 30 + 25 = 55 │
└──────────┬──────────┘
│
▼
Result: 55
Pure functions → Predictable results → No side effects
Pure vs Impure Functions
╔═══════════════════════════════════════════════════════════════╗
║ PURE FUNCTION ║
╠═══════════════════════════════════════════════════════════════╣
║ ║
║ int add(int a, int b) { ║
║ return a + b; // Same input → Same output ✓ ║
║ } // No side effects ✓ ║
║ ║
║ Properties: ║
║ ✓ Deterministic (100% predictable) ║
║ ✓ No side effects (doesn't change external state) ║
║ ✓ Easy to test (input → output, that's it!) ║
║ ✓ Can be cached (memoization works perfectly) ║
║ ✓ Thread-safe (parallel execution friendly) ║
║ ║
║ Example: add(2, 3) ALWAYS returns 5 ║
╚═══════════════════════════════════════════════════════════════╝
╔═══════════════════════════════════════════════════════════════╗
║ IMPURE FUNCTION ║
╠═══════════════════════════════════════════════════════════════╣
║ ║
║ int counter = 0; ║
║ void increment() { ║
║ counter++; // Modifies external state ✗ ║
║ print(counter); // Has side effect (I/O) ✗ ║
║ } ║
║ ║
║ Issues: ║
║ ✗ Unpredictable results (depends on external state) ║
║ ✗ Hard to test (need to set up external state) ║
║ ✗ Can cause bugs in concurrent code (race conditions) ║
║ ✗ Difficult to reason about (hidden dependencies) ║
║ ✗ Cannot be safely cached ║
║ ║
║ Example: increment() returns different values each time ║
╚═══════════════════════════════════════════════════════════════╝
Immutability Concept
╔═══════════════════════════════════════════════════════════════╗
║ MUTABLE vs IMMUTABLE ║
╚═══════════════════════════════════════════════════════════════╝
Mutable Approach (Imperative):
┌──────────────────┐
│ List: [1, 2, 3] │ ← Original object
│ Memory: 0x1000 │
└────────┬─────────┘
│ .add(4) ← Modifies in place
▼
┌──────────────────┐
│ List: [1,2,3,4] │ ← Same object, changed!
│ Memory: 0x1000 │ ← Same memory address
└──────────────────┘
Problem: Other references are affected!
If someone else had a reference to this list,
they'll see the change unexpectedly.
Immutable Approach (Functional):
┌──────────────────┐
│ List: [1, 2, 3] │ ─────────► Still [1, 2, 3] ✓
│ Memory: 0x1000 │ Original unchanged!
└────────┬─────────┘
│ [...list, 4] ← Creates new
▼
┌──────────────────┐
│ New: [1,2,3,4] │ ← New object created ✓
│ Memory: 0x2000 │ ← Different memory address
└──────────────────┘
Benefit: Predictable, safe, no surprises!
Original data is preserved, easier to reason about.
Higher-Order Functions
╔═══════════════════════════════════════════════════════════════╗
║ HIGHER-ORDER FUNCTIONS ║
╚═══════════════════════════════════════════════════════════════╝
A Higher-Order Function is a function that:
1. Takes functions as parameters, OR
2. Returns a function as a result
┌─────────────────────────────────────────────────────────┐
│ Higher-Order Function Example │
│ │
│ applyOperation(numbers, operation) │
│ │ │ │
│ Data Function │
│ to process to apply │
└─────────┬───────────────┬──────────────────────────────┘
│ │
▼ ▼
[1,2,3,4,5] (n) => n * 2
│ │
└───────┬───────┘
│
▼
┌───────────────┐
│ Apply to │
│ all elements │
└───────┬───────┘
│
▼
[2, 4, 6, 8, 10]
Benefits:
✓ Code reusability (same function, different operations)
✓ Abstraction (separate "what to do" from "how to do it")
✓ Composability (combine functions to build complex logic)
Example operations you can pass:
• (n) => n * 2 (double)
• (n) => n * n (square)
• (n) => n + 10 (add 10)
Functional programming shines in data processing and transformation tasks. Its emphasis on pure functions and immutability makes code more predictable, testable, and suitable for parallel execution.
Further Reading
Functional Programming:
- Functional Programming in Dart - Official guidelines
- Functional Programming Principles - Core concepts
- Why Functional Programming Matters - Classic paper
Dart Functional Features:
- Iterable Collections - map, where, reduce
- Functions in Dart - First-class functions
Advanced Topics:
- Immutability in Practice - Benefits and techniques
- Function Composition - Building complex functions
Books:
- Functional Programming in JavaScript - Practical approach
- Professor Frisby's Mostly Adequate Guide to FP - Free online book
Originally published at projekt.github.io/articles. I'm Srđan Ljuština — I build small, privacy-friendly apps at srdapp.rs. Find me on LinkedIn and GitHub.
Top comments (0)