was created in the late 1950s by John McCarthy. In fact, FP predates Object Oriented(OO) programming. However, at that time machine memory was expensive — too expensive to make FP practical, because it requires a lot of memory. Nowadays, memory is absurdly cheap. Functional programs can be implemented easily and FP is becoming increasingly popular.
to test and debugging, reuse, and generalize code more easily. By being both modular and stateless, purely functional code exercises the design principle of composability. FP makes it easier for code to run concurrently. In short, when well executed, functional code is less prone to bugs.
citizens,” meaning they can be returned as values or passed as inputs to other functions. A function that does either of those is referred to as a higher order function. Higher order functions are the linchpin of functional programming.
function that tells whether something is true or not) and a list and then returns the list of elements that satisfy that predicate. Scala: val list = List.range(1, 11) def isGreater?(x: Int, upperBound: Int): Boolean = x > upperBound val greaterThanFive = list.filter(x => isGreater?(x, 5)) greaterThanFive is List(6,7,8,9,10)
value by operating on a list of values. With the power of higher order functions, you can decide how you want to combine your list. Scala: def sum(accumulator: Int , x: Int) = x + y val total = (0 to 10).foldLeft(0)((acc, x) => sum(acc, x)) total is 55
to each element of a list, returning a list of results. Scala: def square(n: Int) = n * n val squaredList = List(1,2,3,4).map{ x => square(x) } squaredList is List(1,4,9,16)
Scala, the language supports function literals or anonymous functions. def isGreater?(x: Int, upperBound: Int): Boolean = x > upperBound val greaterThanFive = list.filter(x => isGreater?(x, 5)) Becomes... val greaterThanFive = list.filter(_ > 5)
programs are constructed using only pure functions —functions that produce no side effects. Some examples of side effects: • Modifying a variable • Modifying a data structure in place • Reading from or writing to a file • Reading or writing to a database
or altered once created. val x = List(1,2,3,4,5) x.map(i => i * 2) x is List(1, 2, 3, 4, 5) val y = x.map(i => i * 2) y is List(2, 4, 6, 8, 10) x is List(1, 2, 3, 4, 5)
need or want. def square(array) array.each_with_index { |n, index| array[index] = n * n } array end changing_array = Array[1,2,3,4] square(changing_array) changing_array is now Array[1,4,9,16]
with recursion. def recursiveFact(r: Int): Int = if (r == 1) r else r * recursiveFact(r-1) def loop_fact(n) (2 .. n-1).each { |i| n *= i } return n end
focus on? Tracking changes in state, performing tasks step by step. How do I transform an input to get the output I want? Does state change? All the time! Nope! Primary flow control Loops, iteration, method calls Recursion, function calls What do I manipulate Classes, instances of classes! I pass functions around!