forked from clj-syd/curriculum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule3.html
190 lines (153 loc) · 5.18 KB
/
module3.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<!DOCTYPE html>
<html>
<head>
<meta name="generator" content=
"HTML Tidy for HTML5 (experimental) for Mac OS X https://github.com/w3c/tidy-html5/tree/c63cc39">
<title>ClojureBridge slides</title>
<link rel="stylesheet" href="assets/reveal.js/css/reveal.css">
<link rel="stylesheet" href="assets/mozilla-theme.css" id=
"theme">
<link rel="stylesheet" href=
"assets/reveal.js/plugin/highlight/github.min.css">
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1 class="slide-title intro">Module 3</h1>
</section>
<section>
<h2 class="slide-title chapter">Functions</h2>
</section>
<section>
<h3 class="slide-title slide">What are functions?</h3>
<ul>
<li><code>count</code>, <code>conj</code>,
<code>first</code></li>
<li><code>+</code>, <code>-</code>, <code>*</code>,
<code>/</code></li>
<li>A piece of code that takes values and returns a
value</li>
</ul>
</section>
<section>
<h3 class="slide-title slide">An example function</h3>
<pre>
<code class="clojure">(defn total-bill
"Given subtotal of bill, return total after tax."
[subtotal]
(* 1.08 subtotal))
(total-bill 8.5) ;=> 9.18
(total-bill 50) ;=> 54.0
(total-bill 50/6) ;=> 9.0</code></pre>
</section>
<section>
<h3 class="slide-title slide">An example function</h3>
<pre>
<code class=
"clojure">(defn ; specifies that we are defining a function
total-bill ; the name of this function
;; documentation, optional
"Given subtotal of bill, return total..."
[x] ; list of arguments
;; body of function
(* 1.08 subtotal))</code></pre>
</section>
<section>
<h3 class="slide-title slide">A function with multiple
arguments.</h3>
<pre>
<code class="clojure">(defn total-with-tip
"Given subtotal, return total after tax and tip."
[subtotal tip-pct] ;; takes 2 arguments
(* 1.08 subtotal (+ 1 tip-pct)))
(total-with-tip 12 0.18) ;=> 15.93</code></pre>
</section>
<section>
<h3 class="slide-title slide">Find per-person share of
bill among a group</h3>
<p>Modify our <code>total-with-tip</code> function, and
call the new function <code>share-per-person</code>, that
additionally takes in as an argument the number of people
in the group for a bill.</p>
<p>Have the function return the average bill amount per
person.</p>
</section>
<section>
<h3 class="slide-title slide">Naming functions</h3>
<ul>
<li>Functions are named like any other value</li>
<li>
<p>Predicate functions usually end in
<code>?</code></p>
<ul>
<li><code>zero?</code></li>
<li><code>vector?</code></li>
<li><code>empty?</code></li>
</ul>
</li>
</ul>
</section>
<section>
<h2 class="slide-title chapter">Functions that take other
functions</h2>
</section>
<section>
<h3><code>map</code></h3>
<pre>
<code class="clojure">(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.5 21.6 22.68 17.28 19.872]
(map total-bill take-out-orders)
;=> [6.48 6.48 8.586 6.75]</code></pre>
</section>
<section>
<h3><code>reduce</code></h3>
<pre>
<code class="clojure">(defn add
[x y]
(+ x y))
(reduce add [1 2 3]) ;=> 6</code></pre>
</section>
<section>
<h3><code>reduce</code> in action</h3>
<pre>
<code class="clojure">(def take-out-totals [6.48 6.48 8.586 6.75])
(reduce add take-out-totals) ;=> 28.296</code></pre>
</section>
<section>
<h3><code>reduce</code> in action</h3>
<pre>
<code class="clojure">(def take-out-totals [6.48 6.48 8.586 6.75])
(reduce add take-out-totals) ;=> 28.296
(add 6.48 6.48) ;=> 12.96
(add 12.96 8.586) ;=> 21.546
(add 21.546 6.75) ;=> 28.296</code></pre>
</section>
<section>
<h3 class="slide-title slide">Exercise: Find the
average</h3>
<p>Create a function called <code>average</code> that
takes a vector of bill amounts and returns the average of
those amounts.</p>
<p>Hint: you will need to use <code>reduce</code> and
<code>count</code>.</p>
</section>
</div>
</div><script src="assets/reveal.js/lib/js/head.min.js">
</script> <script src="assets/reveal.js/js/reveal.js">
</script> <script>
Reveal.initialize({
history: true,
dependencies: [
{src: "assets/reveal.js/plugin/markdown/marked.js"},
{src: "assets/reveal.js/plugin/markdown/markdown.js"},
{src: "assets/reveal.js/plugin/highlight/highlight.js",
async: true,
callback: function () { hljs.initHighlightingOnLoad()}},
{src: "assets/reveal.js/plugin/notes/notes.js"}
]});
</script>
</body>
</html>