Dimitry Solovyov - The imminent threat of functional programming

1,270 views
1,152 views

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,270
On SlideShare
0
From Embeds
0
Number of Embeds
59
Actions
Shares
0
Downloads
7
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Dimitry Solovyov - The imminent threat of functional programming

  1. 1. The Imminent Threat of Functional Programming
  2. 2. Dimitry Solovyov‣ Haskell enthusiast and FP proponent‣ Heavy user of Underscore (FP library for JavaScript)‣ Former assistant teacher of FP course at TTI‣ Developer at Amber Games and Livesheets
  3. 3. dresdencodak.com
  4. 4. FUNCTIONALPROGRAMMING
  5. 5. In programming land,there are two kinds of problems
  6. 6. In programming land,there are two kinds of problems memory management
  7. 7. In programming land, there are two kinds of problemssequencing memory(aka do it in what order?) management
  8. 8. In programming land, there are two kinds of problemssequencing memory E D LV management(aka do it in what order?) SO
  9. 9. In programming land, there are two kinds of problems memory D *sequencing VE(aka do it in what order?) O L management S * Except for null pointer exceptions ;-)
  10. 10. In programming land, there are two kinds of problems ? memory D *sequencing VE(aka do it in what order?) O L management S * Except for null pointer exceptions ;-)
  11. 11. Sequencing in Java while cond stmt do stmt while condfor init cond step stmt for x : xs stmt
  12. 12. Sequencing in Ruby loop stmt while cond stmt until cond stmt stmt while cond stmt until cond for x in xs stmt xs each stmt n times stmt n upto m stmt n downto m stmt ...
  13. 13. Sequencing in Ruby loop stmt while cond stmtHow elegant! until cond stmt stmt while cond stmt until cond for x in xs stmt xs each stmt n times stmt n upto m stmt n downto m stmt ...
  14. 14. Imperative programming in a nutshell10 BEGIN20 DO SOMETHING3040 DO THAT OTHER THING GOTO 20 ✔INSTRUCTIONS RESULT
  15. 15. Declarative programming in a nutshellDear computer,I want a pony. MAGIC ✔DESCRIPTION RESULT
  16. 16. INSTANTSUCCESS
  17. 17. “ OO makes code understandable by encapsulating moving parts. FP makes code understandable by minimizing moving parts. ” -- Michael Feathers“Functional Programming in C++” http://goo.gl/aVSXX
  18. 18. Abstraction and reusability in FPAbsolutely abstract Category theory * ly reu sable High Exciting ly comp osable Mathematically proven Ex tremely fun to be composable * Check out Scalaz for CT in Scala Guava for other FP patterns in Java
  19. 19. + Some GoF pattern analogs provided at semantic level“Design Patterns in Haskell” http://goo.gl/T0Evt
  20. 20. You can get away without encapsulating in objects Data types fo rm r fo ed d ne in to ig s de operate on ImmutableCombinators data structures
  21. 21. You can get away without encapsulating in objects Data types fo rm r fo ed d ne in to ig s de operate on ImmutableCombinators data structures
  22. 22. You can get away without encapsulating in objects Data types Ready for the age of multicore fo rm r fo ed d ne in to ig s de operate on ImmutableCombinators data structures
  23. 23. Traits instead of inheritance Thesis: Inheriting state creates brittle software. Inheriting implementation tends to become useless over time.“Life without Objects” http://goo.gl/wlOyq
  24. 24. Traits instead of inheritance Thesis: Inheriting state creates brittle software. Inheriting implementation tends to become useless over time. A B Data type“Life without Objects” C Expected to behave like A, B, and C http://goo.gl/wlOyq
  25. 25. “ In the exclusive sense, functional means no side-effects. In the inclusive sense it means a programming style which composes functions in interesting ways. ” -- Martin Odersky“In Defense of Pattern Matching” http://goo.gl/HaKQD
  26. 26. Origin of the “functional style” LISP MLCommon Lisp Standard ML Scheme OCaml Dylan F# Racket Haskell Closure JVM Frege JVM Scala * JVM * (cough) Close enough.
  27. 27. LISP ML Dynamic Strong typesCode = data Type inferenceMetaproggin ADTs First-class functions Higher-order functions Pattern matching
  28. 28. LISP ML Dynamic Strong typesCode = data Type inferenceMetaproggin ADTs First-class functions Higher-order functions Pattern matching
  29. 29. Linus says: “Words are cheap, show me the code.”
  30. 30. Fake first-class functions in Javainterface Func<A, B> { B apply(A x);}static <A, B, C> Func<A, C> compose(final Func<A, B> f, final Func<B, C> g) { return new Func<A, C>() { public C apply(A x) { return g.apply(f.apply(x)); } };}
  31. 31. Fake higher-order functions in Java static <A, B> List<B> map(final Iterable<A> xs, final Func<A, B> f) { List<B> ys = new ArrayList<B>(); for (A x : xs) { B y = f.apply(x); ys.add(y); } return ys; } map
  32. 32. Fake higher-order functions in Java static <A, B> B fold(final Iterable<A> xs, final B init, final Func2<A, B, B> f) { B acc = init; for (A x : xs) { acc = f.apply(x, acc); } return acc; } fold
  33. 33. REFERENTIALTRANSPARENCY
  34. 34. How logic works x=1 y=2 x+y=3
  35. 35. in languages How logic works with side-effects x=1 y=2 x + y = 3** But only if the moon is still young! Otherwise the answer is 5.
  36. 36. Functional programming on the JVM en l fe d tio ss ar ia ef le ch n c o cy s nc cla sp nt es at er e- rol ct ns g an re in ur m att fu st- sid nt tr efe os p fir cl r Clojure ✔ ✔ ✔ ✘ ✘ Fantom ✔ ✔ ✔ ✘ ✘ Frege ✔ ✔ ✔ ✔ ✔ Java ✔ ✘ ✘ ✘ ✘ JRuby ✔ ✘ ✘ ✘ ✘ Kotlin ✔ ✔ ✔ ✘ ✔ Scala ✔ ✔ ✔ ✘ ✔
  37. 37. FP exploration checklist❏ Start learning a functional language: Scala, Clojure, or even Haskell❏ Get together @ Latvian FP Group: look for it on LinkedIn http://goo.gl/YhlJl❏ Talk to me! dimituri@gmail.com

×