160 lines
4.6 KiB
Markdown
160 lines
4.6 KiB
Markdown
---
|
|
id: wiki-2026-0508-functional-programming
|
|
title: Functional Programming
|
|
category: 10_Wiki/Topics
|
|
status: verified
|
|
canonical_id: self
|
|
aliases: [FP, Functional Paradigm]
|
|
duplicate_of: none
|
|
source_trust_level: A
|
|
confidence_score: 0.9
|
|
verification_status: applied
|
|
tags: [functional, paradigm, immutability]
|
|
raw_sources: []
|
|
last_reinforced: 2026-05-10
|
|
github_commit: pending
|
|
tech_stack:
|
|
language: Haskell/Scala/F#
|
|
framework: cats/zio/fp-ts
|
|
---
|
|
|
|
# Functional Programming
|
|
|
|
## 매 한 줄
|
|
> **"매 functions as values, immutability as default."** Lambda calculus (Church 1936)에서 origin, Lisp (1958) → ML → Haskell → modern (Scala 3, F#, Rust influence) 의 lineage. 매 2026 mainstream — React hooks, Rust ownership, TypeScript의 functional patterns가 매 industry default.
|
|
|
|
## 매 핵심
|
|
|
|
### 매 Pure functions
|
|
- 매 input → output 의 deterministic.
|
|
- 매 side-effect 없음 (no I/O, no mutation, no global state).
|
|
- 매 referential transparency — `f(x)` replaceable with its value.
|
|
|
|
### 매 Immutability
|
|
- 매 data 의 not modified — 매 new value created instead.
|
|
- 매 structural sharing (persistent data structures) 의 efficiency 보장.
|
|
- 매 concurrency safety 자동 — 매 lock 의 X.
|
|
|
|
### 매 Higher-order functions
|
|
- 매 functions as arguments / return values.
|
|
- map, filter, fold/reduce 의 compositional building blocks.
|
|
- 매 closures 의 lexical scope capture.
|
|
|
|
### 매 응용
|
|
1. React hooks — 매 functional component의 default.
|
|
2. Spark / Flink — 매 distributed FP for big data.
|
|
3. Rust iterators — 매 zero-cost FP abstractions.
|
|
4. Domain modeling — 매 ADTs (sum/product types) 의 type-safe.
|
|
|
|
## 💻 패턴
|
|
|
|
### Pure function
|
|
```haskell
|
|
-- Haskell
|
|
add :: Int -> Int -> Int
|
|
add x y = x + y -- always same result, no side effect
|
|
```
|
|
|
|
### Map / Filter / Reduce
|
|
```typescript
|
|
// TypeScript
|
|
const nums = [1, 2, 3, 4, 5];
|
|
const result = nums
|
|
.filter(n => n % 2 === 0)
|
|
.map(n => n * n)
|
|
.reduce((acc, n) => acc + n, 0); // 4 + 16 = 20
|
|
```
|
|
|
|
### Currying
|
|
```scala
|
|
// Scala 3
|
|
def add(x: Int)(y: Int): Int = x + y
|
|
val addFive = add(5) // partially applied
|
|
addFive(10) // 15
|
|
```
|
|
|
|
### Function composition
|
|
```haskell
|
|
-- Haskell
|
|
process = show . (*2) . (+1)
|
|
-- process 5 = "12"
|
|
```
|
|
|
|
### Algebraic Data Types
|
|
```rust
|
|
// Rust
|
|
enum Result<T, E> {
|
|
Ok(T),
|
|
Err(E),
|
|
}
|
|
|
|
fn parse(s: &str) -> Result<i32, String> {
|
|
s.parse().map_err(|e: std::num::ParseIntError| e.to_string())
|
|
}
|
|
```
|
|
|
|
### Monad (Option/Maybe)
|
|
```scala
|
|
// Scala
|
|
val result = for {
|
|
user <- findUser(id) // Option[User]
|
|
email <- user.email // Option[String]
|
|
domain <- extractDomain(email) // Option[String]
|
|
} yield domain
|
|
```
|
|
|
|
### Persistent data structure
|
|
```clojure
|
|
;; Clojure
|
|
(def v1 [1 2 3])
|
|
(def v2 (conj v1 4)) ;; v1 unchanged, v2 = [1 2 3 4]
|
|
;; structural sharing — O(log n) update
|
|
```
|
|
|
|
### Lens / optics
|
|
```haskell
|
|
-- Haskell with lens library
|
|
import Control.Lens
|
|
data Person = Person { _name :: String, _age :: Int }
|
|
makeLenses ''Person
|
|
|
|
bob = Person "Bob" 30
|
|
bobOlder = bob & age %~ (+1) -- immutable update
|
|
```
|
|
|
|
## 매 결정 기준
|
|
| 상황 | Approach |
|
|
|---|---|
|
|
| Concurrent / distributed system | FP (immutability 의 free safety) |
|
|
| Domain modeling with invariants | FP + ADTs (Scala/Rust/Haskell) |
|
|
| Performance-critical loop | OOP/imperative (cache-friendly, mutation OK) |
|
|
| UI components | FP (React hooks, Elm) |
|
|
| Data pipelines | FP (Spark, Flink, dplyr) |
|
|
|
|
**기본값**: 매 functional core, imperative shell (Gary Bernhardt) — 매 pure logic + thin I/O boundary.
|
|
|
|
## 🔗 Graph
|
|
- 부모: [[Programming-Paradigms]] · [[Lambda-Calculus]]
|
|
- 변형: [[Pure-Functional]] · [[Functional-Reactive-Programming]] · [[Object-Functional]]
|
|
- 응용: [[React-Hooks]] · [[Spark]] · [[Rust-Iterators]]
|
|
- Adjacent: [[Type-Systems]] · [[Category-Theory]] · [[Immutability]]
|
|
|
|
## 🤖 LLM 활용
|
|
**언제**: domain modeling, parser/interpreter design, concurrent code review, type-driven development.
|
|
**언제 X**: 매 tight numeric loop optimization, low-level hardware drivers (mutation/state inevitable).
|
|
|
|
## ❌ 안티패턴
|
|
- **Premature abstraction**: 매 monad transformer stack을 매 simple problem에 매 over-engineering.
|
|
- **Pure-only dogma**: 매 모든 I/O를 IO monad에 매 wrap — 매 readability 손해.
|
|
- **Recursion-only**: 매 stack overflow 가능 — 매 tail-call optimization 의존.
|
|
|
|
## 🧪 검증 / 중복
|
|
- Verified (Bird & Wadler "Introduction to Functional Programming", Okasaki "Purely Functional Data Structures").
|
|
- 신뢰도 A.
|
|
|
|
## 🕓 Changelog
|
|
| 날짜 | 변경 |
|
|
|---|---|
|
|
| 2026-05-08 | Phase 1 |
|
|
| 2026-05-10 | Manual cleanup — FP fundamentals, ADTs, monads, modern industry use |
|