# Functional Programming

## Functional programming

A **programming paradigm.**

The process of building software by:

* composing **pure functions**
* avoiding **shared state** (any variable, object, or memory space that exists in a shared scope)
* avoiding **mutable data**
* avoiding **side-effects**

**Declarative** rather than **imperative**

Application state flows through pure functions

### Immutability

An **immutable** object is an object that can’t be modified after it’s created. Conversely, a **mutable** object is any object which can be modified after it’s created.

Central concept of FP: without it, the data flow in your program is lossy. State history is abandoned, and strange bugs can creep into your software.

In JavaScript, it’s important **not to confuse `const`, with immutability.** `const`creates a variable name binding which can’t be reassigned after creation. `const` does not create immutable objects. You can’t change the object that the binding refers to, but you can still change the properties of the object, which means that bindings created with `const` are mutable, not immutable.

**Immutable objects can’t be changed at all.** You can make a value truly immutable by deep freezing the object. JavaScript has a method that freezes an object one-level deep:<br>

### Side Effects

Any application state change that is observable outside the called function other than its return value. Side effects include:

* Modifying any external variable or object property (e.g., a global variable, or a variable in the parent function scope chain)
* Logging to the console
* Writing to the screen
* Writing to a file
* Writing to the network
* Triggering any external process
* Calling any other functions with side-effects

Mostly avoided in FP, which makes the effects of a program much easier to understand, and much easier to test.

### Declarative (vs Imperative)

FP is a **declarative paradigm**: the program logic is expressed without explicitly describing the flow control.

**Imperative** programs spend lines of code describing the specific steps used to achieve the desired results — the **flow control: How** to do things.

**Imperative** code frequently utilizes statements. A **statement** is a piece of code which performs some action. Examples of commonly used statements include `for`, `if`, `switch`, `throw`, etc…

**Declarative** code relies more on expressions. An **expression** is a piece of code which evaluates to some value. Expressions are usually some combination of function calls, values, and operators which are evaluated to produce the resulting value.

**Declarative** programs abstract the flow control process, and instead spend lines of code describing the **data flow: What** to do. The *how* gets abstracted away.

## map(), filter(), reduce()

## **Pure Functions**

A function which:

* Given the **same input, will always return the same output**
* Produces **no side effects** (it can’t alter any external state)

The foundation of **Functional Programming**

## **Function Composition**

**Combining two or more functions to produce a new function**.

Like snapping together **a series of pipes for our data to flow through**.

A composition of functions *`f`* and *`g`* can be defined as *`f(g(x))`,* which **evaluates from the inside out ,  right to left**. Ie., evaluation order is: *`x, g, f`.*

## Higher-order Functions

Any **function that takes one or more functions as arguments**, which it uses to operate on some data, and/or returns a function as a result.

Meant to abstract some operation that is performed repeatedly.

Example: `map()`, which takes an array and a function as arguments. `map` then uses this function to transform each item in the array, returning a new array with the transformed data.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ricardomol.gitbook.io/notes/frontend/untitled/functional-programming.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
