Slide 1

Slide 1 text

OOP and FP: Bridging the Gap Damir Zekić
 @sidonath

Slide 2

Slide 2 text

“A man who knows two languages is worth two men.” ‒ French Proverb

Slide 3

Slide 3 text

“Those who know nothing of foreign languages
 know nothing of their own.” ‒ Johann Wolfgang von Goethe

Slide 4

Slide 4 text

“A great programmer might be ten
 or a hundred times as productive as an ordinary one” ‒ Paul Graham

Slide 5

Slide 5 text

“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

Slide 6

Slide 6 text

What Can We Learn From FP? Increase in productivity Concurrency safety

Slide 7

Slide 7 text

Functional Programming
 Is Getting Mainstream

Slide 8

Slide 8 text

Indicators

Slide 9

Slide 9 text

Indicators People talk about FP at conferences

Slide 10

Slide 10 text

Indicators People talk about FP at conferences Real-world applications built with FP languages

Slide 11

Slide 11 text

Indicators People talk about FP at conferences Real-world applications built with FP languages OOP languages are getting FP features

Slide 12

Slide 12 text

Sceptics Say…

Slide 13

Slide 13 text

Sceptics Say… Functional programming is like jetpack

Slide 14

Slide 14 text

Sceptics Say… Functional programming is like jetpack FP is only for academia

Slide 15

Slide 15 text

Sceptics Say… Functional programming is like jetpack FP is only for academia It's another tool

Slide 16

Slide 16 text

Sceptics Say… Functional programming is like jetpack FP is only for academia It's another tool … it's weird

Slide 17

Slide 17 text

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/

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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/

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Can I Build Me Some Web Apps? Om

Slide 22

Slide 22 text

Nothing can prepare you
 for functional programming (except programming functionally)

Slide 23

Slide 23 text

… so you better get started

Slide 24

Slide 24 text

Basics of FP Functions as first-class citizens Working with and writing higher-order functions

Slide 25

Slide 25 text

A Simple Task Given a list of cart item objects (product, price, quantity),
 calculate a total price for the cart

Slide 26

Slide 26 text

JavaScript: Imperative var i, total = 0; ! for (i = 0; i < cartItems.length; i++) { total += cartItems[i].price * cartItems[i].quantity; } ! alert(total);

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

C#: Imperative var total = 0m; ! foreach (var cartItem in cartItems) { total += cartItem.Price * cartItem.Quantity; } ! Console.WriteLine(total);

Slide 30

Slide 30 text

C#: Functional var total = cartItems .Select(cartItem => cartItem.Price * cartItem.Quantity) .Aggregate(0m, (sum, subtotal) => sum + subtotal); ! Console.WriteLine(total);

Slide 31

Slide 31 text

Ruby: Imperative total = 0 ! cart_items.each do |cart_item| total += cart_item.price * cart_item.quantity end ! puts total

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

Other FP Language Features Currying, partial application and composition Pattern matching Algebraic types: "product" (record) and "sum" (union) types Immutability and many more…

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

Crossing the Bridge JavaScript → ClojureScript C# → F# Java → Scala Ruby → Elixir

Slide 37

Slide 37 text

F# let total = cartItems |> List.map (fun ci -> ci.Price * decimal ci.Quantity) |> List.fold (+) 0M ! printfn "%M" total

Slide 38

Slide 38 text

Thank you! Q&A Damir Zekić
 @sidonath Slides published at: http://bit.ly/fp-bridge