1 title:: Syntax Shortcuts
2 summary:: syntactic sugar
4 related:: Overviews/SymbolicNotations
8 This file shows a number of syntax equivalences in the compiler.
10 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.
12 // new argument syntax
14 (1..10).collect({|n| n.squared }); // receiver syntax
15 collect((1..10), {|n| n.squared }); // function call syntax
16 (1..10).collect {|n| n.squared }; // receiver syntax with trailing function arg
17 collect ((1..10)) {|n| n.squared }; // function call syntax with trailing function arg
18 (1..10) collect: {|n| n.squared }; // binary operator syntax
21 // old argument syntax
23 (1..10).collect({ arg n; n.squared }); // receiver syntax
24 collect((1..10), { arg n; n.squared }); // function call syntax
25 (1..10).collect { arg n; n.squared }; // receiver syntax with trailing function arg
26 collect ((1..10)) { arg n; n.squared }; // function call syntax with trailing function arg
27 (1..10) collect: { arg n; n.squared }; // binary operator syntax
30 // partial application syntax
32 (1..10).collect( _.squared ); // receiver syntax
33 collect((1..10), _.squared ); // function call syntax
34 (1..10) collect: _.squared ; // binary operator syntax
37 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.
39 section:: Objects, functions, messages and arguments
41 subsection:: functional and receiver notation
43 ## instead of writing: || you can write:
44 ## code:: f(x, y) :: || code:: x.f(y) ::
45 ## code:: f(g(x)) :: || code:: x.g.f ::
48 subsection:: defining instance variable accessor methods
50 ## instead of writing: || you can write:
56 :: || code:: Thing { var <>x; } ::
59 subsection:: calling an instance variable setter method
61 ## instead of writing: || you can write:
62 ## code:: p.x_(y) :: || code:: p.x = y :: or code:: x(p) = y ::
65 subsection:: use a selector as binary operator
67 ## instead of writing: || you can write:
68 ## code:: min(x, y) :: || code:: x min: y ::
71 subsection:: instantiate object
73 ## instead of writing: || you can write:
74 ## code:: Point.new(3, 4); :: || code:: Point(3, 4) ::
77 subsection:: moving blocks out of argument lists
79 ## instead of writing: || you can write:
80 ## code:: if (x<3, {\abc}, {\def}); :: || code:: if (x<3) {\abc} {\def} ::
81 ## code:: z.do({|x| x.play }); :: || code:: z.do {|x| x.play }; ::
82 ## code:: while({ a < b },{ a = a * 2 }); :: || code:: while { a < b } { a = a * 2 }; ::
85 subsection:: shorter argument lists
87 ## instead of writing: || you can write:
88 ## code:: { arg x; x < 2 } :: || code:: {|x| x < 2 } ::
89 ## code:: { arg x = 123; x < 2 } :: || code:: {|x = 123| x < 2 } ::
90 ## code:: { arg x = 10.rand; x < 2 } :: || code:: {|x = (10.rand)| x < 2 } :: or code:: {|x(10.rand)| x < 2 } ::
93 When using the new code::||:: syntax, the default value needs to be enclosed in parenthesis if it's not a literal.
96 subsection:: calling the 'value' method
98 ## instead of writing: || you can write:
99 ## code:: f.value(x) :: || code:: f.(x) ::
102 subsection:: calling performList
104 ## instead of writing: || you can write:
105 ## code:: object.performList(\method, a, b, array) :: || code:: object.method(a, b, *array) ::
108 subsection:: partial application
110 ## instead of writing: || you can write:
111 ## code:: {|x| object.msg(a, x, b) } :: || code:: object.msg(a, _, b) ::
112 ## code:: {|x,y| object.msg(a, x, y) } :: || code:: object.msg(a, _, _) ::
113 ## code:: {|x| a + x } :: || code:: a + _ ::
114 ## code:: {|x| [a, b, x] } :: || code:: [a, b, _] ::
115 ## code:: {|x| (a: x) } :: || code:: (a: _) ::
119 section:: Collections
121 subsection:: create a collection
123 ## instead of writing: || you can write:
124 ## code:: Set.new.add(3).add(4).add(5); :: || code:: Set[3, 4, 5] ::
125 ## code:: Array[3, 4, 5]; :: || code:: [3, 4, 5] ::
128 subsection:: indexing elements
130 ## instead of writing: || you can write:
131 ## code:: z.at(i) :: || code:: z[i] ::
132 ## code:: z.put(i, y); :: || code:: z[i] = y; ::
135 subsection:: creating Events
137 ## instead of writing: || you can write:
138 ## code:: Event[\a -> 1, \b -> 2] :: || code:: (a: 1, b: 2) ::
141 subsection:: creating Arrays with key-value pairs
143 ## instead of writing: || you can write:
144 ## code:: [\a, 1, \b, 2] :: || code:: [a: 1, b: 2] ::
147 subsection:: creating arithmetic series
149 ## instead of writing: || you can write:
150 ## code:: Array.series(16,1,1) :: or code:: series(1,nil,16) :: || code:: (1..16) ::
151 ## code:: Array.series(6,1,2) :: or code:: series(1,3,11) :: || code:: (1,3..11) ::
153 There is also the similar syntax for creating an iterating link::Classes/Routine:: :
155 ## instead of writing: || you can write:
156 ## code:: seriesIter(1,3,11) :: || code:: (:1,3..11) ::
159 subsection:: accessing subranges of Arrays
161 ## instead of writing: || you can write:
162 ## code:: a.copyRange(4,8) :: || code:: a[4..8] ::
163 ## code:: a.copyToEnd(4) :: || code:: a[4..] ::
164 ## code:: a.copyFromStart(4) :: || code:: a[..4] ::
167 section:: Other shortcuts
169 subsection:: multiple assignment
171 ## instead of writing: || you can write:
172 ## code:: x = z.at(0); y = z.at(1); :: || code:: # x, y = z; ::
175 subsection:: accessing environment variables
177 ## instead of writing: || you can write:
178 ## code:: 'myName'.envirGet :: || code:: ~myName ::
179 ## code:: 'myName'.envirSet(9); :: || code:: ~myName = 9; ::
182 subsection:: shorthand for Symbols
184 ## instead of writing: || you can write:
185 ## code:: 'mySymbol' :: || code:: \mySymbol ::
188 subsection:: creating a Ref
190 ## instead of writing: || you can write:
191 ## code:: Ref.new(thing) :: || code:: `thing ::