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).