Think functionally f(x) = y Presenters Pavel Struhar - - PowerPoint PPT Presentation

think functionally
SMART_READER_LITE
LIVE PREVIEW

Think functionally f(x) = y Presenters Pavel Struhar - - PowerPoint PPT Presentation

Think functionally f(x) = y Presenters Pavel Struhar pavel.struhar@accenture.com Twitter: @pavestru Daniel Derevjanik daniel.derevjanik@accenture.com Twitter: @dderevjanik Both from Lightweight Arch. core team Spoiler Everything is a


slide-1
SLIDE 1
slide-2
SLIDE 2

Think functionally

f(x) = y

slide-3
SLIDE 3

Presenters

Pavel Struhar pavel.struhar@accenture.com Twitter: @pavestru Daniel Derevjanik daniel.derevjanik@accenture.com Twitter: @dderevjanik Both from Lightweight Arch. core team

slide-4
SLIDE 4
slide-5
SLIDE 5

Spoiler

Everything is a function

slide-6
SLIDE 6
slide-7
SLIDE 7

A monad is just a monoid in the category of endofunctors.

What's the problem?

slide-8
SLIDE 8

Data and Functions

slide-9
SLIDE 9

Data Flow - Expectations f

INPUT DATA OUTPUT DATA

slide-10
SLIDE 10

Data Flow - Expectations f

INPUT DATA OUTPUT DATA PROCESS DATA

slide-11
SLIDE 11

Data Flow - Expectations f

INPUT DATA OUTPUT DATA

slide-12
SLIDE 12

Simplicity is a key

slide-13
SLIDE 13

Data Flow - Expectations f f f

INPUT DATA

f f

OUTPUT DATA

slide-14
SLIDE 14

Data Flow - Expectations f f f

INPUT DATA

f f

OUTPUT DATA PROCESS DATA

slide-15
SLIDE 15

Data Flow - Expectations f f f

INPUT DATA

f f

OUTPUT DATA

slide-16
SLIDE 16

Simplicity is a key, still...

slide-17
SLIDE 17

Data Flow - Reality

INPUT DATA

class class class method method method method method method

CLASS DATA CLASS DATA CLASS DATA

slide-18
SLIDE 18

Data Flow - Reality - OOP in nutshell

slide-19
SLIDE 19

Simplicity ? Errr….

slide-20
SLIDE 20

What can we achieve ?

slide-21
SLIDE 21

What can we achieve ?

  • one directional dataflow

f

INPUT DATA

f f

OUTPUT DATA

slide-22
SLIDE 22

How can we achieve that ?

slide-23
SLIDE 23

Separation of Data and Functions

slide-24
SLIDE 24

No Hidden Information

slide-25
SLIDE 25

No Side-effects

slide-26
SLIDE 26

Simple is Pure Function

slide-27
SLIDE 27

Pure Function

Function without time, (Always the same output for the same arguments.)

slide-28
SLIDE 28

Pure Function is Timeless

slide-29
SLIDE 29

Pure Function has No side-effects

slide-30
SLIDE 30

Pure Function is Opposite to Class Method

No this keyword

slide-31
SLIDE 31

Why should you care about pure functions ?

slide-32
SLIDE 32

Pure functions advantages

  • Easier to maintain
  • Testability
  • Easy to debug
  • Simply reusable
  • Thread-safe
  • One-directional data-flow
slide-33
SLIDE 33

Time for Demo

slide-34
SLIDE 34

Immutability

slide-35
SLIDE 35

Immutability

default in Functional languages

slide-36
SLIDE 36

Immutability by default in JavaScript?

slide-37
SLIDE 37

ECMAScript 6 introduced let keyword

slide-38
SLIDE 38

ECMAScript 6 introduced let keyword also introduced const

const

slide-39
SLIDE 39

Real life: Code reviews

slide-40
SLIDE 40
slide-41
SLIDE 41
slide-42
SLIDE 42
slide-43
SLIDE 43
slide-44
SLIDE 44

Immutable Data Structures FTW

slide-45
SLIDE 45

Immutable building blocks

  • Records
  • Vectors
  • Maps
  • Sets

… build anything you want

slide-46
SLIDE 46

Tree data structure

slide-47
SLIDE 47

We want to change one node

slide-48
SLIDE 48
slide-49
SLIDE 49
slide-50
SLIDE 50
slide-51
SLIDE 51
slide-52
SLIDE 52
slide-53
SLIDE 53
slide-54
SLIDE 54

...or keep it for the history record

slide-55
SLIDE 55

Immutability in Java

slide-56
SLIDE 56

Practices for immutability in Java (make them habits)

  • Mark the class final
  • Mark all the fields private and final
  • Force all the callers to construct an object of the class

directly, i.e. do not use any setter methods

  • Do not change the state of the objects in any methods of the

class

slide-57
SLIDE 57

“Classes should be immutable unless there's a very good reason to make them mutable.... If a class cannot be made immutable, limit its mutability as much as possible.”

  • - Joshua Bloch (taken from the book Effective Java)
slide-58
SLIDE 58

Benefits of immutable objects

  • Thread-safety
  • Easier to parallelize
  • Consistent internal state (in spite of exception)
  • References to immutable objects can be cached

○ Easy to implement UNDO - REDO functionality

slide-59
SLIDE 59

Time for a success story

slide-60
SLIDE 60

Redux.js

  • FLUX-ish library by Dan Abramov

○ Creator of Hot Module Reloading for ReactJS

  • Motivation:

○ We want Hot Module Reloading not only for View Components ○ but also for Business logic inside FLUX stores (like Models in MVC)

slide-61
SLIDE 61

Redux

  • Solution

○ Take state out of Stores ○ Make Store methods immutable / pure functions ○ No classes, just functions - reducers

slide-62
SLIDE 62

Redux

  • Result

○ HMR also for reducers ○ Application keeps state after HMR !!! ○ Time travelling ○ Awesome debugging tools ○ Community excitement - this is how FLUX was supposed to work from the start

slide-63
SLIDE 63
slide-64
SLIDE 64

Summary

slide-65
SLIDE 65

What can we improve in our daily programming ?

slide-66
SLIDE 66

Shared mutable state is the root

  • f all evil.

Pete Hunt, ReactJS team @ Facebook

slide-67
SLIDE 67

Avoid mutability

slide-68
SLIDE 68

Don’t use global scoped variables

slide-69
SLIDE 69

Make const not var

slide-70
SLIDE 70

Separate Data from Business logic

slide-71
SLIDE 71

Use Pure functions

slide-72
SLIDE 72

Learning Haskell will help you write better Java(Script) code

slide-73
SLIDE 73

OOP vs FP ?

slide-74
SLIDE 74
slide-75
SLIDE 75
slide-76
SLIDE 76

Composition over Inheritance

slide-77
SLIDE 77

Topics for future sessions

Functional programming

  • Higher order functions
  • Partial application
  • Currying
  • Closures
  • Functors
  • Monads?
slide-78
SLIDE 78

Topics for future sessions

Functional Reactive Programming

  • ReactiveX (RxJS, RxJava), …

○ underscore / lodash for streams of events / data

  • Observables
  • Reactive (user) interfaces
slide-79
SLIDE 79

Topics for future sessions

Clojure ClojureScript Haskell PureScript Ocaml ReasonML Elm

slide-80
SLIDE 80

Happy building