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

What is this functional programming everyone is talking about?

What is this functional programming everyone is talking about?

Held at the first Lambda Düsseldorf Meetup (https://www.meetup.com/de-DE/lambdadus/)

Jens Bendisposto

March 22, 2018
Tweet

More Decks by Jens Bendisposto

Other Decks in Technology

Transcript

  1. Functions are first class citizen* * i.e., they can be

    pushed around by a cruel developer
  2. A function can be a parameter • function inc(x) {

    return x+1 } • function apply_twice(f,x) { 
 return f(f(x)) 
 } • apply_twice(inc, 4)
  3. A function can be a return value • function apply_twice(f,x)

    { 
 return f(f(x)) 
 } • function apply_twice_curry(f) { 
 return function(x) { return f(f(x)) }
 } • apply_twice_curry(inc)(4)
  4. filter ( != ) b r b r y g

    map ( firstLetter ) b r b r y g reduce ( ++ ) "brbryg" The usual suspects ...
  5. Exercise Given a collection ns of numbers: • Find the

    sum • of the squares • of the first k numbers • of the even numbers • greater than 5 • If there are less than k matching numbers take all of them ns = [10,11,12,17,14,2,20,12], k = 4 840 ns = [10,11,12,17,14,2,20,12], k = 8 984 Inspired by Venkat Subramaniam's talk given at US Devoxx
  6. Solution A public static int compute(List<Integer> ns, int k) {

    int result = 0; int found = 0; for (int i = 0; (i < ns.size() && found < k); i++) { int v = ns.get(i); if (v > 5 && v % 2 == 0) { result += v * v; found++; } } return result; } Is this code correct?
  7. Solution B public static int compute(List<Integer> ns, int k) {

    return ns.stream() .filter(x -> x % 2 == 0) .filter(x -> x > 5) .mapToInt(x -> x * x) .limit(k) .sum(); } How about this code?
  8. It's simpler! • Reasoning about imperative code is hard! •

    Brain has to simulate the computer • Iteration logic mixed* with domain logic • Reasoning about functional code is easier • Single pass • Iteration logic is abstracted away * or as a Clojure programmer would say: complected
  9. Purity • Functions without side effects are called pure •

    Same inputs => same output • Super easy to test • Easier to reason about • Results can be cached • No locking required Pure functions are good for your mental health! Use them as much as you can!
  10. State Change, Part 1 Is this function pure? There is

    no purity in presence of mutation!
  11. State Change, Part 2 • Non-trivial programs have state •

    Functional programming is about controlling the state changes • Pure languages (e.g., Haskell) • Non-pure languages (e.g., Clojure and Scala)
  12. Treating state (pure) Note, that we cannot directly use the

    result of the method call anymore! nextRandom(0) + 1
  13. Interpreter x := 4 y := x + 2 empty

    global environment x = 4 assign(id(x),int(4)) 4 x = 4, y = 6 assign(id(y),plus(id(x),int(2))) 6
  14. Interpreter x := 4 y := x + 2 assign(id(x),int(4),

    { }) [4, {x = 4}] assign(id(y),plus(id(x),int(2)), {x = 4}) [6, {x = 4, y = 6}] empty global environment x = 4 assign(id(x),int(4)) 4 x = 4, y = 6 assign(id(y),plus(id(x),int(2))) 6
  15. Treating state (impure) • Special constructs (i.e., not variables) to

    manage state • Constructs handle concurrency
  16. Side effects • Pure Languages • Move all side effects

    to the seam of the system • Typically enforced by a static type system • Impure languages • Side effects are ok (concurrency built-ins) • Developer's dicipline to use them with care
  17. In Summary • Reduce and control complexity • Immutable data

    structures • Avoid/Manage side effects • High expressivity • Function as first class citizen
  18. rheinjug • Functional Java Workshops/Trainings • Date: tbd. (First workshop:

    September) • Come join the discussion @ EntwickelBar (26.5)
 
 https://entwickelbar.github.io/