Functional Programming

First-Class Functions

First-class functions can either accept another function as an argument or return a function. Being able to create functions and return them or pass them to other functions becomes extremely useful in code reusability and code abstractions.

Immutable Variables

Immutable variables, once set, cannot be changed. Although immutability seems very difficult to do, given the fact that the state must change within an application at some point, we’ll see ways that we can accomplish it.

Pure Functions

Pure functions are functions that have no side effects. Side effects are actions a function may perform that are not solely contained within the function itself. When we think about side effects, we normally think about other functions, such as println or mutating a global variable. We can also see this when we pass in a variable and mutate it directly inside of that function.

Nonstrict-Evaluation

Nonstrict evaluations allow us to have variables that have not been computed yet. Strict evaluations—assigning a variable as soon as it is defined—are what we are used to. Nonstrict means that we can have a variable that does not get assigned (computed) until the first time it is referenced.

Recursion

Recursion allows us to write smaller, more concise algorithms and to operate by looking only at the inputs to our functions. This means that our function is concerned only with the iteration it is on at the moment and whether it must continue.

Statements

Statements are evaluable pieces of code that have a return value. Think about if state‐ ments that have a return value of some kind. Each line of code should be considered a statement, meaning there are very few side effects within the application itself.

Functional Programming & Concurrency

Concurrency enables us to do processing in parallel. Many functional newbies get the wrong impression that functional programming actually solves concurrency.

This is not the case.

What functional programming does do is utilize concepts that encourage more well-defined patterns to handle concurrency.

Message passing creates more independent threads. It allows a thread to receive messages without causing another thread to be blocked before it's received.

Immutability help us to define global states, allows global state transition as a whole rather than partial state changes or major synchronizations between threads.

Pattern Matching

Pattern matching doesn’t really appear in mathematics, but assists functional program‐ ming in decreasing the need for specific variables. In code we usually encapsulate a group of variables together inside of an object. Pattern matching allows us to bette type-check and extract elements from an object, making for simpler and more concise statements with less need for variable definitions.