functional programming techniques in regular javascript

101
FUNCTIONAL PROGRAMMING TECHNIQUES IN REGULAR JAVASCRIPT

Upload: pavel-klimiankou

Post on 13-Apr-2017

304 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Functional programming techniques in regular JavaScript

FUNCTIONAL PROGRAMMING TECHNIQUES

IN REGULAR JAVASCRIPT

Page 2: Functional programming techniques in regular JavaScript

ABOUT ME

Pavel KlimenkovSenior Software Developer @ Geotab Inc.Oakville, ON, Canada

Page 3: Functional programming techniques in regular JavaScript

WHAT THIS TALK IS ABOUT

• Pick few ideas from functional programming

• Simplify as much as possible

• Apply in JavaScript

• PROFIT!

Page 4: Functional programming techniques in regular JavaScript

WHAT THIS TALK IS ABOUT

• Pick few ideas from functional programming

• Simplify as much as possible

• Apply in JavaScript

• PROFIT!

Page 5: Functional programming techniques in regular JavaScript

WHAT THIS TALK IS ABOUT

• Pick few ideas from functional programming

• Simplify as much as possible

• Apply in JavaScript

• PROFIT!

Page 6: Functional programming techniques in regular JavaScript

WHAT THIS TALK IS ABOUT

• Pick few ideas from functional programming

• Simplify as much as possible

• Apply in JavaScript

• PROFIT!

Page 7: Functional programming techniques in regular JavaScript

WHAT THIS TALK ISN’T ABOUT

• OOP must die, FP will save the world

• Detailed FP manual

• “A monad is just a monoid in the category of endofunctors”

Page 8: Functional programming techniques in regular JavaScript

WHAT THIS TALK ISN’T ABOUT

• OOP must die, FP will save the world

• Detailed FP manual

• “A monad is just a monoid in the category of endofunctors”

Page 9: Functional programming techniques in regular JavaScript

WHAT THIS TALK ISN’T ABOUT

• OOP must die, FP will save the world

• Detailed FP manual

• “A monad is just a monoid in the category of endofunctors”

Page 10: Functional programming techniques in regular JavaScript

AGENDA•Immutability

•Simple functions

•Not that simple functions

•Combining OOP and FP

•Functors

•Monads

Page 11: Functional programming techniques in regular JavaScript

WHAT IS FP?

It’s a declarative programming style where all functions have no side effects and data is readonly.

BTW, SQL is also declarative

Page 12: Functional programming techniques in regular JavaScript

1. IMMUTABILITYObjects don’t change their state

Values are constants

Page 13: Functional programming techniques in regular JavaScript

IMMUTABILITY

• Use only read-only data

• If something needs to be changed - create new instance instead

Page 14: Functional programming techniques in regular JavaScript

EXAMPLE

Convert GPS coordinates to screen points

Page 15: Functional programming techniques in regular JavaScript

EXAMPLE: SOURCE DATA

Page 16: Functional programming techniques in regular JavaScript

EXAMPLE: CONVERSION

Page 17: Functional programming techniques in regular JavaScript

EXAMPLE: MUTABLE DATA

Page 18: Functional programming techniques in regular JavaScript

EXAMPLE: IMMUTABLE DATA

Page 19: Functional programming techniques in regular JavaScript

EXAMPLE

Edit Entity

Page 20: Functional programming techniques in regular JavaScript

EXAMPLE: ENTITY CONSTRUCTOR

Page 21: Functional programming techniques in regular JavaScript

EXAMPLE: MUTABLE DATA

Page 22: Functional programming techniques in regular JavaScript

EXAMPLE: IMMUTABLE DATA

Page 23: Functional programming techniques in regular JavaScript

HOW TO REDUCE TEMPTATION

• Use const or let instead of var

• filter/map/reduce instead of for

• Object.freeze - to prevent object mutation

Page 24: Functional programming techniques in regular JavaScript

IMMUTABILITY: PROS• Simpler code

• Less bugs

• Atomic object construction (object is either fully initialised, or doesn’t exist)

• Less temporal coupling (when order of initialisation matters)

• Simpler caching

• Thread safety*

Page 25: Functional programming techniques in regular JavaScript

IMMUTABILITY: CONS

• Higher CPU impact (more things to do)

• Higher memory impact (and more GC)

• Higher brain impact (OOP often comes with state)

Page 26: Functional programming techniques in regular JavaScript

2. SIMPLE FUNCTIONSFirst class functions

Pure functions

Page 27: Functional programming techniques in regular JavaScript

FIRST CLASS FUNCTIONS

Can be passed as arguments

Can be assigned as values

Page 28: Functional programming techniques in regular JavaScript

