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

What is functional programming?

Avatar for dbousamra dbousamra
January 11, 2014

What is functional programming?

A very basic intro to FP that concentrates on two parts: immutability and statelessness, and higher-order functions. Given at ThoughtWorks LevelUp EXP 2 (levelup.thoughtworks.com/sydney/levelup-exp-dec-2013)

Avatar for dbousamra

dbousamra

January 11, 2014

Other Decks in Programming

Transcript

  1. Curiosity There are other ways of doing things It’s a

    style You don’t need to drink all the koolaid.
  2. A definition A programming paradigm. Expresses computation without describing control

    flow Avoids state and mutable data. Define what, not how.
  3. Purity What? Same arguments in == same result out NO

    side effects! Why? Easier to design, test and understand. Referential transparency => optimisations => laziness
  4. def calculateTax(taxRate, amount) return taxRate / 100 * amount end

    ! ! ! ! ! def calculateTax(amount) return getTaxRateFromDb() / 100 * amount end ! def calculateTax(taxRate, amount) rate = taxRate / 100 * amount saveToDb(rate) return rate end Pure ! ! ! ! ! ! ! Impure
  5. Functionally pure core. Useful programs need to interact in a

    stateful manner. Isolate state if you cannot eliminate it. Functionally pure core. Stateful, impurities
  6. Purity Purity forces us to consider functional design patterns. But…

    How do we do anything useful without our familiar control constructs? Can we… Write our code it’s more about what we want, rather than how we want it?
  7. for (var i = 0; i < 10; i++) {

    // do something side-effecting in here. } ! while (someCondition) { // do something side-effecting in here. } ! foreach(var something in somethings) { // do something side-effecting in here. } State modifying by nature ! Explicitly describing control flow ! ! ! ! !
  8. A higher-order function, is a function that takes one or

    more functions as input, or returns a function. “ “
  9. var numbers = [1, 2, 3, 4, 5]; ! var

    destination = [ ]; for (var i = 0; i < numbers.length; i++) { result = numbers[i] * 2; destination.push(result); } ! // destination => [2, 4, 6, 8, 10] Lots of boilerplate. ! Manually handling iteration, and appending ! ! ! ! !
  10. function map(coll, f) { var destination = []; for (var

    i = 0; i < coll.length; i++) { var result = f(numbers[i]); destination.push(result); } return destination; } ! ! ! ! var numbers = [1, 2, 3, 4, 5]; ! var result = map(numbers, function(n) { return n * 2; }); Utility map function handles iteration and appending ! ! ! ! ! ! Actual business logic
  11. numbers = [1, 2, 3, 4, 5]; ! ! def

    addTen(n) return n + 10 end ! ! result = numbers.map(addTen) ! # result => [11, 12, 13, 14, 15] Some numbers ! ! A transformer function ! ! ! Apply the transformer to each element in the list map
  12. numbers = [1, 2, 3, 4, 5] ! ! def

    isEven?(n) return n % 2 == 0 end ! ! result = numbers.filter(isEven?) ! # result => [2, 4] Some numbers ! ! A filter predicate function ! ! Apply the filter to each element in the list, and keep it if is passes the predicate filter
  13. numbers = [1, 2, 3, 4, 5] ! ! def

    sum(acc, number) return acc + number end ! ! result = numbers.reduce(sum, 0) ! # result => 15 Some numbers ! ! An accumulator function ! ! Apply the accumulator to each number, and the accumulated value. reduce/fold
  14. An example ! What's the sum of the first 10

    natural numbers, whose square value is divisible by 5?
  15. n = 1 num_elements = 0 sum_of_first_10 = 0 !

    while num_elements < 10 if n^2 % 5 == 0 sum_of_first_10 += n num_elements += 1 end n += 1 end ! puts sum_of_first_10 #=> 275 Lots of state going on. ! ! ! Not obvious what we are doing without following code through.
  16. def div_by_five(n) n^2 % 5 == 0 end ! sum_of_first_10

    = [0..] .filter(div_by_five) .take(10) .fold(sum) ! puts sum_of_first_10 #=> 275 Notice we have described what we want, rather than how to get it.
  17. Where to now? ! Write pure functions/methods Learn a functional

    language Use loops as the exception, not the default Google “FP in <insert language here>”