Files
2nd/10_Wiki/Topics/Functional_Programming.md
T
2026-05-10 22:08:15 +09:00

4.6 KiB

id, title, category, status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, verification_status, tags, raw_sources, last_reinforced, github_commit, tech_stack
id title category status canonical_id aliases duplicate_of source_trust_level confidence_score verification_status tags raw_sources last_reinforced github_commit tech_stack
wiki-2026-0508-functional-programming Functional Programming 10_Wiki/Topics verified self
FP
Functional Paradigm
none A 0.9 applied
functional
paradigm
immutability
2026-05-10 pending
language framework
Haskell/Scala/F# 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
add :: Int -> Int -> Int
add x y = x + y  -- always same result, no side effect

Map / Filter / Reduce

// 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 3
def add(x: Int)(y: Int): Int = x + y
val addFive = add(5)  // partially applied
addFive(10)  // 15

Function composition

-- Haskell
process = show . (*2) . (+1)
-- process 5 = "12"

Algebraic Data Types

// 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
val result = for {
  user <- findUser(id)        // Option[User]
  email <- user.email          // Option[String]
  domain <- extractDomain(email) // Option[String]
} yield domain

Persistent data structure

;; 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 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

🤖 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