• Skip to content
  • Skip to footer

PurelyFunctional.tv

  • Learn Clojure
  • About
  • 🛒 Cart
  • Log in
  • Join Now
Functional programming
Subscribe to a paid plan to get instant access to this course.
Download
 Video time: 09m44s
PREVIOUS
NEXT

Want this course?

$499.00 – Team License Checkout Added to cart
$75.00 – Download License Checkout Added to cart
$48.00 – Online License Checkout Added to cart
Membership

Functional programming

Updated January 15, 2019

Description
Notes
Code

Let's talk about Functional Programming. Then we can talk about combining ingredient lists.

Functional programming

Clojure is a functional programming language. It makes it easy to do functional programming. Functional programming is about approaching the problem by breaking it down into three ideas:

  • Data
  • Calculation
  • Effects

When we change the world, we are doing an effect. So in the bakery, that includes baking stuff, moving around the bakery, or delivery orders. It also includes getting the morning orders, because the orders we get can't be gotten by others.

The orders themselves are data. Data are records. Someone took an order and wrote it down. Now that data can be interpreted.

Calculations are most commonly when you generate new data from existing data. The key feature is that the calculation does not change the world. Calculations are exactly what we need to build one big "shopping list" out of all of the orders. We'll take the order data and calculate the data of the shopping list.

Combining two lists

Let's look at how we can calculate a combined shopping list given two shopping lists. If we have List 1 and List 2, we want to calculate List 3. For each item in List 1, if it exists in List 2, we add the numbers together to get List 3.

combining two lists

There's a function in Clojure called merge-with that does that. It takes a function and two hash maps. It will return a new map that has all of the keys of the two argument maps. And when the same key exists in both maps, it will apply the function to combine the two values. If we pass in + as the function, we get what we just described and we can take two shopping lists and combine them into one bigger shopping list.

Exercise 6

Write a function add-ingredients which takes two ingredient lists and adds them together using merge-with.

To navigate to this point in the introduction-to-clojure repo:

$CMD git checkout -f 2.14

Watch me

This function should be easy since it uses an existing function. It takes two ingredient lists, a and b.

(defn add-ingredients [a b]
  (merge-with + a b))

To navigate to this point in the introduction-to-clojure repo:

$CMD git checkout -f 2.15

Code is available: lispcast/introduction-to-clojure

Code for this particular lesson is available at the 2.14 branch.

You can checkout the code in your local repo with this command:

$CMD git clone https://github.com/lispcast/introduction-to-clojure.git 
$CMD cd introduction-to-clojure
$CMD git checkout -f 2.14        

Next Lesson:

Into

6 min

We also want to be able to multiply an ingredient list. For instance, if we need to make 3 cakes, we can multiply the ingredients for a cake by 3. That's not quite as easy as adding, but it's not hard. We just need a few more things.

Introduction to Clojure v2

0 / 60
0 / 60
 
Introduction
free  
12 min 
 
Introducing X5 and JC
free  
2 min 
 
Working with the REPL
free  
14 min 
 
Defining functions
 
7 min 
 
Defining a bake-cake function
 
2 min 
 
Conditionals
 
10 min 
 
Defining scooped?
 
2 min 
 
Defining squeezed?
 
3 min 
 
Defining simple?
 
1 min 
 
Defining ingredient types
 
11 min 
 
Looping a number of times
 
10 min 
 
Using the plural functions
 
2 min 
 
Variadic functions
 
15 min 
 
Baking with variadic functions
 
2 min 
 
Bake cookies function
 
2 min 
 
Day 1 Conclusion
 
2 min 
 
Running Leiningen projects
 
11 min 
 
Error function
 
11 min 
 
Refactoring errors
 
4 min 
 
Bakery locations
 
14 min 
 
Scooped?, squeezed?, and simple? refactor
 
2 min 
 
Fetching ingredients
 
9 min 
 
Fetch ingredients generic
 
3 min 
 
Maps
 
7 min 
 
Fetching a shopping list
 
10 min 
 
Refactoring fetch-list
 
10 min 
 
Refactoring locations
 
9 min 
 
A day at the bakery
 
20 min 
 
Analyzing a day at the bakery
 
3 min 
 
Functional programming
 
10 min 
 
Into
 
6 min 
 
multiply-ingredients
 
7 min 
 
order to ingredients
 
5 min 
 
orders to ingredients
 
8 min 
 
Making one shopping trip
 
3 min 
 
Making one delivery
 
12 min 
 
Day 2 conclusion
 
3 min 
 
Day 3
 
2 min 
 
Add cocoa
 
2 min 
 
Baking brownies
 
6 min 
 
Analyzing recipes
 
6 min 
 
Starting the database
 
2 min 
 
Perform
 
1 min 
 
Mix step
 
1 min 
 
Pouring and baking
 
1 min 
 
Adding ingredients
 
7 min 
 
Bake recipe
 
4 min 
 
Generic bake function
 
3 min 
 
Adding all recipes
 
5 min 
 
Cleaning up order to ingredients
 
6 min 
 
Ingredients in the database
 
3 min 
 
Refactoring scooped, squeezed, and simple
 
2 min 
 
Generic fetch ingredient
 
9 min 
 
Refactoring fetch-list (again)
 
10 min 
 
Make storage-location more useful
 
4 min 
 
Redefining add
 
9 min 
 
Defining actions
 
4 min 
 
Refactoring perform
 
4 min 
 
Conclusions
 
5 min 
 
Appendix: Creating a Leiningen project
 
10 min 

Footer CTA

Get the newsletter for free

The PurelyFunctional.tv Newsletter is a weekly email to inspire functional programmers.

Enter your email address to receive emails about Clojure and Functional Programming. These include the weekly newsletter and other great offers. You can unsubscribe any time.


Copyright © 2019 LispCast and Eric Normand