💻 Programming

Functional Programming

Functional Programming

A paradigm that emphasizes pure functions and immutability. Minimizes side effects. Supported in Haskell, Scala, and Rust.

📖 Detailed Explanation

Functional Programming is a paradigm that structures programs around pure functions and immutable data. It minimizes side effects and treats functions as first-class citizens, so they can be passed to or returned from other functions. This increases the predictability and testability of code.

A Pure Function always returns the same output for the same input and does not modify external state. add(a, b) = a + b is a pure function, but a function that modifies a global variable or performs I/O is not pure. Pure functions are easy to test and debug, and are safe for parallel processing.

Immutability is the principle of creating new data instead of modifying existing data. Rather than modifying an object, you make a copy and change that. JavaScript's spread operator, Python's tuples, and Rust's immutable-by-default variables are examples that support immutability.

A Higher-Order Function is a function that takes a function as an argument or returns a function. map, filter, and reduce are typical examples. Currying is a technique that converts a function taking multiple arguments into a chain of single-argument functions, and is useful for function composition.

💻 Code Example

// JavaScript 함수형 프로그래밍 예제

// 1. 순수 함수
const add = (a, b) => a + b;
const multiply = (a, b) => a * b;

// 2. 불변성 - 원본 수정 대신 새 객체 생성
const user = { name: 'Alice', age: 30 };
const updatedUser = { ...user, age: 31 };  // 불변 업데이트

// 3. 고차 함수 - map, filter, reduce
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(n => n * 2);         // [2, 4, 6, 8, 10]
const evens = numbers.filter(n => n % 2 === 0);  // [2, 4]
const sum = numbers.reduce((acc, n) => acc + n, 0);  // 15

// 4. 함수 합성
const compose = (...fns) => x => fns.reduceRight((acc, fn) => fn(acc), x);
const addOne = x => x + 1;
const double = x => x * 2;
const addOneThenDouble = compose(double, addOne);
console.log(addOneThenDouble(5));  // 12

// 5. 커링
const curry = fn => a => b => fn(a, b);
const curriedAdd = curry(add);
const addFive = curriedAdd(5);
console.log(addFive(3));  // 8

// 6. 명령형 vs 함수형
// 명령형 (Imperative)
let total = 0;
for (let i = 0; i < numbers.length; i++) {
    total += numbers[i];
}

// 함수형 (Declarative)
const totalFP = numbers.reduce((acc, n) => acc + n, 0);

🗣️ Examples of Practical Conversations

Senior: This state management code is complex. Let's refactor it with an immutable update pattern.

Junior: Won't copying every time hurt performance?

Senior: If you use a library like Immer, it does structural sharing internally, so it's efficient. On top of that, tracking state changes becomes much easier when debugging.

Interviewer: What are the advantages of pure functions?

Candidate: They're easy to test. With no external dependencies, you can verify the output just by providing the input. Also, thanks to referential transparency, results can be cached (memoization), and no race conditions occur in parallel processing.

Interviewer: What are the drawbacks of functional programming?

Candidate: There is a learning curve, and it isn't suitable for every situation. In code with a lot of I/O or state management, it's hard to implement with pure functions alone. Mixing the approaches appropriately is the realistic choice.

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

Reviewer: This reduce chain is too long. It hurts readability.

Author: I was trying to write it in a functional style.

Reviewer: Functional style is good, but overdoing it backfires. Split the intermediate steps into meaningfully named variables, or extract complex logic into named functions.

⚠️ Cautions

🔗 Related Terms

📚 Learn More