Include a header file required for build on mac 10.4
[supercollider.git] / Help / Language / Partial-Application.html
blob401b93642020bc64068244bc918f3975e734890b
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta http-equiv="Content-Style-Type" content="text/css">
6 <title></title>
7 <meta name="Generator" content="Cocoa HTML Writer">
8 <meta name="CocoaVersion" content="824.42">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #a71e12}
16 span.s1 {color: #0019b7}
17 span.s2 {color: #326f17}
18 span.s3 {color: #000000}
19 span.s4 {color: #a71e12}
20 </style>
21 </head>
22 <body>
23 <p class="p1"><br></p>
24 <p class="p2"><b>Function Creation via Partial Application</b></p>
25 <p class="p1"><br></p>
26 <p class="p3">Partial application is a way to create a function by passing only some arguments to a method. The _ character stands in for missing arguments and becomes an argument to the created function. It only applies to a single method, list, or dictionary call, not to a more complex nested expression.</p>
27 <p class="p4"><br></p>
28 <p class="p3">for example:</p>
29 <p class="p4"><br></p>
30 <p class="p5">f = _ + 2;</p>
31 <p class="p4"><br></p>
32 <p class="p3">f is now a function of one argument.</p>
33 <p class="p4"><br></p>
34 <p class="p5">f.value(7);</p>
35 <p class="p4"><br></p>
36 <p class="p3">it is equivalent to having written:</p>
37 <p class="p4"><br></p>
38 <p class="p5">f = {<span class="s1">|x|</span> x + 2 };</p>
39 <p class="p4"><br></p>
40 <p class="p3">(except that there is no name 'x' declared)</p>
41 <p class="p4"><br></p>
42 <p class="p4"><br></p>
43 <p class="p5">g = <span class="s1">Point</span>(<span class="s1">_</span>, <span class="s1">_</span>);</p>
44 <p class="p4"><br></p>
45 <p class="p3">g is a function of two arguments.</p>
46 <p class="p4"><br></p>
47 <p class="p5">g.value(3, 4);</p>
48 <p class="p4"><br></p>
49 <p class="p4"><br></p>
50 <p class="p3">Here are some example usages of this in a collect message. Below each is written the equivalent function.</p>
51 <p class="p5">(1..8).collect(<span class="s1">_</span>.isPrime);</p>
52 <p class="p5">(1..8).collect {<span class="s1">|x|</span> x.isPrime };</p>
53 <p class="p4"><br></p>
54 <p class="p5">(1..8).collect(<span class="s1">_</span>.hash);</p>
55 <p class="p5">(1..8).collect {<span class="s1">|x|</span> x.hash };</p>
56 <p class="p4"><br></p>
57 <p class="p5">(1..8).collect([<span class="s2">\a</span>, <span class="s2">\b</span>, <span class="s1">_</span>]);</p>
58 <p class="p5">(1..8).collect {<span class="s1">|x|</span> [<span class="s2">\a</span>, <span class="s2">\b</span>, x] };</p>
59 <p class="p4"><br></p>
60 <p class="p5">(1..8).collect((a:<span class="s1">_</span>));</p>
61 <p class="p5">(1..8).collect {<span class="s1">|x|</span> (a:x) };</p>
62 <p class="p4"><br></p>
63 <p class="p5">(1..8).collect(<span class="s1">Polar</span>(<span class="s1">_</span>, pi));</p>
64 <p class="p5">(1..8).collect {<span class="s1">|x|</span> <span class="s1">Polar</span>(x, pi) };</p>
65 <p class="p4"><br></p>
66 <p class="p5">(1..8).collect((1..<span class="s1">_</span>));</p>
67 <p class="p5">(1..8).collect {<span class="s1">|x|</span> (1..x) };</p>
68 <p class="p4"><br></p>
69 <p class="p6"><span class="s3">f = (a:</span><span class="s1">_</span><span class="s3">, b:</span><span class="s1">_</span><span class="s3">); </span>// f is a two argument function</p>
70 <p class="p6"><span class="s3">g = f.(</span><span class="s1">_</span><span class="s3">, 5); <span class="Apple-converted-space">  </span></span>// g is a partial application of f</p>
71 <p class="p6"><span class="s3">g.(7);<span class="Apple-converted-space">          </span></span>// get the answer</p>
72 <p class="p4"><br></p>
73 <p class="p6">// equivalent to this:</p>
74 <p class="p5">f = {<span class="s1">|x, y|</span> (a:x, b:y) }</p>
75 <p class="p5">g = {<span class="s1">|z|</span> f.(z, 5) };</p>
76 <p class="p5">g.value(7);</p>
77 <p class="p4"><br></p>
78 <p class="p4"><br></p>
79 <p class="p3">An example of what you can't do:</p>
80 <p class="p4"><br></p>
81 <p class="p5">(1..8).collect( <span class="s1">Point</span>(100 * <span class="s1">_</span>, 50) ); <span class="s4">// nested expression won't work.</span></p>
82 <p class="p6">// only the * gets partially applied, not the surrounding expression.</p>
83 <p class="p4"><br></p>
84 <p class="p6"><span class="s3">(1..8).collect {</span><span class="s1">|x|</span><span class="s3"> </span><span class="s1">Point</span><span class="s3">(100 * x, 50) }; </span>// need to use a function for this.</p>
85 <p class="p4"><br></p>
86 </body>
87 </html>