-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
1220 lines (980 loc) · 36.9 KB
/
index.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Wizard Class</title>
<meta name="description" content="A framework for easily creating beautiful presentations using HTML">
<meta name="author" content="Hakim El Hattab">
<meta name="apple-mobile-web-app-capable" content="yes"/>
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"/>
<meta name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/sky.css" id="theme">
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<style>
section.bigcode {
width: 1440px !important;
margin-left: -240px
}
section.bigcode code {
max-height: 600px;
}
</style>
<!-- Printing and PDF exports -->
<script>
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match(/print-pdf/gi) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName('head')[0].appendChild(link);
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1>Wizard Class</h1>
<h4>with <a href="mailto:rskonnord@plos.org">Ryan Skonnord</a></h4>
</section>
<section>
<section>
<h2>The Wizard Book</h2>
</section>
<section>
<p>In 1985, some MIT professors wrote <i>Structure and Interpretation of Computer Programs</i>, known informally
as "SICP" or "the Wizard Book".</p>
<p class="fragment">This textbook was used for the introductory computer science course at MIT and UC Berkeley
for decades.</p>
<p class="fragment">An e-book edition is <a href="https://mitpress.mit.edu/sicp/">available for free</a> and
licensed under
<a href="http://creativecommons.org/licenses/by-sa/4.0/">Creative Commons BY-SA</a>.</p>
</section>
<section>
<img src="sicp-cover.jpg" alt="Structure and Interpretation of Computer Programs"/>
</section>
<section>
<p>My lecturer remarked (I'm paraphrasing) that, to this day, it covers the entire discipline of computer
science, aside from implementation details.</p>
<p class="fragment">It uses basic, but very elegant and powerful, concepts to explain a wide variety of topics
in
programming.</p>
<p class="fragment">These concepts are expressed in the language Scheme, a dialect of Lisp.</p>
</section>
<section>
<h3>Why wizards?</h3>
<blockquote>
“A computational process is indeed much like a sorcerer's idea of a spirit. It cannot be seen or
touched. It is not composed of matter at all. However, it is very real. It can perform intellectual work. It
can answer questions. It can affect the world...
</blockquote>
</section>
<section>
<h3>Why wizards?</h3>
<blockquote cite="https://mitpress.mit.edu/sicp/full-text/book/book-Z-H-9.html">
“The programs we use to conjure processes are like a sorcerer's spells. They are carefully composed from
symbolic expressions in arcane and esoteric programming languages that prescribe the tasks we want our
processes to perform.”
<br/>—<a href="https://mitpress.mit.edu/sicp/full-text/book/book-Z-H-9.html"><i>SICP</i>, Chapter 1</a>
</blockquote>
</section>
<section>
<p>A central theme of the book: code is data.</p>
<blockquote>“It is no exaggeration to regard this as the most fundamental idea in programming: The
evaluator, which determines the meaning of expressions in a programming language, is just another program.”
<br/>—<a href="https://mitpress.mit.edu/sicp/full-text/book/book-Z-H-25.html"><i>SICP</i>, Chapter 4</a>
</blockquote>
<p class="fragment">To illustrate this idea, the book spends a chapter implementing a Scheme interpreter in
Scheme.</p>
</section>
</section>
<section>
<section>
<h2>What are we doing here?</h2>
</section>
<section>
<p>Let's do something similar: build an abstract programming tool out of more basic, but elegant,
abstractions.</p>
<p class="fragment">I was inspired by something that the faculty at Berkeley had added on to the course
material: a library of
macros that extended Scheme with object-oriented features.</p>
<p class="fragment">Let's make our own classes.</p>
</section>
<section>
<p>I was also inspired by Matt Bowen's talk on abstraction layers.</p>
<p class="fragment">Whereas Matt delved through multiple layers, I want to take a microscope to a single
abstraction.</p>
</section>
<section>
<p>You might think of classes as magic: something that the programming language has to provide to us as a
feature.</p>
<p>Well...</p>
<p class="fragment">You're right. Classes are magic.</p>
<p class="fragment" style="font-size: 150%;">...and <strong>WE</strong> are the wizards!</p>
</section>
<section class="bigcode">
<pre><code class="ruby">class Person
attr_accessor :name
attr_accessor :weapon
def introduce
puts "Hello, my name is #{@name}."
end
def drink_tea
puts 'Slurp'
end
def fight(target)
unless @weapon.nil?
puts "#{@name} fights #{target.name}. #{@weapon.attack}"
end
end
end
class Wizard < Person
attr_accessor :hat_color
attr_accessor :is_humble
def introduce
if is_humble then
super
else
puts "Lo, behold, I am #{name} the #{hat_color}."
end
end
end
sam = Person.new
sam.name = 'Sam'
sam.introduce
gandalf = Wizard.new
gandalf.name = 'Gandalf'
gandalf.hat_color = 'Grey'
gandalf.introduce
saruman = Wizard.new
saruman.name = 'Saruman'
saruman.hat_color = 'White'
saruman.introduce
gandalf.is_humble = true
gandalf.introduce
class Staff
def attack
'Zap!'
end
end
class FireStaff < Staff
def attack
'Fwoosh! Kaboom!'
end
end
class FrostStaff < Staff
def attack
'Brr! Crack!'
end
end
gandalf.weapon = FireStaff.new
saruman.weapon = FrostStaff.new
gandalf.fight(saruman)
saruman.fight(gandalf)</code></pre>
</section>
</section>
<section>
<h2>Arcane Musings</h2>
</section>
<section>
<section>
<h3>Definition of an object</h3>
<p>An object has</p>
<ul>
<li>State</li>
<li>Behavior</li>
</ul>
</section>
<section>
<h3>Definition of object-oriented programming</h3>
<p>Uses objects with the properties of:</p>
<ul>
<li><strong>Encapsulation</strong>
<ul>
<li class="fragment">State that governs behavior is bundled up inside one object.</li>
<li class="fragment">Includes, but does not require, <strong>information hiding</strong>.</li>
</ul>
</li>
<li><strong>Polymorphism</strong>
<ul>
<li class="fragment">Multiple types of object that share behavior can be referred to with one common
interface.
</li>
</ul>
</li>
<li><strong>Inheritance</strong>
<ul>
<li class="fragment">Subclasses can extend a class, keeping the parent's state and behavior while adding
their own.
</li>
</ul>
</li>
</ul>
</section>
</section>
<section>
<section>
<h3>Definition of a class</h3>
<ul>
<li>A class defines the state and behavior that are common to a group of objects.</li>
<li class="fragment">A class is an object!</li>
<li class="fragment">The purpose of the class object is to construct other objects.
<ul>
<li class="fragment">This is called <em>instantiating</em> the class.</li>
<li class="fragment">The object made is called an <em>instance</em> of the class.</li>
</ul>
</li>
</ul>
</section>
<section>
<h3>Semantics</h3>
<p>Many languages don't model classes as objects.</p>
<p class="fragment">Many programmers use "object" and "instance" interchangeably.</p>
<p class="fragment">For that matter, many programmers use "instantiate" and "construct" interchangeably.</p>
</section>
<section>
<p>However, classes-as-objects isn't just an academic concept.</p>
<p class="fragment">This is very relevant if you use any of the metaprogramming features
<a href="http://stackoverflow.com/a/6581949">in Python</a> or
<a href="https://rubymonk.com/learning/books/2-metaprogramming-ruby">in Ruby.</a>
</p>
</section>
</section>
<section>
<section>
<h3>Encapsulation, Lisp-style</h3>
<p>Before OOP became a buzzword, the Wizard Book used the term
"<a href="https://mitpress.mit.edu/sicp/full-text/book/book-Z-H-13.html">data abstraction</a>"
to describe almost the same concept as encapsulation.</p>
<pre><code class="python">def make_rectangle(length, width):
return {'length': length, 'width': width}
def get_perimeter(rectangle):
return 2 * (rectangle['length'] + rectangle['width'])
def get_area(rectangle):
return rectangle['length'] * rectangle['width']
my_rectangle = make_rectangle(3, 5)
print('Perimeter is:', get_perimeter(my_rectangle))
print('Area is:', get_area(my_rectangle))</code></pre>
</section>
<section>
<h3>Encapsulation, Lisp-style</h3>
<ul>
<li>Define a set of functions to create and act on an object.</li>
<li class="fragment">In those functions, "privately" use any data structure you want to represent the object.
</li>
<li class="fragment">"Outsider" code uses those functions exclusively, and is agnostic to the data structure.
</li>
<li class="fragment">Ignoring the private implementation details is a matter of convention.</li>
<li class="fragment">A failure in such discipline is called "violating the data abstraction".</li>
</ul>
</section>
<section>
<h3>Encapsulation, Lisp-style</h3>
<p>If you do it right, you can swap out the functions' private behavior and everything that
uses them will work
the same.</p>
<pre><code class="python">def make_rectangle(length, width):
return {'olinguito': length, 'keeshond': width}
def get_perimeter(rectangle):
return 2 * (rectangle['olinguito'] + rectangle['keeshond'])
def get_area(rectangle):
return rectangle['olinguito'] * rectangle['keeshond']
my_rectangle = make_rectangle(3, 5)
print('Perimeter is:', get_perimeter(my_rectangle))
print('Area is:', get_area(my_rectangle))</code></pre>
</section>
</section>
<section>
<section>
<h3>First-class functions</h3>
<pre><code class="python">def square(x):
return x * x
def apply_twice_and_print(function, argument):
result = function(argument)
result = function(result)
print(result)
apply_twice_and_print(square, 4)</code></pre>
</section>
<section>
<h3>First-class functions</h3>
<p>A more realistic example: callbacks.</p>
<pre><code class="javascript">$("button").click(function() {
$.ajax({
url: "http://example.com/my-service",
success: function(result, status, xhr) {
var formatted = formatResult(result);
$("#display").html(formatted);
},
error: function(xhr, status, error) {
$("#display").html(sadFace);
alert(error );
}
});
});</code></pre>
</section>
</section>
<section>
<section>
<h3>Message passing</h3>
<p>Data abstractions + First-class functions = Power</p>
<p class="fragment">The <strong>message passing</strong> pattern gives us a way to bundle up both state and
multiple kinds of behavior in a single first-class value.</p>
</section>
<section>
<h3>Message passing in Python</h3>
<pre><code class="python">def make_rectangle(length, width):
def receive_message(message):
if message == 'get_perimeter':
return 2 * (length + width)
if message == 'get_area':
return length * width
else: raise
return receive_message
def get_perimeter(rectangle):
return rectangle('get_perimeter')
def get_area(rectangle):
return rectangle('get_area')
small_rectangle = make_rectangle(3, 5)
big_rectangle = make_rectangle(30, 50)
print(get_area(small_rectangle), get_area(big_rectangle))</code></pre>
<p class="fragment">Note that the calculation logic has moved from the "get" functions to the object itself.</p>
</section>
<section>
<p>Message passing isn't the kind of design pattern you use in everyday coding, but it's really what happens
under the covers of your favorite OOP language.</p>
<p class="fragment">In fact, you don't generally write your own message-passing functions because OOP represents
it so elegantly that you don't think about it.</p>
</section>
<section>
<p>No one go downstairs and check in code that looks like this.</p>
<pre><code class="java"> public void processArticle(String message, Article article) {
if ("ingest".equals(message)) {
// ...
} else if ("render".equals(message)) {
// ...
} else if ("delete".equals(message)) {
// ...
}
}</code></pre>
<p class="fragment">It will not pass code review.</p>
</section>
<section>
<p>Message passing gives us encapsulation: data is bundled with behavior.</p>
<p class="fragment">Message passing gives us polymorphism: you can create different types of object that
recognize common messages.</p>
<p class="fragment">We're two thirds of the way there!</p>
</section>
</section>
<section>
<section>
<h3>Closures</h3>
<ul>
<li>The rectangle functions were able to store information about their length and width.</li>
<li class="fragment">Different instances of the function stored different values.</li>
<li class="fragment">This is because they are not merely functions, but <strong>closures</strong>.</li>
<li class="fragment">A closure is a function that holds on to a reference to the variables that existed when
it was defined (its "environment").
</li>
</ul>
</section>
<section>
<h3>Closures in languages</h3>
<table>
<tr>
<th>Language</th>
<th>First-class functions?</th>
<th>Lambdas?</th>
<th>Closures?</th>
</tr>
<tr>
<td>Python, Ruby, JavaScript</td>
<td>Yes</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr>
<td>C</td>
<td>Sort of</td>
<td>No</td>
<td>No</td>
</tr>
<tr>
<td>Java</td>
<td>Fakes it pretty well</td>
<td>Yes,<br/>in Java 8!</td>
<td>Fakes it awkwardly</td>
</tr>
</table>
</section>
<section>
<p>In Java, what you see is this:</p>
<pre><code class="java"> public long getCount(final String name) {
return hibernateTemplate.execute(new HibernateCallback<Long>() {
@Override public Long doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery("select count(*) from Thing where name=:name");
query.setParameter("name", name);
return (Long) query.uniqueResult();
}
});
}</code></pre></section>
<section>
<p>What it's actually doing is this:</p>
<pre><code class="java"> public long getCount(final String name) {
return hibernateTemplate.execute(new InvisibleAnonymousClass(name));
}
private class InvisibleAnonymousClass implements HibernateCallback<Long> {
private final String name;
private InvisibleAnonymousClass(String name) {
this.name = name;
}
@Override public Long doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery("select count(*) from Thing where name=:name");
query.setParameter("name", name);
return (Long) query.uniqueResult();
}
}</code></pre>
<p class="fragment">Java forces you to declare <code>name</code> as <code>final</code> because you can't see
when it is passed to
the invisible constructor.
</p>
</section>
</section>
<section>
<section>
<h3>Wizard words</h3>
<p>More near-synonyms that are (ab)used interchangeably.</p>
<ul>
<li class="fragment"><strong>First-class function:</strong> A function that can be stored and passed around as
a value.
</li>
<li class="fragment"><strong>Anonymous function:</strong> A first-class function declared within an
expression, rather than in
a definition block where it's given a name.
</li>
<li class="fragment"><strong>Lambda (λ):</strong> An operator for declaring an anonymous function.</li>
<li class="fragment"><strong>Closure:</strong> A first-class function that's equipped with an
<em>environment</em> (table of
variables).
</li>
</ul>
</section>
<section>
<pre><code class="python">def square(x):
return x * x
map(square, # First-class, but not anonymous
[1, 2, 3])
map((lambda x: x * x), # Anonymous
[1, 2, 3])</code></pre>
</section>
</section>
<section>
<section>
<h3>Ground rules</h3>
<p>Obviously it would defeat the purpose to use Ruby's object-oriented features to implement
object-oriented
programming.</p>
<p class="fragment">But there will be a few exceptions...</p>
</section>
<section>
<h3>Ground rules</h3>
<p>I get to call the <code>call</code> method on a first-class function.</p>
<p class="fragment">Ruby's syntax doesn't otherwise make it possible to distinguish between referring to a
lambda-defined
function and calling it.</p>
</section>
<section>
<h3>Ground rules</h3>
<p>I get to use primitive types and basic data structures (<code>Array</code> and <code>Hash</code>), including
calling their methods.</p>
<ul>
<li class="fragment">If we really wanted to, we could implement these from scratch.</li>
<li class="fragment">Message passing and closures are enough for the basic building blocks.</li>
<li class="fragment">But, in the interests of time and sanity, we'll use Ruby's nice ones instead.</li>
</ul>
</section>
<section>
<h3>Ground rules</h3>
<ul>
<li>I am using Ruby to make basic classes, not making Ruby classes.
<ul>
<li class="fragment">Ruby classes do awesome things.</li>
<li class="fragment" style="font-size: 80%;">awe·some /ˈôsəm/ <i>adj.</i> — extremely impressive or
daunting;
inspiring great admiration, apprehension, or fear.
</li>
</ul>
</li>
</ul>
</section>
<section>
<h3>Ground rules</h3>
<p>And a few more caveats while I'm at it...</p>
<ul>
<li class="fragment">There will be mutations.
<ul>
<li class="fragment">A purely functional solution is left as an exercise for Haskell enthusiasts.</li>
</ul>
</li>
<li class="fragment">There will be no information hiding.
<ul>
<li class="fragment">Python doesn't do it.</li>
<li class="fragment">Open access!</li>
</ul>
</li>
</ul>
</section>
<section>
<h3>A few more disclaimers...</h3>
<p>It will be a little more verbose than native classes.</p>
<p class="fragment">Pretty syntax is a perk of having the language define classes for you.</p>
</section>
<section>
<h4>My, these are a lot of disclaimers</h4>
<p>Performance doesn't count.</p>
<ul>
<li class="fragment">
We can't specify details like array storage unless the programming language lets us.
</li>
<li class="fragment">“Lisp programmers know the value of everything and the cost of nothing.”
<br/>—<a href="http://pu.inf.uni-tuebingen.de/users/klaeren/epigrams.html">Alan Perlis, <i>Epigrams on
Programming</i>
</a></li>
</ul>
</section>
<section>
<h4>Prices and participation may vary.<br/>Void where prohibited.</h4>
<p>Also, no type-safety, error-handling, built-in methods, instance-of operator, overloading, or
constructors.</p>
</section>
</section>
<section>
<h2>Onto the main event!</h2>
</section>
<section class="bigcode"><pre><code class="ruby">def make_object(message_table)
return (lambda do |message, *message_args|
message_table[message].call(*message_args)
end)
end
def ask(object, message, *message_args)
return object.call(message, *message_args)
end</code></pre></section>
<section class="bigcode"><pre><code class="ruby">def make_object(message_table)
return (lambda do |message, *message_args|
message_table[message].call(*message_args)
end)
end
def ask(object, message, *message_args)
return object.call(message, *message_args)
end
def make_class()
class_message_table = {}
new_class = make_object(class_message_table)
class_message_table[:instantiate] = lambda do | |
instance_message_table = {}
new_instance = make_object(instance_message_table)
return new_instance
end
return new_class
end
Person = make_class()
sam = ask(Person, :instantiate)
puts sam</code></pre></section>
<section class="bigcode"><pre><code class="ruby">def make_object(message_table)
return (lambda do |message, *message_args|
message_table[message].call(*message_args)
end)
end
def ask(object, message, *message_args)
return object.call(message, *message_args)
end
def make_class()
class_message_table = {}
new_class = make_object(class_message_table)
class_message_table[:instantiate] = lambda do | |
fields = {}
instance_message_table = {}
new_instance = make_object(instance_message_table)
instance_message_table[:get_field] = lambda do |field_name|
return fields[field_name]
end
instance_message_table[:set_field] = lambda do |field_name, value|
fields[field_name] = value
end
return new_instance
end
return new_class
end
Person = make_class()
sam = ask(Person, :instantiate)
ask(sam, :set_field, :name, 'Sam')
puts ask(sam, :get_field, :name)</code></pre></section>
<section class="bigcode"><pre><code class="ruby">def make_object(message_table)
return (lambda do |message, *message_args|
message_table[message].call(*message_args)
end)
end
def ask(object, message, *message_args)
return object.call(message, *message_args)
end
def make_class(method_table)
class_message_table = {}
new_class = make_object(class_message_table)
class_message_table[:instantiate] = lambda do | |
fields = {}
instance_message_table = {}
new_instance = make_object(instance_message_table)
instance_message_table[:get_field] = lambda do |field_name|
return fields[field_name]
end
instance_message_table[:set_field] = lambda do |field_name, value|
fields[field_name] = value
end
instance_message_table[:call_method] = lambda do |method_name, *method_args|
method = method_table[method_name]
return method.call(new_instance, *method_args)
end
return new_instance
end
return new_class
end
Person = make_class({
:introduce => lambda do |this|
name = ask(this, :get_field, :name)
puts "Hello, my name is #{name}."
end,
})
sam = ask(Person, :instantiate)
ask(sam, :set_field, :name, 'Sam')
ask(sam, :call_method, :introduce)</code></pre></section>
<section class="bigcode"><pre><code class="ruby">def make_object(message_table)
return (lambda do |message, *message_args|
message_table[message].call(*message_args)
end)
end
def ask(object, message, *message_args)
return object.call(message, *message_args)
end
def make_class(parents, method_table)
class_message_table = {}
new_class = make_object(class_message_table)
class_message_table[:get_method] = lambda do |method_name|
method = nil
if method_table.has_key? method_name
method = method_table[method_name]
else
parents.each do |parent|
method = ask(parent, :get_method, method_name)
unless method.nil?
break
end
end
end
return method
end
class_message_table[:instantiate] = lambda do | |
fields = {}
instance_message_table = {}
new_instance = make_object(instance_message_table)
instance_message_table[:get_field] = lambda do |field_name|
return fields[field_name]
end
instance_message_table[:set_field] = lambda do |field_name, value|
fields[field_name] = value
end
instance_message_table[:call_method] = lambda do |method_name, *method_args|
method = ask(new_class, :get_method, method_name)
return method.call(new_instance, *method_args)
end
return new_instance
end
return new_class
end
Person = make_class([],
{
:introduce => lambda do |this|
name = ask(this, :get_field, :name)
puts "Hello, my name is #{name}."
end,
:drink_tea => lambda do |this|
puts 'Slurp'
end,
})
Wizard = make_class([Person],
{
:introduce => lambda do |this|
name = ask(this, :get_field, :name)
hat_color = ask(this, :get_field, :hat_color)
puts "Lo, behold, I am #{name} the #{hat_color}."
end
})
sam = ask(Person, :instantiate)
ask(sam, :set_field, :name, 'Sam')
ask(sam, :call_method, :introduce)
ask(sam, :call_method, :drink_tea)
gandalf = ask(Wizard, :instantiate)
ask(gandalf, :set_field, :name, 'Gandalf')
ask(gandalf, :set_field, :hat_color, 'Grey')
ask(gandalf, :call_method, :introduce)
ask(gandalf, :call_method, :drink_tea)</code></pre></section>
<section class="bigcode"><pre><code class="ruby">def make_object(message_table)
return (lambda do |message, *message_args|
message_table[message].call(*message_args)
end)
end
def ask(object, message, *message_args)
return object.call(message, *message_args)
end
def make_class(parents, method_table)
class_message_table = {}
new_class = make_object(class_message_table)
class_message_table[:get_method] = lambda do |method_name|
method = nil
if method_table.has_key? method_name
method = method_table[method_name]
else
parents.each do |parent|
method = ask(parent, :get_method, method_name)
unless method.nil?
break
end
end
end
return method
end
class_message_table[:get_usual_method] = lambda do |method_name|
return method_table[method_name]
end
class_message_table[:instantiate] = lambda do | |
fields = {}
instance_message_table = {}
new_instance = make_object(instance_message_table)
instance_message_table[:get_field] = lambda do |field_name|
return fields[field_name]
end
instance_message_table[:set_field] = lambda do |field_name, value|
fields[field_name] = value
end
instance_message_table[:call_method] = lambda do |method_name, *method_args|
method = ask(new_class, :get_method, method_name)
return method.call(new_instance, *method_args)
end
instance_message_table[:call_usual_method] = lambda do |class_obj, method_name, *method_args|
method = ask(class_obj, :get_usual_method, method_name)
return method.call(new_instance, *method_args)
end
return new_instance
end
return new_class
end
Person = make_class([],
{
:introduce => lambda do |this|
name = ask(this, :get_field, :name)
puts "Hello, my name is #{name}."
end,
:drink_tea => lambda do |this|
puts 'Slurp'
end,
})
Wizard = make_class([Person],
{
:introduce => lambda do |this|
if ask(this, :get_field, :is_humble) then
ask(this, :call_usual_method, Person, :introduce)
else
name = ask(this, :get_field, :name)
hat_color = ask(this, :get_field, :hat_color)
puts "Lo, behold, I am #{name} the #{hat_color}."
end
end
})
sam = ask(Person, :instantiate)
ask(sam, :set_field, :name, 'Sam')
ask(sam, :call_method, :introduce)
ask(sam, :call_method, :drink_tea)
gandalf = ask(Wizard, :instantiate)
ask(gandalf, :set_field, :name, 'Gandalf')
ask(gandalf, :set_field, :hat_color, 'Grey')
ask(gandalf, :call_method, :introduce)
ask(gandalf, :call_method, :drink_tea)
ask(gandalf, :set_field, :is_humble, true)
ask(gandalf, :call_method, :introduce)</code></pre></section>
<section class="bigcode"><pre><code class="ruby">def make_object(message_table)
return (lambda do |message, *message_args|
message_table[message].call(*message_args)
end)
end