# 13 Chaining functional tools ## The customer communications team continues Requirement: 1. Filter for only good customers (three or more purchases). 2. Map those to their biggest purchases. ### version 1 ![](https://i.imgur.com/9JfYL9U.jpg) ### version 2 ![](https://i.imgur.com/a4PfGrZ.jpg) ![](https://i.imgur.com/r1ZLF7X.jpg) ![](https://i.imgur.com/bvYYGpJ.jpg) ## Clarifying chains, method 1: Name the steps ### version 3 If we extracted a function for each higher-order function and named it, it would look like this: ![](https://i.imgur.com/Rkk7qMD.jpg) ### version 4 This time, instead of extracting and naming the steps, we will extract and name the callbacks: ![](https://i.imgur.com/bvYYGpJ.jpg) ![](https://i.imgur.com/nzm6iaW.jpg) ## Clarifying chains: Two methods compared method 1 ![](https://i.imgur.com/Rkk7qMD.jpg) method 2 ![](https://i.imgur.com/nzm6iaW.jpg) 1. By extracting and naming the callbacks, we’ve created more reusable functions. * For instance, `isGoodCustomer()` works on a single customer, while `selectBestCustomers()` only works on anarray of customers. 2. It also removes a level of nesting since the callbacks now have names instead of being inline. ## Example: Emails of customers who have made one purchase ![](https://i.imgur.com/7LgfWq9.jpg) ![](https://i.imgur.com/efAzZvS.jpg) ## optimization ![](https://i.imgur.com/4awyD5Z.jpg) ![](https://i.imgur.com/w5iucGF.jpg) ![](https://i.imgur.com/S9Ov6HY.jpg) ![](https://i.imgur.com/cvIV3lf.jpg) ## Refactoring existing for loops to functional tools ### Tip 1: Make data ![](https://i.imgur.com/MTIqMGG.jpg) ![](https://i.imgur.com/vlMu4de.jpg) ![](https://i.imgur.com/ATpi3AV.jpg) ### Tip 2: Operate on whole array at once ![](https://i.imgur.com/WJT3HBw.jpg) ![](https://i.imgur.com/3fimoEM.jpg) ### Tip 3: Take many small steps ![](https://i.imgur.com/5xA9bVn.jpg) ![](https://i.imgur.com/RH5IxZl.jpg) range function ![](https://i.imgur.com/abOtL3G.jpg) --- ![](https://i.imgur.com/ji9JMsQ.jpg) ## Comparing functional to imperative code ![](https://i.imgur.com/pmJr4Bx.jpg) ![](https://i.imgur.com/vlMu4de.jpg) ## Debugging tips for chaining 1. Keep it concrete * Be sure to use clear names in your code so that you don’t lose track of what’s what. 2. Print it out * insert a print statement between two steps * Pro tip: For really complex chains, add one step at a time and check the result before you add the next. 3. Flow your types ## JavaScript conveniences ![](https://i.imgur.com/WQeWow7.jpg) ## reduce() for building values Let’s say we lost the user’s shopping cart. Luckily, we have logged all of the items the user has added to their cart in an array. It looks like this: ```javascript= var itemsAdded = ["shirt", "shoes", "shirt", "socks", "hat", ....]; ``` ![](https://i.imgur.com/STbQ3OM.jpg) ### Event Sourcing Event Sourcing is a pattern for storing data as events in an append-only log. https://www.eventstore.com/event-sourcing ## Conclusion * We combine them into multi-step processes called chains. Each step in the chain is a simple operation that transforms the data to be one bit closer to the desired result. * We also learned how to refactor existing for loops into chains of functional tools. * Finally, we saw how powerful reduce() can be.