\w _ ____________ ______ ______ ____ \w / | / / ____/ __ \\/ ____/ /_ __/___ _/ / /_______ \w / |/ / __/ / / / / / / / / __ `/ / //_/ ___/ \w / /| / /___/ /_/ / /___ / / / /_/ / / ,< (__ ) \w/_/ |_/_____/_____/\\____/ /_/ \\__,_/_/_/|_/____/ Presenting a lecture on: \g ______ __ _ __ \g / ____/_ ______ _____/ /_(_)___ ____ ____ _/ / \g / /_ / / / / __ \\/ ___/ __/ / __ \\/ __ \\/ __ `/ / \g / __/ / /_/ / / / / /__/ /_/ / /_/ / / / / /_/ / / \g/_/ \\__,_/_/ /_/\\___/\\__/_/\\____/_/ /_/\\__,_/_/ \g ____ _ \g / __ \\_________ ____ __________ _____ ___ ____ ___ (_)___ ____ _ \g / /_/ / ___/ __ \\/ __ `/ ___/ __ `/ __ `__ \\/ __ `__ \\/ / __ \\/ __ `/ \g / ____/ / / /_/ / /_/ / / / /_/ / / / / / / / / / / / / / / / /_/ / \g /_/ /_/ \\____/\\__, /_/ \\__,_/_/ /_/ /_/_/ /_/ /_/_/_/ /_/\\__, / \g /____/ /____/ By Aaron Gross --- | \*\w What is functional programming? Functional Programming is simply a programming paradigm, just like imperative or object oriented programming --- \*\w Some History / Functional programming was born of a branch of formal logic called Lambda calculus. Lambda calculus is a formal notation for defining and composing functions, and has been around since the early 20th century. Lambda calculus has been largely confined to academia, and as a result, functional programming, which is derived from it, has been somewhat isolated to academia as well. Recently, functional programming has recently begun gaining more popularity. --- \*\w What is functional programming? \*\w This time with an actual answer! / Functional Programming has two defining qualities: * Functional programs perform computations by the evaluation of expressions, as opposed to executing statements. Functions are the basic building blocks of a functional program, as opposed to statements or objects. * Functions are first class, meaning they can be used like any other sort of value. That means functions can be used just like a value by being passed as arguments to other functions, assigned to variables, or returned from functions. --- \*\w This is not the whole story! / There are many other aspects of functional programming that either compliment the two features mentioned above, or are direct consequences of them. For example, functional purity is a concept embraced either partially or fully by almost every functional language. Pure functions are functions that have no side effects. That is, they do not change a global state, which could mean anything from printing to the screen, or changing the state of a variable scoped outside the function. As stated before, not all languages are fully pure. Haskell, for example, enforces purity while Scala does not. --- \*\w The story goes on / Immutability is another large part of functional programming. Immutability means that once the value of a variable is set, that value is fixed. This facilitates functional purity, by ensuring that any functions return an altered copy of the data they operate on, thus avoiding altering a global state and maintaining purity. One may wonder however, what implications immutability has for memory usage, if every alteration to a set of data requires a new copy to be made. Referential transparency is another aspect of functional programming relating to purity. Referential transparency means that a function called with the same inputs will yield the same output each time it is called. This again serves to enforce the stateless nature of functional programming. --- \*\w Two final important aspects / Higher order functions are functions that take other functions one or more of their arguments. E.g. map() ``` val numbers = List(1, 2, 3, 4) println(numbers.map((i: Int) => i * 2)) ``` Recursion is also a key element of functional programming. ``` def factorial(n: Int): Int = if (n == 0) 1 else n * factorial(n - 1) factorial(5) ``` --- \*\w Why Functional Programming? / For the same reason as object oriented programming. It provides a layer of abstraction. One thing I've seen personally, is that functional programming addresses some of the design patterns object oriented code has that can be tedious: applicator.apply() imageManipulatorFactoryFactory.make() doer.do() As a counter point to this, more abstraction does mean more overhead, which implies that code can be slower. --- \*\w Why Functional Programming? / Functional programs can be shorter, more expressive, and easier to read that other types of code. As a counterpoint to this, the opposite can also be true. Functional programs can be longer, and harder to read, especially if you are new to the paradigm. Also, purity can be a pain to deal with when it complicates things like I/O. --- \*\w Why Functional Programming? / There is a claim that stateless code is easier to parallelize, both from the perspective of the developer and the compiler. --- \*\w What is the takeaway from all of this? / Anyone who tells you that functional programming is going to be *the* future of programming is lying to you. Functional programming has some good application specific domains (compiler programming gets cited a lot), but it is not a universal panacea. IT has some positive aspects that will see adoption by mainstream programming languages over time, and it's also already have aspects incorporated into languages (map in python and javascript, lambdas in lots of languages). --- --- | \*\w Scala time! Let's take a look at a specific language. Scala is a mixed paradigm, statically typed programming language based on the JVM. --- \*\w Hello World! / Save to HelloWorld.scala: object HelloWorld { def main(args: Array[String]) { println("Hello, World!") } } run as a script with: $ scala HelloWorld.scala or complile and execute with: $ scalac HelloWorld.scala $ scala HelloWorld \* or: $ scala ``` println("Hello, World!") ``` --- \*\w Pattern Matching / ``` def matchTest(x: Int): String = x match { case 1 => "one" case 2 => "two" case _ => "many" } println(matchTest(3)) ``` --- \*\w Sequence comprehensions / ``` def even(from: Int, to: Int): List[Int] = for ( i <- List.range(from, to) if i % 2 == 0) yield i println(even(0,20)) ``` --- https://github.com/marconilanna/REPLesent