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=
"949.43">
9 <style type=
"text/css">
10 p
.p1
{margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica
}
11 p
.p2
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; min-height: 12.0px}
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: 12.0px Helvetica
; min-height: 14.0px}
14 p
.p5
{margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica
}
15 p
.p6
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
}
16 p
.p7
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #000000}
17 span
.s1
{color: #0000bf}
18 span
.s2
{color: #0000bf}
19 span
.s3
{color: #007300}
23 <p class=
"p1"><b>Adverbs for Binary Operators
</b></p>
24 <p class=
"p2"><br></p>
25 <p class=
"p3">Adverbs are a third argument passed to binary operators that modifies how they iterate over SequenceableCollections or Streams. The idea for adverbs is taken from the J programming language which is a successor of APL.
</p>
26 <p class=
"p4"><br></p>
27 <p class=
"p5"><b>Adverbs and SequenceableCollections
</b></p>
28 <p class=
"p4"><br></p>
29 <p class=
"p3">Normally when you add two arrays like this:
</p>
30 <p class=
"p2"><br></p>
31 <p class=
"p6">[
10,
20,
30,
40,
50] + [
1,
2,
3]
</p>
32 <p class=
"p2"><br></p>
33 <p class=
"p3">You get this result:
</p>
34 <p class=
"p2"><br></p>
35 <p class=
"p6">[
11,
22,
33,
41,
52 ]
</p>
36 <p class=
"p2"><br></p>
37 <p class=
"p3">A new array is created which is the length of the longer array and items from each array are added together by wrapped indexing.
</p>
38 <p class=
"p4"><br></p>
39 <p class=
"p4"><br></p>
40 <p class=
"p3">Using adverbs can change this behavior. Adverbs are symbols and they follow a '.' after the binary operator. Adverbs can be applied to all binary operators.
</p>
41 <p class=
"p4"><br></p>
42 <p class=
"p3"><b>adverb 's'
</b></p>
43 <p class=
"p4"><br></p>
44 <p class=
"p3">The first adverb is 's' which means 'short'. The add operation now returns the shorter of the two arrays.
</p>
45 <p class=
"p2"><br></p>
46 <p class=
"p6">[
10,
20,
30,
40,
50] +.s [
1,
2,
3]
</p>
47 <p class=
"p2"><br></p>
48 <p class=
"p3">returns:
</p>
49 <p class=
"p2"><br></p>
50 <p class=
"p6">[
11,
22,
33 ]
</p>
51 <p class=
"p2"><br></p>
52 <p class=
"p3"><b>adverb 'f'
</b></p>
53 <p class=
"p2"><br></p>
54 <p class=
"p3">Another adverb is 'f' which uses folded indexing instead of wrapped:
</p>
55 <p class=
"p2"><br></p>
56 <p class=
"p6">[
10,
20,
30,
40,
50] +.f [
1,
2,
3]
</p>
57 <p class=
"p2"><br></p>
58 <p class=
"p3">returns:
</p>
59 <p class=
"p2"><br></p>
60 <p class=
"p6">[
11,
22,
33,
42,
51 ]
</p>
61 <p class=
"p2"><br></p>
62 <p class=
"p3"><b>adverb 't'
</b></p>
63 <p class=
"p2"><br></p>
64 <p class=
"p3">The table adverb 't' makes an array of arrays where each item in the first array is added to the whole second array and the resulting arrays are collected.
</p>
65 <p class=
"p2"><br></p>
66 <p class=
"p6">[
10,
20,
30,
40,
50] +.t [
1,
2,
3]
</p>
67 <p class=
"p2"><br></p>
68 <p class=
"p3">returns:
</p>
69 <p class=
"p2"><br></p>
70 <p class=
"p6">[ [
11,
12,
13 ], [
21,
22,
23 ], [
31,
32,
33 ], [
41,
42,
43 ], [
51,
52,
53 ] ]
</p>
71 <p class=
"p2"><br></p>
72 <p class=
"p3"><b>adverb 'x'
</b></p>
73 <p class=
"p2"><br></p>
74 <p class=
"p3">The cross adverb 'x' is like table, except that the result is a flat array:
</p>
75 <p class=
"p2"><br></p>
76 <p class=
"p6">[
10,
20,
30,
40,
50] +.x [
1,
2,
3]
</p>
77 <p class=
"p2"><br></p>
78 <p class=
"p6">[
11,
12,
13,
21,
22,
23,
31,
32,
33,
41,
42,
43,
51,
52,
53 ]
</p>
79 <p class=
"p2"><br></p>
80 <p class=
"p5"><b>Adverbs and Streams
</b></p>
81 <p class=
"p2"><br></p>
82 <p class=
"p3">There is currently one adverb defined for streams. This is the cross adverb, 'x'.
</p>
83 <p class=
"p4"><br></p>
84 <p class=
"p3">Normally when you add two streams like this:
</p>
85 <p class=
"p2"><br></p>
86 <p class=
"p2"><br></p>
87 <p class=
"p6">p = (
<span class=
"s1">Pseq
</span>([
10,
20]) +
<span class=
"s1">Pseq
</span>([
1,
2,
3])).asStream;
</p>
88 <p class=
"p6">Array.fill(
3, { p.next });
</p>
89 <p class=
"p2"><br></p>
90 <p class=
"p3">you get this:
</p>
91 <p class=
"p2"><br></p>
92 <p class=
"p6">[
11,
22, nil ]
</p>
93 <p class=
"p2"><br></p>
94 <p class=
"p3">The items are paired sequentially and the stream ends when the earliest stream ends.
</p>
95 <p class=
"p2"><br></p>
96 <p class=
"p2"><br></p>
97 <p class=
"p3">The cross adverb allows you to pair each item in the first stream with every item in the second stream.
</p>
98 <p class=
"p2"><br></p>
99 <p class=
"p6">p = (
<span class=
"s1">Pseq
</span>([
10,
20]) +.x
<span class=
"s1">Pseq
</span>([
1,
2,
3])).asStream;
</p>
100 <p class=
"p6">Array.fill(
7, { p.next });
</p>
101 <p class=
"p2"><br></p>
102 <p class=
"p3">the result is:
</p>
103 <p class=
"p2"><br></p>
104 <p class=
"p6">[
11,
12,
13,
21,
22,
23, nil ]
</p>
105 <p class=
"p2"><br></p>
106 <p class=
"p2"><br></p>
107 <p class=
"p3">You can string these together. Every item in the left stream operand is
"ornamented" by the right stream operand.
</p>
108 <p class=
"p2"><br></p>
109 <p class=
"p6">p = (
<span class=
"s1">Pseq
</span>([
100,
200]) +.x
<span class=
"s1">Pseq
</span>([
10,
20,
30]) +.x
<span class=
"s1">Pseq
</span>([
1,
2,
3,
4])).asStream;
</p>
110 <p class=
"p6">Array.fill(
25, { p.next });
</p>
111 <p class=
"p2"><br></p>
112 <p class=
"p6">[
111,
112,
113,
114,
121,
122,
123,
124,
131,
132,
133,
134,
<span class=
"Apple-converted-space"> </span></p>
113 <p class=
"p6"><span class=
"Apple-converted-space"> </span>211,
212,
213,
214,
221,
222,
223,
224,
231,
232,
233,
234, nil ]
</p>
114 <p class=
"p2"><br></p>
115 <p class=
"p2"><br></p>
116 <p class=
"p3">Sound example:
</p>
117 <p class=
"p2"><br></p>
118 <p class=
"p2"><br></p>
119 <p class=
"p7"><span class=
"s2">Pbind
</span>(
<span class=
"s3">\dur
</span>,
0.17,
<span class=
"s3">\degree
</span>,
<span class=
"s2">Pwhite
</span>(
0,
6) +.x
<span class=
"s2">Pseq
</span>([[
0,
2,
4],
1, [
0,
2],
3])).trace.play
</p>
120 <p class=
"p2"><br></p>