23 cards · Public · Shuffle

Functional Programming - Immutability, Pure Functions & Higher-Order Functions

Unlock the power of Functional Programming! Master immutability, pure functions, and higher-order functions to write predictable, testable, and concurrent code with this comprehensive flashcard deck.

Cards in this deck

(23 cards)

Preview terms and definitions before starting your study session.

#1
Term
What is Functional Programming (FP)?
Definition
A programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing state and mutable data. It emphasizes immutability, pure functions, and declarative code.
#2
Term
Define 'Immutability' in Functional Programming.
Definition
Immutability means that data, once created, cannot be changed. Instead of modifying existing data, new data is created with the desired changes, leaving the original data untouched.
#3
Term
Why is Immutability important in FP?
Definition
It prevents unexpected side effects, makes code easier to reason about, simplifies concurrent programming by eliminating race conditions, and enhances predictability and testability.
#4
Term
Provide a JavaScript example of immutable array manipulation.
Definition
Instead of arr.push(4) (mutates arr), use const newArr = [...arr, 4]; or const newArr = arr.concat(4);. For objects, const newObj = { ...oldObj, key: 'newValue' }; creates a new object without modifying oldObj.
#5
Term
What is a 'Pure Function'?
Definition
A function that satisfies two conditions:
1. Deterministic: Given the same input, it will always return the same output.
2. No Side Effects: It produces no observable changes to the system outside its local scope (e.g., modifying global state, I/O operations, changing arguments).
#6
Term
Give an example of a pure function in Python.
Definition
python2 lines
def add(a, b):
    return a + b
This function always returns the same output for the same inputs and has no side effects.
#7
Term
Give an example of an impure function in JavaScript and explain why it's impure.
Definition
javascript5 lines
let total = 0;
function addToTotal(num) {
    total += num; // Side effect: modifies external state
    return total;
}
This is impure because it modifies a variable outside its scope (total) and its output depends on the external total's current value, not just its inputs.
#8
Term
What is 'Referential Transparency'?
Definition
An expression is referentially transparent if it can be replaced with its corresponding value without changing the program's behavior. Pure functions are inherently referentially transparent.
Showing 8 of 23 cards in this deck.
Study All Now