Functional Programming with JavaScript
-
Upload
aung-zan-baw -
Category
Technology
-
view
88 -
download
0
Transcript of Functional Programming with JavaScript
![Page 1: Functional Programming with JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022070509/589ea2691a28ab38288b464f/html5/thumbnails/1.jpg)
Functional ProgrammingWith JavaScript
![Page 2: Functional Programming with JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022070509/589ea2691a28ab38288b464f/html5/thumbnails/2.jpg)
In the beginning, God created “Programming paradigms” in 7 days (Genesis 101)👼
Imperative Programming
Procedure Programming
Object Oriented Programming
Functional Programming
~ a coding style / a way of thinking
![Page 3: Functional Programming with JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022070509/589ea2691a28ab38288b464f/html5/thumbnails/3.jpg)
တတေတာတေ�ာငတ
![Page 4: Functional Programming with JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022070509/589ea2691a28ab38288b464f/html5/thumbnails/4.jpg)
တေးတေ�တာပ ဘာထးလလ
Q: Don’t we write functions in every language ?
A: But functional programming take it to a whole another level
Def : Break any complex problem down into smaller sub-problems, solve them using functions, and finally combine them together to solve the bigger problem.
![Page 5: Functional Programming with JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022070509/589ea2691a28ab38288b464f/html5/thumbnails/5.jpg)
ဘယသတေတ�သ�းလ လ
F#, C# (.NETs)
Lisp & Perl (grandpas)
Erlang , Elixir & Haskell (hotshots)
Clojure & Scala (JVMs)
D, R, JS, Python, … (sexies)
![Page 6: Functional Programming with JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022070509/589ea2691a28ab38288b464f/html5/thumbnails/6.jpg)
Why FP in JavaScript
The root of all evil is “this” keyword 😈Object.prototype
OOP in JS is like sugar coated paracetamol 💊
.call .apply .bind // anyone ?
![Page 7: Functional Programming with JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022070509/589ea2691a28ab38288b464f/html5/thumbnails/7.jpg)
Input => process => output
f(x) = x * x // function (x) { return x * x }
g(f(x))
“ Programmer think of their programs more as pipes for data to travel through just like we did in Math “
Source ~ wellesley.edu
![Page 8: Functional Programming with JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022070509/589ea2691a28ab38288b464f/html5/thumbnails/8.jpg)
Imperative
let name = “azb”
let greeting = “Hello, I’m ”
console.log(greeting + name)
function greeting(name){
return “Hello, I’m ” + name
}
greeting(“azb”)
Functional
![Page 9: Functional Programming with JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022070509/589ea2691a28ab38288b464f/html5/thumbnails/9.jpg)
First-class citizen အခ�ငထးခ လတ�းစားTreat like other values
Pass around arguments and return as value
Define and manipulate functions from other functions
Reference from variable or self
![Page 10: Functional Programming with JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022070509/589ea2691a28ab38288b464f/html5/thumbnails/10.jpg)
Side Effects
Changing the value of a variable (parent/ global), OBVIOUSLY
Writing some data to disk, file, console, network
Manipulating of UI
Calling external process
Generally - STATE CHANGED
![Page 11: Functional Programming with JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022070509/589ea2691a28ab38288b464f/html5/thumbnails/11.jpg)
Pure functions (no side effects)
![Page 12: Functional Programming with JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022070509/589ea2691a28ab38288b464f/html5/thumbnails/12.jpg)
High-order function (return)
![Page 13: Functional Programming with JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022070509/589ea2691a28ab38288b464f/html5/thumbnails/13.jpg)
High-order function (accept)
![Page 14: Functional Programming with JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022070509/589ea2691a28ab38288b464f/html5/thumbnails/14.jpg)
Referential transparency
function add(a, b) { return a + b }
add(add(2, 3), add(4, 1))
add((2 + 3), add(4, 1))
add((2 + 3), (4 + 1))
(2 + 3) + (4 + 1)
![Page 15: Functional Programming with JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022070509/589ea2691a28ab38288b464f/html5/thumbnails/15.jpg)
Don’t LOOP (stream of data / HO funs)
.forEach
.map
.reduce
.filter
![Page 16: Functional Programming with JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022070509/589ea2691a28ab38288b464f/html5/thumbnails/16.jpg)
Immutability is bad
.pop .push
.shift .unshift
let a = 1
a = “hello world”
![Page 17: Functional Programming with JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022070509/589ea2691a28ab38288b464f/html5/thumbnails/17.jpg)
Persistent data structures
var, let, const right ? ahuh not really
Wait a min Object.freeze Object.seal
MORI
IMMUTABLE (sponsored by facebook)
Underscore
Lodash
Ramda
![Page 18: Functional Programming with JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022070509/589ea2691a28ab38288b464f/html5/thumbnails/18.jpg)
More FP
Currying
Tail Recursion
Pattern matching
Infinite data structure
Composition
Lazy evaluation
self.Q&A
Concurrency
Parallelism
Async
![Page 19: Functional Programming with JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022070509/589ea2691a28ab38288b464f/html5/thumbnails/19.jpg)
READABLE, EASY TO UNDERSTAND, REUSABLE
Writing with a larger team
Application larger than a todo app
Performance are not critical
You have to support the project
P.S
1. I/O is tricky in FP
2. Code LineNums are just a number
3. There’s no single true paradigm
![Page 20: Functional Programming with JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022070509/589ea2691a28ab38288b464f/html5/thumbnails/20.jpg)
Aung Zan Baw - တေအာငဇတေဘာWeb developer (Fairway Technology)
![Page 21: Functional Programming with JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022070509/589ea2691a28ab38288b464f/html5/thumbnails/21.jpg)
Links
Slides - https://goo.gl/3fmMXk
Best FP in JS - https://goo.gl/IqmkoT
ReactiveX !!!
Starter book - https://goo.gl/ctmBTo