1 <!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
4 <meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8">
5 <meta http-equiv=
"Content-Style-Type" content=
"text/css">
7 <meta name=
"Generator" content=
"Cocoa HTML Writer">
8 <meta name=
"CocoaVersion" content=
"824.41">
9 <style type=
"text/css">
10 p
.p1
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica
}
11 p
.p2
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica
; min-height: 14.0px}
12 p
.p3
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; min-height: 12.0px}
13 p
.p4
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #bf0000}
14 p
.p5
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #000000}
15 p
.p6
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #000000; min-height: 12.0px}
16 span
.s1
{font: 18.0px Helvetica
}
17 span
.s2
{color: #0000bf}
18 span
.s3
{color: #007300}
19 span
.s4
{color: #000000}
20 span
.s5
{color: #bf0000}
21 span
.Apple-tab-span
{white-space:pre
}
25 <p class=
"p1"><span class=
"s1"><b>Thunk
<span class=
"Apple-tab-span"> </span></b></span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><b> unevaluated value
</b></p>
26 <p class=
"p2"><br></p>
27 <p class=
"p1">superclass: AbstractFunction
</p>
28 <p class=
"p2"><br></p>
29 <p class=
"p2"><br></p>
30 <p class=
"p1"><b>Thunk(function)
</b></p>
31 <p class=
"p2"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></p>
32 <p class=
"p1"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><b>function
<span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></b>some function that returns the desired value
</p>
33 <p class=
"p2"><b><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></b></p>
34 <p class=
"p1"><b>value
<span class=
"Apple-tab-span"> </span></b>return the value. If calculation is done, use previous value, otherwise do calculation.
</p>
35 <p class=
"p2"><b></b><br></p>
36 <p class=
"p2"><b><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></b></p>
37 <p class=
"p1">Thunk,
<i>"past tense of think"</i>, can be used
<span class=
"Apple-tab-span"> </span>when a calculation may, or may not have to be performed at a later point in time, and its value is needed several times. This is an example of lazy evaluation, and can be used to avoid unnecessary calculations and to make state immutable.
</p>
38 <p class=
"p2"><span class=
"Apple-tab-span"> </span></p>
39 <p class=
"p3"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></p>
40 <p class=
"p4">// so for example, random values will result in a single instance:
</p>
41 <p class=
"p5">a =
<span class=
"s2">Thunk
</span>({
<span class=
"s3">\done
</span>.postln; rrand(
2.0,
8.0) });
</p>
42 <p class=
"p4"><span class=
"s4">a.value;
</span>// posts
"done"</p>
43 <p class=
"p5">a.value;
</p>
44 <p class=
"p6"><br></p>
45 <p class=
"p4">// it is an AbstractFunction, so one can use it for math operations:
</p>
46 <p class=
"p6"><br></p>
47 <p class=
"p5">a =
<span class=
"s2">Thunk
</span>({ rrand(
2.0,
8.0) });
</p>
48 <p class=
"p5">b = a *
5 / (a -
1);
</p>
49 <p class=
"p5">b.value;
</p>
50 <p class=
"p6"><br></p>
51 <p class=
"p4">// lazy evaluation
</p>
52 <p class=
"p6"><br></p>
53 <p class=
"p5">a =
<span class=
"s2">Thunk
</span>({
<span class=
"s3">\done1
</span>.postln;
<span class=
"s2">Array
</span>.fill(
10000, {
<span class=
"s2">|i|
</span> i +
6 %
5 * i /
2 }) });
<span class=
"s5">// some calculation.
</span></p>
54 <p class=
"p5">b =
<span class=
"s2">Thunk
</span>({
<span class=
"s3">\done2
</span>.postln;
<span class=
"s2">Array
</span>.fill(
10000, {
<span class=
"s2">|i|
</span> i +
5 %
6 * i /
3 }) });
<span class=
"s5">// some other calculation.
</span></p>
55 <p class=
"p5">c = [a, b].choose +
700;
</p>
56 <p class=
"p4"><span class=
"s4">(c * c * c).value;
</span>// caclulation happens here, and only once.
</p>
57 <p class=
"p6"><br></p>
58 <p class=
"p4">// compare to a function:
</p>
59 <p class=
"p6"><br></p>
60 <p class=
"p5">a = {
<span class=
"s3">\done1
</span>.postln;
<span class=
"s2">Array
</span>.fill(
10000, {
<span class=
"s2">|i|
</span> i +
6 %
5 * i /
2 }) };
<span class=
"s5">// some calculation.
</span></p>
61 <p class=
"p5">b = {
<span class=
"s3">\done2
</span>.postln;
<span class=
"s2">Array
</span>.fill(
10000, {
<span class=
"s2">|i|
</span> i +
5 %
6 * i /
3 }) };
<span class=
"s5">// some other calculation.
</span></p>
62 <p class=
"p5">c = [a, b].choose +
700;
</p>
63 <p class=
"p4"><span class=
"s4">(c * c * c).value;
</span>// calculation happens here, but
3 times (for each c)
</p>
64 <p class=
"p6"><br></p>