Master Functional Programming with 50 free flashcards. Study using spaced repetition and focus mode for effective learning in Programming.
Functional programming is a declarative programming paradigm that treats computation as the evaluation of mathematical functions. It emphasizes immutability, pure functions, and avoiding side effects. Languages like Haskell, Erlang, Clojure, and F# are primarily functional.
A pure function is a function that: Always returns the same output for the same input (deterministic)Has no side effects — it doesn't modify external state, perform I/O, or depend on mutable data Example: const add = (a, b) => a + b; is pure, while Math.random() is not.
Side effects are any observable changes a function makes beyond returning a value. Examples include: Modifying a global or external variableWriting to a file or databasePrinting to the consoleMaking network requestsMutating input arguments FP aims to isolate side effects from core logic.
Immutability means that once a data structure is created, it cannot be changed. Instead of modifying data, you create new copies with the desired changes. Benefits include: Easier reasoning about codeThread safety without locksSimpler debugging and testing Example: const newArr = [...arr, newItem]; instead of arr.push(newItem);
Persistent data structures always preserve previous versions of themselves when modified. They use structural sharing to efficiently reuse unchanged portions, avoiding full copies. Libraries like Immutable.js and languages like Clojure provide these. They enable O(log n) updates while maintaining immutability.
In a language with first-class functions, functions are treated as values — they can be: Assigned to variables: const greet = function() {}Passed as arguments to other functionsReturned from other functionsStored in data structures This is a prerequisite for functional programming.
A higher-order function is a function that either: Takes one or more functions as arguments, orReturns a function as its result Examples: map, filter, reduce, sort with a comparator. Example: const double = arr.map(x => x * 2);
A closure is a function that captures and remembers variables from its enclosing lexical scope, even after that scope has finished executing. Example:function makeCounter() { let count = 0; return () => ++count;}
The returned function closes over the count variable.
Lexical scoping (static scoping) means a variable's scope is determined by its position in the source code, not by the runtime call stack. Inner functions can access variables from their enclosing functions. This is the foundation of closures and is used in most modern languages including JavaScript, Python, and Haskell.
A currying transforms a function that takes multiple arguments into a sequence of functions, each taking a single argument. Example:const add = a => b => a + b;add(2)(3); // 5
Named after mathematician Haskell Curry. In Haskell, all functions are automatically curried.
Partial application fixes some arguments of a function, producing a new function with fewer parameters. Unlike currying, it can fix multiple arguments at once. Example:const multiply = (a, b) => a * b;const double = multiply.bind(null, 2);double(5); // 10
Currying transforms a function of N arguments into N nested functions of 1 argument each — it's a structural transformationPartial application fixes some arguments and returns a function expecting the remaining arguments — it's about pre-filling values Currying enables partial application but they are distinct concepts.
Flashcards
Flip to reveal
Focus Mode
Spaced repetition
Multiple Choice
Test your knowledge
Type Answer
Active recall
Learn Mode
Multi-round mastery
Match Game
Memory challenge