PURE FUNCTIONS• No side-effects

• Depend on arguments only (deterministic)

Page 29: Functional programming techniques in regular JavaScript

EXAMPLE: SIDE-EFFECTS

Page 30: Functional programming techniques in regular JavaScript

EXAMPLE: NONDETERMINISTIC FUNCTION

Page 31: Functional programming techniques in regular JavaScript

EXAMPLE: DETERMINISTIC FUNCTION

Page 32: Functional programming techniques in regular JavaScript

SIMPLE FUNCTIONS: PROS• No side-effects

• Less bugs

• Simpler testing

• Simpler code

• Absolute thread safety

Page 33: Functional programming techniques in regular JavaScript

SIMPLE FUNCTIONS: CONS

• No side-effects

• Our job is to create side-effects

• IO and Cache are side-effects

Page 34: Functional programming techniques in regular JavaScript

3. NOT THAT SIMPLE FUNCTIONSFunctions composition

Functions currying

Page 35: Functional programming techniques in regular JavaScript

FUNCTIONS COMPOSITION

Composition - gluing functions together

Page 36: Functional programming techniques in regular JavaScript

EXAMPLE: COMPOSITION

Page 37: Functional programming techniques in regular JavaScript

EXAMPLE: COMPOSITION

Page 38: Functional programming techniques in regular JavaScript

EXAMPLE: COMPOSITION

Two temporary arrays. Oh, the horror!

Page 39: Functional programming techniques in regular JavaScript

EXAMPLE: COMPOSITION

One temporary array. Harmony restored.

Page 40: Functional programming techniques in regular JavaScript

EXAMPLE: MORE COMPOSITION

Page 41: Functional programming techniques in regular JavaScript

EXAMPLE: MORE COMPOSITION

Page 42: Functional programming techniques in regular JavaScript

FUNCTIONS CURRYING

Currying - partial application of a function.

Page 43: Functional programming techniques in regular JavaScript

EXAMPLE: CURRYING

Page 44: Functional programming techniques in regular JavaScript

EXAMPLE: CURRYING

Page 45: Functional programming techniques in regular JavaScript

EXAMPLE: CURRYING

Page 46: Functional programming techniques in regular JavaScript

EXAMPLE: CURRYING

Page 47: Functional programming techniques in regular JavaScript

EXAMPLE: CURRYING

Page 48: Functional programming techniques in regular JavaScript

EXAMPLE: CURRYING

Page 49: Functional programming techniques in regular JavaScript

NOT THAT SIMPLE FUNCTIONS: PROS• Flexibility in using and reusing functions

• Functions tend to be smaller

• Memoization (“Simple functions” PROS list was already too long)

Page 50: Functional programming techniques in regular JavaScript

NOT THAT SIMPLE FUNCTIONS: CONS• Sometimes code looks cryptic

• Might introduce negative performance impact

Page 51: Functional programming techniques in regular JavaScript

COMBINING OOP AND FPShell - OOP, IO, side-effects

Core - FP, logic, pure functions

Core

Shell

Page 52: Functional programming techniques in regular JavaScript

MAIN PRINCIPLES• Imperative shell

• Deals with IO

• Thin layer

Page 53: Functional programming techniques in regular JavaScript

MAIN PRINCIPLES• Functional core

• deals with immutable data and pure functions

• contains application logic

Page 54: Functional programming techniques in regular JavaScript

EXAMPLE: MOVING VEHICLES

Page 55: Functional programming techniques in regular JavaScript

EXAMPLE: MOVING VEHICLES

IO, shell

Page 56: Functional programming techniques in regular JavaScript

EXAMPLE: MOVING VEHICLES

Logic, pure functions,

core

Page 57: Functional programming techniques in regular JavaScript

SHELL/CORE: PROS

• Main bugs will come from the shell, which is small

• Core is easy to test. Unit tests should be enough

• Shell is the one that needs integration tests (shell is small!)

• Command query responsibility segregation out of the box

Page 58: Functional programming techniques in regular JavaScript

SHELL/CORE: CONS• Anything?

Page 59: Functional programming techniques in regular JavaScript

FUNCTORSSmart containers for values

Page 60: Functional programming techniques in regular JavaScript

EXAMPLE: INCREMENTING A PICTURE

Page 61: Functional programming techniques in regular JavaScript

EXAMPLE: INCREMENTING A PICTURE

Page 62: Functional programming techniques in regular JavaScript

EXAMPLE: INCREMENTING A PICTURE

Page 63: Functional programming techniques in regular JavaScript

EXAMPLE: INCREMENTING A PICTURE

Page 64: Functional programming techniques in regular JavaScript

