RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB...

54
FUNCTIONAL PROGRAMMING IN THE WEB WITH ELIXIR AND ELM RODRIGO NONOSE SÃO PAULO, 18. NOVEMBER. 2017.

Transcript of RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB...

Page 1: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

FUNCTIONAL PROGRAMMING IN THE WEB WITH ELIXIR AND ELM

RODRIGO NONOSE

SÃO PAULO, 18. NOVEMBER. 2017.

Page 2: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

rhnonose

RodrigoNonose

Page 3: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo
Page 4: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo
Page 5: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

● OO + Its problems● Introduction to Elm and Elixir● FP concepts + code examples● Runtime

Summary

Page 6: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

"Original" definitionObject Orientation

● Objects contain state● An object can send messages● An object can receive messages

Page 7: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

How it was implemented in mainstream languages

● State and Logic in the same place● State mutability anywhere in the method body● State mutability outside of the module● Temporal coupling● Inheritance doesn't offer what's promised● Passing objects by reference breaks encapsulation

Object Orientation Problems

Page 8: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

Design Patterns● Singleton● Factory● Factory Method● Abstract Factory● Builder● Object Pool● Chain of

Responsibility● Command● Interpreter● Iterator● Mediator

● Prototype● Adapter● Decorator● Bridge● Proxy● Composite● Flyweight● Memento● Strategy● Template Method● Visitor● ...

Page 9: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

Functional Programming

Page 10: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

● Purely functional and statically typed● Expressive, concise and self-documented● Immutability and Referential Transparency● There's no null● There's no undefined function● Interoperable with Javascript● Compiled to Javascript● Forced semantic versioning

Elm

Page 11: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

Elixir

● Functional, dinamically typed● Focus on scalability and maintenability● Fault-tolerant● Extensible● Compiled to Erlang Virtual Machine (BEAM)

Page 12: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

Functional Programming

High-order functions

First-class functions

Recursion

Pure functions

Page 13: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

Pure Functions

Page 14: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

Pure Functions

input do something output

Page 15: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

Pure Functions

input do something output

?

Page 16: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

Pure FunctionsJavascript

Page 17: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

Pure FunctionsElm

Page 18: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

Pure FunctionsElixir

Page 19: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

Recursion

Page 20: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

RecursionJava

Page 21: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

RecursionElm

Page 22: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

RecursionElixir

Page 23: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

Recursion

Page 24: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

First-class Functions

Page 25: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

First-class FunctionsElm

Page 26: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

First-class FunctionsElixir

Page 27: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

High-order Functions

Page 28: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

High-order FunctionsJava

Page 29: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

High-order FunctionsJava

Page 30: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

High-order FunctionsElixir

Page 31: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

High-order Functions

● Map● Reduce● Filter● Find● Split

● Count● Sum● Reject● Min● Max

Page 32: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

High-order FunctionsJava 8

Page 33: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

Functional Programming (bonus)

Pattern matching

Piping

Partial application

Immutability

Page 34: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

Partial ApplicationElm

Page 35: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

PipingElixir

Page 36: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

PipingElixir

Page 37: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

Pattern MatchingElm

Page 38: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

Pattern MatchingElixir

Page 39: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

Immutability

Page 40: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

OOP vs FP

● More abstractions● Requires discipline

and training● Hidden and mutable

state● Dependent on state

● Less abstractions● Easier to program

without too much experience

● Exposed and immutable state

● Independent on state

Page 41: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

Runtime

Page 42: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

Elm Architecture

Page 43: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

Model:● State of the application● Initial state definition is

mandatory● Leverages Elm types (records,

union types)

Page 44: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

view:● It's a single pure function● Receives the current state to be

rendered● Returns HTML and Messages● Rendering leverages Virtual DOM

Page 45: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

update:● It's a single pure function● Receives Message and current

state as parameters● Updates the state according to

the message and content● Returns the updated state

Page 46: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

Elm

Page 47: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

BEAM Architecture

Page 48: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

BEAM Architecture

Process

Process

Process

Messages

Messages

Messages

Page 49: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

Elixir

Page 50: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

Elixir

Page 51: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo
Page 52: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo
Page 53: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

Want to move to Europe?We're Hiring!

digitalnatives.hu/jobsrhnonose RodrigoNonose

Page 54: RODRIGO NONOSE FUNCTIONAL PROGRAMMING IN THE WEB …rhnonose.github.io/prog_funcional_web_rubyconf.pdf · functional programming in the web with elixir and elm rodrigo nonose sÃo

● Code Examples: https://github.com/rhnonose/rubyconf_examples● Elixir website: https://elixir-lang.org/● Elm website: http://elm-lang.org/● Beam architecture: https://happi.github.io/theBeamBook/● Benchmark: https://github.com/mroth/phoenix-showdown● Github and Twitter icons designed by Dave Gandy from Flaticon● Design Patterns in Dynamic Languages:

http://www.norvig.com/design-patterns/

Links and Acknowledgements