> For the complete documentation index, see [llms.txt](https://edsonha.gitbook.io/my-gitbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://edsonha.gitbook.io/my-gitbook/object-oriented-programming-oop/functional-programming-fp/example-of-fp.md).

# Example of FP

```javascript
const user = {
  name: 'Kim',
  active: true,
  cart: [{ name: 'orange', price: 20}],
  purchases: [{name: 'apple', price: 10}]
}

const item = {name: 'laptop', price: 200}

let amazonHistory = [];

const compose = (accumulatorFunction, currentFunction) => {
  return (...args) => accumulatorFunction(currentFunction(...args)); 
}

function purchaseItem(...fns) {
  return fns.reduce(compose)
}

function addItemToCart(user, item) {
  amazonHistory.push(user)
  const updatedCart = user.cart.concat([item]);
  return Object.assign({}, user, {cart: updatedCart})
}

function applyTaxToItems(user) {
  amazonHistory.push(user)
  const { cart } = user;
  const taxRate = 1.3;
  const updatedCart = cart.map(item => {
    return {
      name: item.name,
      price: item.price*taxRate
    }
  })
  return Object.assign({}, user, {cart: updatedCart})
}

function buyItem(user){
  amazonHistory.push(user)
  const updatedPurchases=user.purchases.concat(user.cart)
  return Object.assign({},user,{purchases:updatedPurchases})
}

function emptyCart(user) {
  amazonHistory.push(user)
  return Object.assign({}, user, {cart: []})
}

purchaseItem(emptyCart, buyItem, applyTaxToItems, addItemToCart)(user, item)
//amazonHistory
```

Why we're doing push to Amazon History? Isn't that state? Are we modifying state? Remember we can't just purely have pure functions in order for us to do anything with the program. We do have to mutate data and the idea of functional programming is to minimize those mutations.

Now the benefit of functional programming is it's really good at doing one to one data transformations. That is we have a piece of data and we have functions acting upon it.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://edsonha.gitbook.io/my-gitbook/object-oriented-programming-oop/functional-programming-fp/example-of-fp.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.
