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

  1. Preface
  2. (Avoiding) Flow Control
  3. Callables
  4. Lazy Evaluation
  5. 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).