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

Why Functional Programming Matters

αλεx π
January 10, 2014
230

Why Functional Programming Matters

Ramblings on the subject of "why FP matters"

αλεx π

January 10, 2014
Tweet

Transcript

  1. Expressions, not assignments FP encourages thinking in results of evaluation,

    rather than assigning results to re-assignable variables
  2. (def map! (fn [f coll]! (if (empty? coll) (quote ())!

    (cons (f (first coll))! (map f (next coll))))))!
  3. How to do, not what to do FP discourages operational

    reasoning, promotes algorithmical reasoning
  4. eval closure sym@(LispSymbol _) eval :: [LispEnvironment] -> LispExpression ->

    State LispEnvironment LispExpression eval closure (LispList[(ReservedKeyword FnKeyword), LispVector bindings, form])
  5. Outcome Obvious problem description, based on signatures Matching logic doesn’t

    involve if/else statements Logic is inherently recursive
  6. Outcome Both handlers and parsing logic is reusable Testing handlers

    doesn’t involve any additional object composition
  7. Powerful collection fns All Functional languages have amazingly powerful collection

    manipulation primitives, such as filter/map/reduce, which always involves conditionals, mutable state, loops and so on.
  8. Outcome Obvious results Thinking in patterns Reusability, describing processes as

    sequence of steps Many opportunities instead of a single “for” loop
  9. Inherently concurrent FP languages do not let you modify state,

    which prevents using locking structures, synchronisation primitives and allows thinking of state as “value at a time”
  10. Different polymorphism In different languages, there are different means. For

    example, in Clojure you have protocols and multimethods, in Haskell - algebraic data types, type classes and pattern matching
  11. Outcome Dispatch rarely involves any if/else statements It’s easier to

    refactor functions to use a common protocol or implement a type class / ADT
  12. Partial evaluation / currying Concept that requires lots of bells

    and whistles in Java In both Clojure and Haskell, you can split a function to sequence of partial evaluation calls
  13. Outcome It’s possible to return a partial evaluation result, and

    act on it as if it was a future, evaluating it till the end as the rest of args pop in
  14. Lazy evaluation Despite the fact the concept is extremely easy,

    it’s seldom used in imperative languages. Most FP languages have built-in primitives for `next` calculation
  15. (defn positions! "Returns a lazy sequence containing positions of elements"!

    [types]! ((fn rpositions [[t & more] current-pos]! (when t! (cons current-pos ! ! ! ! ! (lazy-seq ! ! ! ! ! (rpositions more (+ current-pos (size t))))))) types 0))
  16. Outcome Infinite data structures No mutable state during `next` generation

    Generator is easy to read and modify, knowing the semantics No leaks, no accidental captures
  17. Outcome Easier to test Easier to debug Smaller error rate

    Determinism / potential for optimisation Easier to prove program correctness
  18. Knowing it before them Adoption of FP languages is growing

    every day. Not being able to provide services will leave us with limited business opportunities.
  19. Rapid Prototyping FP languages allow you to express a problem

    fast, with minimal effort and provide a working result quicker than imperative one
  20. FP In Java Reactor is one of my favourite examples

    of using FP paradigms even in Java 7. Java 8 and lambdas is a step forward. Being able to use them knowing the context of where they’re coming from opens up a lot of possibilities.
  21. Reactive programming Reactive programming also grows from FP. Having a

    function as a callback was used singe ages in FP.