Functional Programming Python - accidentlywoo/python GitHub Wiki
FunctionalProgrammingPython
open source book λ²μ μ 리
Class ShortCut
forμΈλ 10μ€μ΄μ κ°λ μ± λ¨μ΄μ§λ€.
Functions are first class(objects). That is, everything you can do with "data"="κ°"
꼬리μ¬κ·λ₯Ό μΈμ΄μ체μμ μ κ±°ν΄μ£Όμ§ μλλ€.
'ν¨μλ νκΊΌλ²μ μ²λ¦¬λ₯Ό ν λ μ¬μ©νλ€'-> νλμ©μ²λ¦¬νλ κΈ°λ₯μμ μμ’λ€.
side effect issue
cf) global variable
Python is most definitely not a "pure functional programming language"
Operator -> μ°μ°μλ₯Ό ν¨μλ‘ λ§λ€μ΄μ μ¬μ©.
- itertools
- functools
- operator : λͺ¨λ μ°μ°μλ₯Ό ν¨μλ‘ λ°κΎΌλ€.
higher order function -> μΈμλ₯Ό ν¨μλ‘
ν¨μλ₯Ό μ¬μ©νλ©΄ μ¬μ¬μ© κ°λ₯νλ€. λ©ν°νλ‘μΈμ±ν λμλ μ¬μ©ν μ μλ€.
μ€μ»΄μ λ©΄λλ
λ νμ΄ν
Recursion : νμ΄μ¬μμλ 꼬리ν¨μμ²λ¦¬ μ§μμ ν΄μ£Όμ§ μμμ λ°λ‘ μ²λ¦¬λ₯Ό ν΄μ€μΌνλ€.
Eliminating Loop : forλ¬Έ*μ μμ΄λ€.
Index
- Preface
- (Avoiding) Flow Control
- Callables
- Lazy Evaluation
- Higher-Order Functions
Preface
ν¨μν νλ‘κ·Έλλ°?? μ μλ, λͺκ°μ§ νΉμ§λ€λ‘ ν¨μν νλ‘κ·Έλλ°μ λλ΅μ μΌλ‘ νΉμ§μ§λλ€. ν¨μλ₯Ό νΈμΆνλ μΈμ΄λ λ§λ€λ €λκ²μ μ½κ² λ§λ€κ±°λ λλ μ΄λ ΅κ²λ§λ€κ±°λ, λΆκ°λ₯νκ² λ§λ€ μ μλ€. :
- ν¨μλ 1κΈ κ°μ²΄(class)μ΄λ€. μ΄κ²μ, λͺ¨λ κ²μ ν¨μλ₯Ό μ΄μ©ν΄ "κ°(data)"λ‘ λ§λ€ μ μλ€λ κ²μ΄λ€.
- μ¬κ·λ(λ°λ³΅) κΈ°λ³Έ μ μ΄ κ΅¬μ‘°λ‘ μ¬μ©λλ€. λͺ μΈμ΄λ€μ, λ€λ₯Έ λ°λ³΅λ¬Έ"loop"κ΅¬μ‘°κ° μκΈ°λ νλ€.
νμ΄μ¬μ forλ‘ λ§λ€μ΄μ iterator, generatorλ‘ λ§λ λ€. -> forλ¬Έ μ¬μ©μ μ§μνλ€.
- list μ²λ¦¬(ex. Lisp)μ μ§μ€νλ λΆλΆμ΄ μλ€.리μ€νΈκ΅¬μ‘°λ€μ μ’ μ’ λ°λ³΅λ¬Έμμ ??λ¨?
- = There is a focus on list processing (for example, it is the source of the name Lisp). Lists are often used with recursion on sublists as a substitute for loops.
- "μμ" ν¨μν μΈμ΄λ 'side effects'λ₯Ό ννΌνλ€. 'side effects'λ μ΄λμμλ λ°°μ νλ€
- "Pure" functional languages eschew side effects. This excludes the almost ubiquitous(μ΄λμμ μλ) pattern in imperative(λ°λμ ν΄μΌνλ, κΈ΄μν) languages(imperative language - λͺ λ Ήμ΄) of assigning first one, then another value to the same variable to track the program state
"Pure" functional languages eschew side effects. μμμ±μ μ¬μ΄λ μ΄ν©νΈλ₯Ό νΌνλ€. But νμ΄μ¬μ μμνμ§ μλ€.
- Functional programming either discourages or outright disallows statements, and instead works with the evaluation of expressions (in other word, functions plus arguments). In the pure case, one program is one expression (plus supporting definitions).
- Functional programming worries about what is to be computed rather than how it is to be computed.
- Much functional programming utilizes "higher order" functionas (in other words, functions that operate on functions that operate on functions).