Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dodie
Last active November 10, 2016 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dodie/112100230fc0cb5730d9800fef04f322 to your computer and use it in GitHub Desktop.
Save dodie/112100230fc0cb5730d9800fef04f322 to your computer and use it in GitHub Desktop.

#FP intro

Lambda calculus, Alonzo Church, '30

JS:

  • functional: Lisp (1958, John McCarthy, fun) -> Scheme (1970, Guy L. Steele, fun) -> JS
  • OO: Smalltalk (1972, Alan Kay, OO) -> Self (1987, David Ungar, OO, prototype) -> JS
  • syntax, API: C -> Java -> JS

Michael Feathers: "OO makes code understandable by encapsulating moving parts. FP makes code understandable by minimizing moving parts."

Declaratív programozási paradigma,

  1. matematikai függvényekkel,
  2. állandó adatstruktúrákkal,
  3. a függvények elsődleges típusokkal.

Mentális modell alapja a kifejezés, nem pedig az utasítás.

1., 2. megkötések:

// pure
const add5 = (a) => a + 5

// impure
let x = 10
const addX = (a) => a + x

// impure
const setX = (v) => x = v

// side effect
let x = 10
x = 5

Előnyei:

  • ha egy függvény eredményét nem használjuk fel sehol, törölhető
  • az eredmény mindig csak a bemeneti paraméterektől függ, mindegy mikor hívjuk (referential transparency):
  • a hívó fél átmozgatható
  • a függvény cachelható (memoization)
  • függvények csak input és output paramétereiken keresztül függhetnek egymástól
  • átmozgathatóak
  • párhuzamosan végrehajthatóak
  • (más kiértékelési stratégiák használata is lehetséges, ha nincs side effect, pl non-strict(lazy))

Azaz könnyebb megérteni, és (unit) tesztelni, debuggolni.

vs. utility class ?! ennél azért többről van szó

3., a kompozícióról szól: Kisebb függvények kombinálásával újabb függvényeket állnak elő. (példa)

  • first class functions
  • higher order functions
  • composition
  • partial application
  • currying

OO design patternek lefedhetőek függvényekkel is, van, hogy egyszerűbban, pl strategy, visitor, adapter.

Lazyness:

const s1 = longOp1()
const s2 = longOp2()
if(s1 !== null && s2 !== null) {/*...*/}
const s1 = (a) => longOp1()
const s2 = (a) => longOp2()
if(s1() !== null && s2() !== null) {/*...*/}

A függvénykompozícióval sok esetben olvashatóbb, egyszerűbb kódot kapunk. (Példa)

Néznivaló

http://underscorejs.org/

http://ramdajs.com/

const add = R.curry((a, b) => a + b)
add(1, 2) // => 3
const add1 = add(1)
add1(2) // => 3
add1(10) // => 11

https://facebook.github.io/immutable-js/

Olvasnivaló

Learn You a Haskell for Great Good! http://learnyouahaskell.com/

Marijn Haverbeke, Eloquent JavaScript http://eloquentjavascript.net/

John Backus, 1977 Turing Award lecture "Can Programming Be Liberated From the von Neumann Style? A Functional Style and its Algebra of Programs": http://worrydream.com/refs/Backus-CanProgrammingBeLiberated.pdf

John Hughes, 1990 Why Functional Programming Matters https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf

Rich Hickey, 2009 Are we there yet? https://www.infoq.com/presentations/Are-We-There-Yet-Rich-Hickey

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment