Skip to content

Commit

Permalink
Merge pull request ClojureBridge#6 from echeran/master
Browse files Browse the repository at this point in the history
more changes from SF May 2014 workshop
  • Loading branch information
seancorfield committed May 14, 2014
2 parents 642b724 + 7352f99 commit c90b347
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 60 deletions.
2 changes: 1 addition & 1 deletion outline/data_structures.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ Now, take a look at the last three functions. `count` does what you'd expect: it

### EXERCISE: Make a vector

Make a vector of the average high temperatues in each month of the year in the town where you live. Then use the `nth` function to get the average high temperature for the current month from the vector.
Make a vector of the high temperatues for the next 7 days in the town where you live. Then use the `nth` function to get the high temperature for next Tuesday.
7 changes: 4 additions & 3 deletions outline/data_structures2.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ We write maps by enclosing alternating keys and values in curly braces, like so:
{}
```

As you may have noticed by now, in Clojure, commas are optional. Similar to comments, commas are ignored by the REPL. If you see commas in Clojure code, they often occur between key-value pairs in a map definition.

Maps are useful because they can hold data in a way we normally think about it. Take our made up example, Sally Brown. A map can hold her first name and last name, her address, her favorite food, or anything else. It's a simple way to collect that data and make it easy to look up. That last example? It is an empty map. It is a map that is ready to hold some things, but doesn't have anything in it yet.

Let's look at some functions we can use with maps:
Expand Down Expand Up @@ -49,9 +51,6 @@ Let's look at some functions we can use with maps:

(vals {:first "Sally" :last "Brown"})
;=> ("Sally" "Brown")

(into {} [[1 2] [3 4]])
;=> {1 2, 3 4}
```

We don't have nearly as many functions here in common as vectors and lists did.
Expand Down Expand Up @@ -104,6 +103,8 @@ Here is an example of how it should work:
;=> ["Margaret Atwood" "Doris Lessing" "Ursula Le Guin" "Alice Munro"]
```

Hint: First, create a function that returns the name when given a single person's map.

### EXERCISE: Modeling your classmates

First, take the map you made about yourself.
Expand Down
35 changes: 18 additions & 17 deletions outline/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ A _function_ is an independent, discrete piece of code that takes in some values

```clj
(defn total-bill
"Given the subtotal of a bill, return the total after tax."
"Given subtotal of bill, return total after tax."
[subtotal]
(* 1.085 subtotal))
(* 1.08 subtotal))
```

In this code:
Expand All @@ -26,25 +26,26 @@ In this code:
* `total-bill` is the name of this function.
* The string on the next line is the documentation for the function, which explains what the function does. This is optional.
* `[subtotal]` is the list of arguments. Here, we have one argument called `subtotal`.
* `(* 1.085 subtotal)` is the _body_ of the function. This is what executes when we use the function.
* `(* 1.08 subtotal)` is the _body_ of the function. This is what executes when we use the function.

To use `total-bill`, we _call_ the function, just like we've done with all the functions we've already used.

```clj
(total-bill 8.90) ;=> 9.6565
(total-bill 50) ;=> 54.25
(total-bill 50/7) ;=> 7.75
(total-bill 8.5) ;=> 9.18
(total-bill 50) ;=> 54.0
(total-bill 50/6) ;=> 9.0
```

Functions can also take more than one argument. Let's make a `total-with-tip` function that additionally takes a tip percentage and calculates the total amount paid:

```clj
(defn total-with-tip
"Given subtotal, return total after tax and tip."
[subtotal tip-pct]
(* 1.085 subtotal (+ 1 tip-pct)))
(* 1.08 subtotal (+ 1 tip-pct)))

(total-with-tip 8.90 0.18) ;=> 11.3946999
(total-with-tip 50 0.18) ;=> 64.015
(total-with-tip 12.50 0.18) ;=> 15.93
(total-with-tip 50 0.18) ;=> 63.72
```

### EXERCISE: Find per-person share of bill among a group
Expand Down Expand Up @@ -88,11 +89,11 @@ Some of the most powerful functions you can use with collections can take other
One of the most magical things about Clojure--and many other programming languages--is that it can have functions that take other functions as arguments. That may not make sense at first, so let's look at an example:

```clj
(def dine-in-orders [12.40 18.95 23.81 19.95 12.40])
(def dine-in-orders [12.50 20 21 16 18.40])
(def take-out-orders[6.00 6.00 7.95 6.25])

(map total-bill dine-in-orders) ;=> [13.454 20.56075 25.833849999999998 21.64575 13.454]
(map total-bill take-out-orders) ;=> [6.51 6.51 8.62575 6.78125]
(map total-bill dine-in-orders) ;=> [13.5 21.6 22.68 17.28 19.872]
(map total-bill take-out-orders) ;=> [6.48 6.48 8.586 6.75]
```

`map` is a function that takes another function, along with a collection. It calls the function provided to it on each member of the collection, then returns a new collection with the results of those function calls. This is a weird concept, but it is at the core of Clojure and functional programming in general.
Expand All @@ -112,15 +113,15 @@ Let's look at another function that takes a function. This one is `reduce`, and
This process is complicated, so let's illustrate it further.

```clj
(def take-out-totals [6.51 6.51 8.62575 6.78125])
(def take-out-totals [6.48 6.48 8.586 6.75])

(reduce add take-out-totals) ;=> 28.427
(reduce add take-out-totals) ;=> 28.296
```

In the example above, `reduce` calls `add` with the parameters `6.51` and `6.51`, returning `13.02`. Then, in order, it makes the following function calls:
In the example above, `reduce` calls `add` with the parameters `6.48` and `6.48`, returning `12.96`. Then, in order, it makes the following function calls:

* `(add 13.02 8.62575) ;=> 21.64575`
* `(add 21.64575 6.78125) ;=> 28.427`
* `(add 12.96 8.586) ;=> 21.546`
* `(add 21.546 6.75) ;=> 28.296`

### EXERCISE: Find the average

Expand Down
10 changes: 6 additions & 4 deletions outline/functions2.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ There are some functions that are essential when using Clojure. The arithmetic f

### Comparison (boolean) functions

You can use the function `=` to test the equality of two things. For example, here is a function called `vegetarian?` that determines whether a person is vegetarian or not:
You can use the function `=` to test the equality of two things. For example, here is a function called `meaning-of-life?` that determines whether the input is `42` or not:

```clj
(defn vegetarian?
[person]
(= :vegetarian (get person :dietary-restrictions)))
(defn meaning-of-life?
[x]
(= x 42))
```

The other comparison functions are `>`, `>=`, `<`, `<=`, and `not=`, and all but the last of these are used exclusively with numbers. Like all Clojure functions, the comparison functions are used as prefixes, so they can be a little tricky. Here's some examples:
Expand All @@ -27,6 +27,8 @@ The other comparison functions are `>`, `>=`, `<`, `<=`, and `not=`, and all but
(>= 4 5) ;=> false
(< -1 1) ;=> true
(<= -1 -2) ;=> false
(< 1 5 9) ;=> true
(< 1 5 3) ;=> false
```

### String functions
Expand Down
12 changes: 12 additions & 0 deletions outline/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ What is the function on the next line? What are its arguments? What do you think

How about the last line? That has a thing called a string in it, which we'll learn more about later. Looks like someone is hungry.

### Comments

When we write code, we try to make it as clear as possible. Doing so is a huge advantage because our code gets read by others (oftentimes more so than by us!), or we come back to our own code to read it later by which point we may have forgotten each exact detail of the code. One way that we can clarify our code is annotating it with comments. Comments are notes that we add to code, for our own sake, that the computer ignores.

In Clojure, comments can be started with a semicolon. Everything after the semicolon until the end of a line is a comment and gets ignored by the computer. Only one semicolon is necessary, but sometimes you see two semicolons in a row depending on stylistic tastes.

```clojure
;; more food code
(eat "cookie") ; nom nom nom
(eat "donut") ; mmm donuts
```

## What is the REPL?

"REPL" stands for "Read-Eval-Print-Loop," which still doesn't make a ton of sense without context. Many programming languages, including Clojure, have a way to execute code interactively so you get instant feedback. In other words, the code is read, then it is evaluated, then the result is printed, and you begin again--thus, a loop.
Expand Down
2 changes: 1 addition & 1 deletion outline/simple_values2.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Keywords are the strangest of the basic value types, because they don't have a r

### EXERCISE: Store the name of your hometown

Write the name of your hometown as a string, and then assign that string to the symbol `my-hometown`.
Write the name of your hometown as a string, and then assign that string to the name `my-hometown`.

### EXERCISE: Make a function to format names

Expand Down
9 changes: 9 additions & 0 deletions slides/src/module1.cljs.hl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@
(eat "sandwich")
;;}}
))
(slide
:title "Comments"
(highlight
;;{{
;; more food code
(eat "cookie") ; nom nom nom
(eat "donut") ; mmm donuts
;;}}
))
(slide
:title "What is a REPL?"
(div
Expand Down
2 changes: 1 addition & 1 deletion slides/src/module2.cljs.hl
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@
))
(slide
:title "Exercise: Make a vector"
(p "Make a vector of the average high temperatues in each month of the year in the town where you live. Then use the " (code "nth") " function to get the average high temperature for the current month from the vector."))))))
(p "Make a vector of the high temperatues for the next 7 days in the town where you live. Then use the " (code "nth") " function to get the high temperature for next Tuesday."))))))
45 changes: 24 additions & 21 deletions slides/src/module3.cljs.hl
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
(hlclj
;;{{
(defn total-bill
"Given the subtotal of a bill, return the total after tax."
"Given subtotal of bill, return total after tax."
[subtotal]
(* 1.085 subtotal))
(* 1.08 subtotal))

(total-bill 8.90) ;=> 9.6565
(total-bill 50) ;=> 54.25
(total-bill 50/7) ;=> 7.75
(total-bill 8.5) ;=> 9.18
(total-bill 50) ;=> 54.0
(total-bill 50/6) ;=> 9.0
;;}}
))
(slide
Expand All @@ -52,23 +52,24 @@
total-bill ; the name of this function

;; documentation, optional
"Given the subtotal of a bill, return the total..."
"Given subtotal of bill, return total..."

[x] ; list of arguments

;; body of function
(* 1.085 subtotal))
(* 1.08 subtotal))
;;}}
))
(slide
:title "A function with multiple arguments."
(hlclj
;;{{
(defn total-with-tip
"Given subtotal, return total after tax and tip."
[subtotal tip-pct] ;; takes 2 arguments
(* 1.085 subtotal (+ 1 tip-pct)))
(* 1.08 subtotal (+ 1 tip-pct)))

(total-with-tip 50 0.18) ;=> 64.015
(total-with-tip 12 0.18) ;=> 15.93
;;}}
))
(slide
Expand All @@ -90,11 +91,13 @@
:title (h3 (code "map"))
(hlclj
;;{{
(def dine-in-orders [12.40 18.95 23.81 19.95 12.40])
(def dine-in-orders [12.50 20 21 16 18.40])
(def take-out-orders[6.00 6.00 7.95 6.25])

(map total-bill dine-in-orders) ;=> [13.454 20.56075 25.833849999999998 21.64575 13.454]
(map total-bill take-out-orders) ;=> [6.51 6.51 8.62575 6.78125]
(map total-bill dine-in-orders)
;=> [13.5 21.6 22.68 17.28 19.872]
(map total-bill take-out-orders)
;=> [6.48 6.48 8.586 6.75]
;;}}
))
(slide
Expand All @@ -112,21 +115,21 @@
:title (h3 (code "reduce") " in action")
(hlclj
;;{{
(def take-out-totals [6.51 6.51 8.62575 6.78125])

(reduce add take-out-totals) ;=> 28.427
(def take-out-totals [6.48 6.48 8.586 6.75])
(reduce add take-out-totals) ;=> 28.296
;;}}
))
(slide
:title (h3 (code "reduce") " in action")
(hlclj
;;{{
(def take-out-totals [6.51 6.51 8.62575 6.78125])
(reduce add take-out-totals) ;=> 28.427
;;{{
(def take-out-totals [6.48 6.48 8.586 6.75])
(reduce add take-out-totals) ;=> 28.296

(add 6.51 6.51)
(add 13.02 8.62575)
(add 21.64575 6.78125)
(add 6.48 6.48) ;=> 12.96
(add 12.96 8.586) ;=> 21.546
(add 21.546 6.75) ;=> 28.296
;;}}
))

Expand Down
6 changes: 5 additions & 1 deletion slides/src/module4.cljs.hl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
;;{{
true
false
;;}}
)
(highlight :class "language-clojure"
;;{{
nil
;;}}
))
Expand All @@ -47,7 +51,7 @@
))
(slide
:title "Exercise: Store the name of your hometown"
(p "Write the name of your hometown as a string and then assign that string to the symbol "
(p "Write the name of your hometown as a string and then assign that string to the name "
(code "my-hometown")
"."))
(slide
Expand Down
10 changes: 6 additions & 4 deletions slides/src/module5.cljs.hl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
(intro
:title "Module 5")
(chapter
:title "Function library"
:title "Common, core functions"
(slide
:title "Comparison functions"
(hlclj
Expand All @@ -33,15 +33,17 @@
(>= 4 5) ;=> false
(< -1 1) ;=> true
(<= -1 -2) ;=> false
(< 1 5 9) ;=> true
(< 1 5 3) ;=> false
;;}}
))
(slide
:title "Using comparison functions"
(hlclj
;;{{
(defn vegetarian?
[person]
(= :vegetarian (get person :dietary-restrictions)))
(defn meaning-of-life?
[x]
(= x 42))
;;}}
))
(slide
Expand Down
13 changes: 6 additions & 7 deletions slides/src/module6.cljs.hl
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@
:title "Maps"
(hlclj
;;{{
{:first "Sally", :last "Brown", :occupation "programmer"}
{:first "Sally", :last "Brown", :job "programmer"}
{:a 1, :b "two"}
{}
;;}}
))
)
(ul (li "Similar to comments, commas are ignored by REPL")))
(slide
:title "Map functions"
(hlclj
Expand Down Expand Up @@ -77,9 +78,6 @@

(vals {:first "Sally" :last "Brown"})
;;=> ("Sally" "Brown")

(into {} [[1 2] [3 4]])
;;=> {1 2, 3 4}
;;}}
))
(slide
Expand Down Expand Up @@ -127,11 +125,12 @@
;;=> ["Margaret Atwood" "Doris Lessing"
;; "Ursula Le Guin" "Alice Munro"]
;;}}
))
)
(bullet "Hint: First, create a function that returns the name when given a single person's map."))
(slide
:title "Exercise: Modeling your classmates"
(ol
(li "Take the map from the last exercise -- the one about you.")
(li "Take the map from a recent exercise -- the one about you.")
(li "Find two or three classmates. Ask their name and hometown. "
" Make a vector of maps with their information.")
(li "Then, add your information to their information using "
Expand Down

0 comments on commit c90b347

Please sign in to comment.