File: Syntax-Shortcuts.schelp

package info (click to toggle)
supercollider 1%3A3.6.6~repack-2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 23,792 kB
  • ctags: 25,269
  • sloc: cpp: 177,129; lisp: 63,421; ansic: 11,297; python: 1,787; perl: 766; yacc: 311; sh: 286; lex: 181; ruby: 173; makefile: 168; xml: 13
file content (193 lines) | stat: -rw-r--r-- 6,007 bytes parent folder | download | duplicates (2)
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
title:: Syntax Shortcuts
summary:: syntactic sugar
categories:: Language
related:: Overviews/SymbolicNotations

section:: Introduction

This file shows a number of syntax equivalences in the compiler.

Because of the multiple syntax equivalences, some expressions can be written in many different ways. All of the following do the same thing and compile to the same code.
code::
// new argument syntax

(1..10).collect({|n| n.squared }); // receiver syntax
collect((1..10), {|n| n.squared }); // function call syntax
(1..10).collect {|n| n.squared }; // receiver syntax with trailing function arg
collect ((1..10)) {|n| n.squared }; // function call syntax with trailing function arg
(1..10) collect: {|n| n.squared }; // binary operator syntax


// old argument syntax

(1..10).collect({ arg n; n.squared }); // receiver syntax
collect((1..10), { arg n; n.squared }); // function call syntax
(1..10).collect { arg n; n.squared }; // receiver syntax with trailing function arg
collect ((1..10)) { arg n; n.squared }; // function call syntax with trailing function arg
(1..10) collect: { arg n; n.squared }; // binary operator syntax


// partial application syntax

(1..10).collect( _.squared ); // receiver syntax
collect((1..10), _.squared ); // function call syntax
(1..10) collect: _.squared ; // binary operator syntax
::

You could even start expanding out the equivalent of (1..10) which is really a shortcut for code:: series(1, nil, 10) ::. This could also be written code:: 1.series(nil,10) ::. This adds another 26 variations to the 13 variations above.

section:: Objects, functions, messages and arguments

subsection:: functional and receiver notation
table::
## instead of writing: || you can write:
## code:: f(x, y) :: || code:: x.f(y) ::
## code:: f(g(x)) :: || code:: x.g.f ::
::

subsection:: defining instance variable accessor methods
table::
## instead of writing: || you can write:
## code::
Thing { var x;
    x { ^x }
    x_ { arg z; x = z; }
}
:: || code:: Thing { var <>x; } ::
::

subsection:: calling an instance variable setter method
table::
## instead of writing: || you can write:
## code:: p.x_(y) :: || code:: p.x = y :: or code:: x(p) = y ::
::

subsection:: use a selector as binary operator
table::
## instead of writing: || you can write:
## code:: min(x, y) :: || code:: x min: y ::
::

subsection:: instantiate object
table::
## instead of writing: || you can write:
## code:: Point.new(3, 4); :: || code:: Point(3, 4) ::
::

subsection:: moving blocks out of argument lists
table::
## instead of writing: || you can write:
## code:: if (x<3, {\abc}, {\def}); :: || code:: if (x<3) {\abc} {\def} ::
## code:: z.do({|x| x.play }); :: || code:: z.do {|x| x.play }; ::
## code:: while({ a < b },{ a = a * 2 }); :: || code:: while { a < b } { a = a * 2 }; ::
::

subsection:: shorter argument lists
table::
## instead of writing: || you can write:
## code:: { arg x; x < 2 } :: || code:: {|x| x < 2 } ::
## code:: { arg x = 123; x < 2 } :: || code:: {|x = 123| x < 2 } ::
## code:: { arg x = 10.rand; x < 2 } :: || code:: {|x = (10.rand)| x < 2 } :: or code:: {|x(10.rand)| x < 2 } ::
::
note::
When using the new code::||:: syntax, the default value needs to be enclosed in parenthesis if it's not a literal.
::

subsection:: calling the 'value' method
table::
## instead of writing: || you can write:
## code:: f.value(x) :: || code:: f.(x) ::
::

subsection:: calling performList
table::
## instead of writing: || you can write:
## code:: object.performList(\method, a, b, array) :: || code:: object.method(a, b, *array) ::
::

subsection:: partial application
table::
## instead of writing: || you can write:
## code:: {|x| object.msg(a, x, b) } :: || code:: object.msg(a, _, b) ::
## code:: {|x,y| object.msg(a, x, y) } :: || code:: object.msg(a, _, _) ::
## code:: {|x| a + x } :: || code:: a + _ ::
## code:: {|x| [a, b, x] } :: || code:: [a, b, _] ::
## code:: {|x| (a: x) } :: || code:: (a: _) ::
::


section:: Collections

subsection:: create a collection
table::
## instead of writing: || you can write:
## code:: Set.new.add(3).add(4).add(5); :: || code:: Set[3, 4, 5] ::
## code:: Array[3, 4, 5]; :: || code:: [3, 4, 5] ::
::

subsection:: indexing elements
table::
## instead of writing: || you can write:
## code:: z.at(i) :: || code:: z[i] ::
## code:: z.put(i, y); :: || code:: z[i] = y; ::
::

subsection:: creating Events
table::
## instead of writing: || you can write:
## code:: Event[\a -> 1, \b -> 2] :: || code:: (a: 1, b: 2) ::
::

subsection:: creating Arrays with key-value pairs
table::
## instead of writing: || you can write:
## code:: [\a, 1, \b, 2] :: || code:: [a: 1, b: 2] ::
::

subsection:: creating arithmetic series
table::
## instead of writing: || you can write:
## code:: Array.series(16,1,1) :: or code:: series(1,nil,16) :: || code:: (1..16) ::
## code:: Array.series(6,1,2) :: or code:: series(1,3,11) :: || code:: (1,3..11) ::
::
There is also the similar syntax for creating an iterating link::Classes/Routine:: :
table::
## instead of writing: || you can write:
## code:: seriesIter(1,3,11) :: || code:: (:1,3..11) ::
::

subsection:: accessing subranges of Arrays
table::
## instead of writing: || you can write:
## code:: a.copyRange(4,8) :: || code:: a[4..8] ::
## code:: a.copyToEnd(4) :: || code:: a[4..] ::
## code:: a.copyFromStart(4) :: || code:: a[..4] ::
::

section:: Other shortcuts

subsection:: multiple assignment
table::
## instead of writing: || you can write:
## code:: x = z.at(0); y = z.at(1); :: || code:: # x, y = z; ::
::

subsection:: accessing environment variables
table::
## instead of writing: || you can write:
## code:: 'myName'.envirGet :: || code:: ~myName ::
## code:: 'myName'.envirSet(9); :: || code:: ~myName = 9; ::
::

subsection:: shorthand for Symbols
table::
## instead of writing: || you can write:
## code:: 'mySymbol' :: || code:: \mySymbol ::
::

subsection:: creating a Ref
table::
## instead of writing: || you can write:
## code:: Ref.new(thing) :: || code:: `thing ::
::