Upgrade to Pro — share decks privately, control downloads, hide ads and more …

OOP and FP: Bridging the Gap

Damir
July 12, 2014

OOP and FP: Bridging the Gap

Transition to functional programming. Gently.

Damir

July 12, 2014

Transcript

38 slides
  1. “Those who know nothing of foreign languages
 know nothing of

    their own.” ‒ Johann Wolfgang von Goethe
  2. “A great programmer might be ten
 or a hundred times

    as productive as an ordinary one” ‒ Paul Graham
  3. “Still, I don't think it's a stretch to believe this

    data
 shows 5:1 or 10:1 productivity differences between programmers.” ‒ Joel Spolsky
  4. Indicators People talk about FP at conferences Real-world applications built

    with FP languages OOP languages are getting FP features
  5. Sceptics Say… Functional programming is like jetpack FP is only

    for academia It's another tool … it's weird
  6. Real-world Apps Built with FP Pandoc: Universal markup converter Converts

    from: Markdown, HTML, LaTeX, Media Wiki markup… Converts to: HTML, docx, ODT, EPUB, LaTeX, PDF, Markdown… — https://github.com/jgm/pandoc/
  7. Real-world Apps Built with FP "Most of SoundCloud's products are

    written in Scala, Clojure, or JRuby."
 — https://developers.soundcloud.com/blog/building-products-at-soundcloud-part-1-dealing-with-the-monolith
  8. Real-world Apps Built with FP "Scala is one of the

    main application programming
 languages used at Twitter. […] Our use of Scala is mainly
 for creating high volume services that form distributed systems"
 — http://twitter.github.io/effectivescala/
  9. Real-world Apps Built with FP "We do tens of millions

    of RPC calls every day. […] Erlang makes
 writing those kinds of servers just ridiculously simple. I would
 never write a server like that in any other languages, Erlang
 is so perfect for that."
 — http://www.infoq.com/interviews/erlang-and-github
  10. A Simple Task Given a list of cart item objects

    (product, price, quantity),
 calculate a total price for the cart
  11. JavaScript: Imperative var i, total = 0; ! for (i

    = 0; i < cartItems.length; i++) { total += cartItems[i].price * cartItems[i].quantity; } ! alert(total);
  12. JavaScript: ES5 style var total = cartItems .map(function(cartItem) { return

    cartItem.price * cartItem.quantity; }) .reduce(function(sum, subtotal) { return sum + subtotal; }, 0); ! alert(total);
  13. JavaScript: ES6* style var total = cartItems .map(cartItem => cartItem.price

    * cartItem.quantity) .reduce((sum, subtotal) => sum + subtotal, 0); ! alert(total); ! ! ! ! * May require ES6 transpiler https://github.com/google/traceur-compiler
  14. C#: Imperative var total = 0m; ! foreach (var cartItem

    in cartItems) { total += cartItem.Price * cartItem.Quantity; } ! Console.WriteLine(total);
  15. C#: Functional var total = cartItems .Select(cartItem => cartItem.Price *

    cartItem.Quantity) .Aggregate(0m, (sum, subtotal) => sum + subtotal); ! Console.WriteLine(total);
  16. Ruby: Imperative total = 0 ! cart_items.each do |cart_item| total

    += cart_item.price * cart_item.quantity end ! puts total
  17. Ruby: Functional total = cart_items. map { |cart_item| cart_item.price *

    cart_item.quantity }. reduce(0) { |sum, subtotal| sum + subtotal } ! puts total
  18. Ruby: Functional #2 total = cart_items. map { |cart_item| cart_item.price

    * cart_item.quantity }. reduce(0, :+) ! puts total !
  19. Other FP Language Features Currying, partial application and composition Pattern

    matching Algebraic types: "product" (record) and "sum" (union) types Immutability and many more…
  20. Become a Better Programmer in 3 Steps 1. Write code

    in functional style in your language to grasp the concepts 2. Read "Why Functional Programming Matters"
 http://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf 3. Learn a functional programming language
  21. F# let total = cartItems |> List.map (fun ci ->

    ci.Price * decimal ci.Quantity) |> List.fold (+) 0M ! printfn "%M" total

More Decks by Damir

Other Decks in Programming