EXAMPLE: INCREMENTING A PICTURE

??? 3.png ???

Page 65: Functional programming techniques in regular JavaScript

FUNCTOR• Is a container around some data,

• that knows how to apply functions to that data,

• keeping result in the container

Page 66: Functional programming techniques in regular JavaScript

FUNCTOR• Container - array, async, nullable - anything

• Some data - numbers, objects, other functors - anything

• Functions are applied through map()

Page 67: Functional programming techniques in regular JavaScript

ARRAY IS A FUNCTOR!

Page 68: Functional programming techniques in regular JavaScript

PROMISE IS ALMOST A FUNCTOR!

Page 69: Functional programming techniques in regular JavaScript

BACK TO EXAMPLE

.png

Page 70: Functional programming techniques in regular JavaScript

USEFUL FUNCTORS• Maybe

• Either

• IO

• Writer

• State

Page 71: Functional programming techniques in regular JavaScript

MAYBE - ALMOST LIKE NULLABLE

Page 72: Functional programming techniques in regular JavaScript

MAYBE - ALMOST LIKE NULLABLE*

Page 73: Functional programming techniques in regular JavaScript

MAYBE - ALMOST LIKE NULLABLE

Page 74: Functional programming techniques in regular JavaScript

MAYBE EXAMPLE

Page 75: Functional programming techniques in regular JavaScript

MAYBE EXAMPLE

Page 76: Functional programming techniques in regular JavaScript

EITHER = LEFT ♥ RIGHT

Page 77: Functional programming techniques in regular JavaScript

EITHER = LEFT ♥ RIGHT

Page 78: Functional programming techniques in regular JavaScript

EITHER = LEFT ♥ RIGHT

Page 79: Functional programming techniques in regular JavaScript

EITHER = LEFT ♥ RIGHT

Page 80: Functional programming techniques in regular JavaScript

EITHER = LEFT ♥ RIGHT

Page 81: Functional programming techniques in regular JavaScript

EITHER = LEFT ♥ RIGHT

Page 82: Functional programming techniques in regular JavaScript

EITHER ♥ RAILWAY ORIENTED

PROGRAMMING

Right

Left

f(x) g(x) h(x)

Picture and concept are shamelessly borrowed from here

Page 83: Functional programming techniques in regular JavaScript

TO BE HONEST…

Traditional Maybe = Just ♥ Nothing

Page 84: Functional programming techniques in regular JavaScript

OTHER FUNCTORS

• IO - for… IO

• Writer - for logs

• State - for… state

Page 85: Functional programming techniques in regular JavaScript

FUNCTORS: PROS• Convenient control over computation

• Logic and context separation

Page 86: Functional programming techniques in regular JavaScript

FUNCTORS: CONS• Steep learning curve

• Temptation to use functors for everything

Page 87: Functional programming techniques in regular JavaScript

SKIPPED CHAPTERS• Applicatives

• Monoids

Page 88: Functional programming techniques in regular JavaScript

MONADS

Page 89: Functional programming techniques in regular JavaScript

MONADS ARE CURSED: EITHER YOU DON’T GET THEM, OR YOU CANT EXPLAIN THEM

Page 90: Functional programming techniques in regular JavaScript

EXAMPLE: NESTED ARRAYS

Page 91: Functional programming techniques in regular JavaScript

EXAMPLE: NESTED MAYBE

Page 92: Functional programming techniques in regular JavaScript

EXAMPLE: NESTED MAYBE

Page 93: Functional programming techniques in regular JavaScript

MAYBE WILL BECOME A MONAD IF

Page 94: Functional programming techniques in regular JavaScript

MAYBE WILL BECOME A MONAD IF

Page 95: Functional programming techniques in regular JavaScript

MAYBE WILL BECOME A MONAD IF

Page 96: Functional programming techniques in regular JavaScript

NOW YOU KNOW KUNG FU MONADS

Whoa!

Page 97: Functional programming techniques in regular JavaScript

ARRAY WILL BECOME A MONAD IF

ECMAScript gets flatten or flatMap for arrays (soon)

Page 98: Functional programming techniques in regular JavaScript

BTW, PROMISE IS ALMOST A MONAD

Page 99: Functional programming techniques in regular JavaScript
Page 100: Functional programming techniques in regular JavaScript

SUMMARY• Immutability - everything is read-only, less bugs

• Pure functions - no side-effects, less bugs

• Composition, currying - reusing functions

• Functors - separating context and logic

• Monads - smart functors

Page 101: Functional programming techniques in regular JavaScript

THANK YOU!

DotsAndBrackets.com

/pasha.klimenkov

/in/pavelklimenkov

slideshare.com/pashaklimenkov

Questions?