# Resurgence of FUNctional Programming

## Words of Wisdom

No matter what language you work in, programming in a functional style provides benefits. You should do it whenever it is convenient, and you should think hard about the decision when it isn't convenient.

- John Carmack, ID Software

So

## What is FUNctional Programming ?

FUNctional Programming is a list of things you can't do.

### Things You can't do

1. No Assignments.
2. No Varying Your Variables. (Immutable)
3. No While/For Loops.
4. No Control Over Order of Execution.
5. No Side Effects.
6. No Mutating or changing state.
Are You Kidding me ?

How Can Anyone Program like this ?

So What is a better definition
FUNctional Programming ?

"Functional Programming is so called because a program consists entirely of functions."

-  John Hughes , Why Functional Programming Matters

## Functions

f(x) = 3x + 7

1. When we evaluate f(4) = 3 * 4 + 7 = 19
2. Value of 'x' does not change in function body.
3. Same Input. Same Output. Every time.
4. We can replace any occurrence of  f(4) with 19.                      (Referential Integrity)

## Words of Wisdom

"The Language that does not affect the way you think about programming, is not worth knowing."

- Alan Perlis

## Origin

Alonzo Church
(1903 - 1995)

( Lambda Calculus )

## Imperative vs Functional

Imperative:
``````total = 0
i = 0
while i <= 10
total += i
i = i + 1
end
total
``````

Functional:
`` total = (1..10).inject(:+)``

## Words of Wisdom

Functional Programming is unfamiliar territory for most

"If you want everything to be familiar you will never learn anything new"
- Rich Hickey (Author of Clojure)

First Class

&

## Higher Order Functions

programming language is said to have first-class functions if it treats functions as first-class citizens.

Higher-order functions are functions that can either take other functions as arguments or return them as results.

Higher Order Functions Enable Currying

## Currying

Currying is the technique of translating the evaluation of a function that takes multiple arguments  into evaluating a sequence of functions, each with a single argument (partial application).

```add = proc { |x, y, z| x + y + z }

## Pure Functions

Purely functional functions (or expressions) have no side effects (memory or I/O)

## Words of Wisdom

Functional programming is like describing your problem to a mathematician.
Imperative programming is like giving instructions to an idiot.

## Tail Recursion

A recursive function is tail recursive if the final result of the recursive call is the final result of the function itself.

## Non Tail Recursive:

`````` def factorial(n)
if n == 0
return 1
else
return n * factorial(n-1)
end
end
``````

Tail Recursive:
`````` def factorial_one(n, total)
if n == 0
else
return factorial_one(n-1, n * total)
end
end

def factorial(n)
return factorial_one(n, 1)
end``````

## Tail Call Optimization

Process by which a smart compiler can make a call to a function taking no additional stack space.

## Lazy Evaluation

Strict evaluation always fully evaluates function arguments before invoking the function

print length([2+1, 3*2, 1/0, 5-4])

Strict Evaluation: Fails
Lazy Evaluation: 4

Lazy evaluation does not evaluate function arguments unless their values are required to evaluate the function call itself.

## Things you can do

1. Lock Free Concurrency.
2. Brevity. (Modular Code)
3. Lazy Evaluation.
4. Composability.
5. Parallelism.
6. Improved ways of Testing.
7. Referential Transparency.
8. Lesser Bugs.

## Kick - Ass

1. Steep Learning Curve.

1. Clojure
3. Erlang
4. Lisp
5. Scala
6. Scheme
7. F#
8. OCaml

@rbharshetty