Use Promises, Futures and some functional programing stuff without being a math nerd

39
USE PROMISES, FUTURES AND SOME FUNCTIONAL PROGRAMING STUFF WITHOUT BEING A MATH NERD. @WAXZCE TAKEOFF CONFERENCE

description

talk given to the take off conference 2014 about promises and futures in javascript world https://www.youtube.com/watch?v=2OjHNe9Akaw

Transcript of Use Promises, Futures and some functional programing stuff without being a math nerd

Page 1: Use Promises, Futures and some functional programing stuff without being a math nerd

USE PROMISES, FUTURES AND SOME FUNCTIONAL PROGRAMING STUFF WITHOUT BEING A MATH NERD.

@WAXZCE

TAKEOFF CONFERENCE

Page 2: Use Promises, Futures and some functional programing stuff without being a math nerd

CEO at www.clever-cloud.com

Polyglot developer

WHO AM I ?

@waxzce on twitter

Page 3: Use Promises, Futures and some functional programing stuff without being a math nerd

And learn a lot of things about your code, apps, and good/bad design…

KEEP YOUR APPS ONLINE. MADE WITH NODE.JS, SCALA, JAVA, RUBY, PHP, PYTHON, GO…

Page 4: Use Promises, Futures and some functional programing stuff without being a math nerd

I’M NOT A MATH NERD

Page 5: Use Promises, Futures and some functional programing stuff without being a math nerd

NODE.JS MAIN PROBLEM

Page 6: Use Promises, Futures and some functional programing stuff without being a math nerd

SO, TRY TO KEEP IT ALIVE AND MINIMIZE ERROR EXPOSURE

Page 7: Use Promises, Futures and some functional programing stuff without being a math nerd

ON THE OTHER HAND: THE CALLBACK SOUP

Page 8: Use Promises, Futures and some functional programing stuff without being a math nerd

EXAMPLE : DATABASE CONNECTION

Page 9: Use Promises, Futures and some functional programing stuff without being a math nerd

REALLY ? ALL DB INTERACTION IN ONE CALLBACK ?

Page 10: Use Promises, Futures and some functional programing stuff without being a math nerd

REALLY ? UNCATCHED EXCEPTIONS BUBBLE TO THE TOP AND KILL THE APP ?

Page 11: Use Promises, Futures and some functional programing stuff without being a math nerd

REALLY ? IF/ELSE/UNDEFINED ERROR TREATMENT ?

Page 12: Use Promises, Futures and some functional programing stuff without being a math nerd

DB DRIVER SHOULDN’T DRIVE YOUR CODE ARCHITECTURE, THEORICALLY

Page 13: Use Promises, Futures and some functional programing stuff without being a math nerd

SO, WE HAVE A PROBLEM : HOW TO USE A VALUE WE DON’T HAVE YET, AND USE IT IN THE FUTURE?

Page 14: Use Promises, Futures and some functional programing stuff without being a math nerd

CHANGE YOUR MINDHOW DO YOU INTERACT WITH DATA NOW?

• Wait for it• Possibly

blocking i/o

Get data from DB and/or

WS

• Act on it• Data

treatment

Data is here

Page 15: Use Promises, Futures and some functional programing stuff without being a math nerd

EXAMPLE

Page 16: Use Promises, Futures and some functional programing stuff without being a math nerd

YOU WANT TO RETURN SOMETHING !

Page 17: Use Promises, Futures and some functional programing stuff without being a math nerd

YOU MIX I/O HANDLING WITH YOUR BUSINESS PROCESSES

THE OTHER PROBLEM :

Page 18: Use Promises, Futures and some functional programing stuff without being a math nerd

SPLIT

Business process and

valued computing

I/O management and other

“computing stuff”

Page 19: Use Promises, Futures and some functional programing stuff without being a math nerd

CHANGE YOUR MINDHOW CAN WE DO IT?

• Give all the “recipes” about how to deal with this data

• You only describe how to manage data you get

Get data from DB and/or

WS

That’s all

Page 20: Use Promises, Futures and some functional programing stuff without being a math nerd

WORK ON FUTURE DATA YOU HAVE PROMISED TO YOUR CODE ;-)

Page 21: Use Promises, Futures and some functional programing stuff without being a math nerd

EXAMPLESIMPLE PROMISE MAP

Page 22: Use Promises, Futures and some functional programing stuff without being a math nerd

EXAMPLESIMPLE PROMISE MAP

Page 23: Use Promises, Futures and some functional programing stuff without being a math nerd

MORE IMPORTANT : YOU CAN RETURN A DATA YOU DON’T HAVE AT THE MOMENT

Page 24: Use Promises, Futures and some functional programing stuff without being a math nerd

NO MORE DEALING WITH BLOCKING I/O IN YOUR BUSINESS CODE

Page 25: Use Promises, Futures and some functional programing stuff without being a math nerd

DEALING WITH MULTIPLE FUTURE DATA

Page 26: Use Promises, Futures and some functional programing stuff without being a math nerd

And stop using boolean counter for event « ready »….

GET ALL YOU NEED TO WORK

Page 27: Use Promises, Futures and some functional programing stuff without being a math nerd

REAL WORLD EXAMPLE

Page 28: Use Promises, Futures and some functional programing stuff without being a math nerd

BUILT IN ERROR MANAGEMENT

Page 29: Use Promises, Futures and some functional programing stuff without being a math nerd

SIMPLE WAY : YOU CAN RETURN SOMETING :-)

Page 30: Use Promises, Futures and some functional programing stuff without being a math nerd

PROMISES WILL MANAGE TEMPORALITY PROBLEMS AND I/O IN A SIMPLE WAY

Page 31: Use Promises, Futures and some functional programing stuff without being a math nerd

NOT TOO COMPLICATED ?

Page 32: Use Promises, Futures and some functional programing stuff without being a math nerd

WE’VE JUST DEALT WITH FUNCTORS, MONADS, SEMIGROUPS… ALL OF THESE ALGEBRAIC STRUCTURES

Page 33: Use Promises, Futures and some functional programing stuff without being a math nerd

MATH WORDS, NOT SO COMPLICATED, JUST MADE TO SHARE CONCEPTS BETWEEN PEOPLE :-)

Page 34: Use Promises, Futures and some functional programing stuff without being a math nerd

FUNCTIONAL PROGRAMING IS NOT A LANGUAGE : IT’S THE ABILITY TO USE ALGEBRAIC STRUCTURES AND MATH TOOLS IN YOUR CODE

SO YOU CAN REUSE IT WHERE YOU WANT…

Page 35: Use Promises, Futures and some functional programing stuff without being a math nerd

Made by the awesome Brian McKenna - @puffnfresh

FUNCTIONAL JAVASCRIPT : FANTASY LAND SPECIFICATION

Page 36: Use Promises, Futures and some functional programing stuff without being a math nerd

Node.js + browser implementation, totally agnostic javascript,

Fantasy Land compatible + Promises/A+ compatible

made by Paul Mucur with help from Rodolphe Belouin

DEMO MADE WITH PACTA IMPLEMENTATION

Page 37: Use Promises, Futures and some functional programing stuff without being a math nerd

TRY IT YOU WILL

Page 38: Use Promises, Futures and some functional programing stuff without being a math nerd

BE A BADASS !

Page 39: Use Promises, Futures and some functional programing stuff without being a math nerd

I give you a www.clever-cloud.com gift : takeoff2014

I’m on twitter : @waxzce

THANK YOU !ANY QUESTIONS ?