Skip to content

Commit ec5d273

Browse files
committed
About koans
About first class functions Signed-off-by: David Laing <david@davidlaing.com>
1 parent d86a9df commit ec5d273

File tree

7 files changed

+4608
-0
lines changed

7 files changed

+4608
-0
lines changed

index.html

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
<html>
2+
<head>
3+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
4+
<title>Learning Advanced JavaScript</title>
5+
<link href="lib/presentation/presentation.css" rel="stylesheet"/>
6+
<link rel="stylesheet" href="lib/presentation/recipes.css">
7+
<script src="lib/presentation/jquery.js"></script>
8+
<script src="lib/presentation/jquery.chili.js"></script>
9+
<script src="lib/presentation/recipes.js"></script>
10+
<script src="lib/presentation/presentation.js"></script>
11+
12+
<script type="text/javascript" src="lib/underscore-min.js"></script>
13+
<script type="text/javascript" src="lib/FILL_ME_IN.js"></script>
14+
15+
<script>
16+
function assert(pass, msg) {
17+
var type = pass ? "PASS" : "FAIL";
18+
var karma = pass ? "Your awareness has been expanded": "Your karma is being damaged...";
19+
jQuery("#results").append("<li class='" + type + "'><b>" + karma + "</b> " + msg + "</li>");
20+
}
21+
function error(msg) {
22+
jQuery("#results").append("<li class='ERROR'><b>Your karma is being damaged...</b> " + msg + "</li>");
23+
}
24+
function log() {
25+
var msg = "";
26+
for (var i = 0; i < arguments.length; i++) {
27+
msg += " " + arguments[i];
28+
}
29+
jQuery("#results").append("<li class='LOG'><b>LOG</b> " + msg + "</li>");
30+
}
31+
</script>
32+
</head>
33+
<body>
34+
35+
<h3 class="">Javascript function programming koans</h3>
36+
37+
<div id="area">
38+
<pre id="pre" class="javascript" style="height: 425px; ">
39+
<a href="#1">1) Our Goal</a>
40+
41+
</pre>
42+
<form id="form">
43+
<div class="buttons" style="display: none; ">
44+
<input type="submit" value="Meditate" class="run" id="run">
45+
<input type="button" id="prev" value="« Prev">
46+
<input type="button" id="next" value="Next »">
47+
</div>
48+
<div id="container">
49+
<textarea id="code" wrap="off" style="height: 425px; "></textarea>
50+
<ol id="results"></ol>
51+
<p id="cite">Double-click the code to edit the tutorial and try your own code.
52+
<br>John Resig increased his karma by <a href="http://ejohn.org/apps/learn/">creating this tutorial framework</a></p>
53+
</div>
54+
</form>
55+
</div>
56+
57+
<dl style="display: none; ">
58+
<!-- Slide 1 -->
59+
<dt>Our Goal</dt>
60+
<dd></dd>
61+
62+
<!-- Slide 2 -->
63+
<dt>Our Goal > Enlightenment through functional programming</dt>
64+
<dd><pre>
65+
66+
/*
67+
* Today we will expand our awareness of:
68+
*
69+
* (1) First class functions
70+
*
71+
* (2) Higher order functions and collections
72+
*
73+
* (3) Continuations
74+
*
75+
* by meditating on a series of koans
76+
*/
77+
</pre></dd>
78+
79+
<!-- Slide 3 -->
80+
<dt>What are these koans of which you speak master?</dt>
81+
<dd><pre>
82+
//Koans help us contemplate truth by testing reality.
83+
assert(false, "This should be true" );
84+
85+
//To understand reality, we must compare our assertions against reality.
86+
assert( ( 1 + 1 ) === FILL_ME_IN);
87+
88+
/*
89+
* Double click here to edit reality
90+
*
91+
* Then click [Meditate] to test whether you have gained enlightenment
92+
*/
93+
</pre></dd>
94+
95+
<!-- Slide 3 -->
96+
<dt>How koans work (2)</dt>
97+
<dd><pre>
98+
//Sometimes we just want to see what's going on
99+
var theAnswer = 6 * 7;
100+
log( "Just a simple log", "of", "values.", theAnswer );
101+
102+
//And other times we want to avoid causing an error
103+
if (6 * 7 !== FILL_ME_IN ) {
104+
error( "I'm an error!" );
105+
} else {
106+
assert(true);
107+
}
108+
</pre></dd>
109+
110+
<!-- Slide 1 -->
111+
<dt>First class functions</dt>
112+
<dd></dd>
113+
114+
<!-- Slide 4 -->
115+
<dt>Let us meditate on first class functions</dt>
116+
<dd><pre>
117+
/*
118+
* A language supports first-class functions when:
119+
*
120+
* (1) You can bind an identifier to the function,
121+
* i.e. you can give it a name
122+
*
123+
* (2) You can store the function in a data structure (e.g: a list)
124+
*
125+
* (3) You can pass the function as an argument in another function call
126+
*
127+
* (4) You can return a function from another function
128+
*/
129+
</pre></dd>
130+
131+
<!-- Slide 5 -->
132+
<dt>Giving functions names</dt>
133+
<dd><pre>
134+
var add = function(x, y) {
135+
return x + y;
136+
};
137+
138+
var result = add(2, 3);
139+
assert(result === FILL_ME_IN, "Execute a named function using ()");
140+
141+
var alsoAdd = add;
142+
143+
assert(alsoAdd(1, 6) === FILL_ME_IN, "Named functions behave just like other variables");
144+
</pre></dd>
145+
146+
<!-- Slide 6 -->
147+
<dt>Storing functions in data structures</dt>
148+
<dd><pre>
149+
var result = 0;
150+
151+
var add = function(x, y) {
152+
return x + y;
153+
};
154+
155+
var subtract = function(x, y) {
156+
return x - y;
157+
};
158+
159+
var operations = [add, subtract, add, subtract, subtract, add, add];
160+
161+
for (i in operations) {
162+
result = operations[i](result,5);
163+
// log("result is now:", result);
164+
}
165+
166+
assert(result === FILL_ME_IN, "what is the final result?");
167+
</pre></dd>
168+
169+
<!-- Slide 7 -->
170+
<dt>Passing a function as an argument to another function call</dt>
171+
<dd><pre>
172+
var afterMeditation = function(knowledge) {
173+
assert(knowledge === FILL_ME_IN, "what knowledge has been accumulated");
174+
};
175+
176+
var thinkLongAndHard = function(fact1, fact2, afterThinking) {
177+
var result = fact1 + ';' + fact2;
178+
afterThinking(result);
179+
};
180+
181+
thinkLongAndHard('wind', 'rain', afterMeditation);
182+
</pre></dd>
183+
184+
<!-- Slide 8 -->
185+
<dt>You can return a function from another function</dt>
186+
<dd><pre>
187+
var powerFactory = function(power) {
188+
return function(number) {
189+
var result = 1;
190+
for(var i = 0; i < power; i++) {
191+
result = result * number;
192+
}
193+
return result;
194+
}
195+
};
196+
197+
var squared = powerFactory(2);
198+
var cubed = powerFactory(3);
199+
200+
assert(squared(2) + cubed(3) === FILL_ME_IN, "functions can create new functions");
201+
</pre></dd>
202+
203+
<!-- Slide 1 -->
204+
<dt>Higher order functions</dt>
205+
<dd></dd>
206+
207+
<!-- Slide 9 -->
208+
<dt>First class functions make common operations with collections easy</dt>
209+
<dd><pre>
210+
/*
211+
* The following are known as higher order functions:
212+
*
213+
* (1) each
214+
* (2) map
215+
* (3) filter
216+
* (4) reduce
217+
* (5) all
218+
* (6) any
219+
* (7) range
220+
* (8) flatten
221+
*
222+
* because the apply a function to a list in a common way.
223+
*
224+
*/
225+
</pre></dd>
226+
227+
<!-- Slide 10 -->
228+
<dt>Each</dt>
229+
<dd><pre>
230+
/* Iterates over a list of elements, yielding each in turn to an iterator function. */
231+
var numbers = [1,2,3];
232+
var msg = "";
233+
var isEven = function(item) {
234+
msg += (item % 2) === 0;
235+
};
236+
237+
_.each(numbers, isEven);
238+
239+
assert(msg === FILL_ME_IN, "isEven was called once for each element");
240+
assert(numbers === FILL_ME_IN, "but the original list wasn't touched");
241+
</pre></dd>
242+
243+
244+
<!-- Slide 9 -->
245+
<dt>Continuations</dt>
246+
<dd><pre>
247+
/*TODO*/
248+
</pre></dd>
249+
250+
</dl>
251+
</body>
252+
</html>

0 commit comments

Comments
 (0)