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; ::
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 } ::
91 subsection:: calling the 'value' method
93 ## instead of writing: || you can write:
94 ## code:: f.value(x) :: || code:: f.(x) ::
97 subsection:: calling performList
99 ## instead of writing: || you can write:
100 ## code:: object.performList(\method, a, b, array) :: || code:: object.method(a, b, *array) ::
103 subsection:: partial application
105 ## instead of writing: || you can write:
106 ## code:: {|x| object.msg(a, x, b) } :: || code:: object.msg(a, _, b) ::
107 ## code:: {|x,y| object.msg(a, x, y) } :: || code:: object.msg(a, _, _) ::
108 ## code:: {|x| a + x } :: || code:: a + _ ::
109 ## code:: {|x| [a, b, x] } :: || code:: [a, b, _] ::
110 ## code:: {|x| (a: x) } :: || code:: (a: _) ::
114 section:: Collections
116 subsection:: create a collection
118 ## instead of writing: || you can write:
119 ## code:: Set.new.add(3).add(4).add(5); :: || code:: Set[3, 4, 5] ::
120 ## code:: Array[3, 4, 5]; :: || code:: [3, 4, 5] ::
123 subsection:: indexing elements
125 ## instead of writing: || you can write:
126 ## code:: z.at(i) :: || code:: z[i] ::
127 ## code:: z.put(i, y); :: || code:: z[i] = y; ::
130 subsection:: creating Events
132 ## instead of writing: || you can write:
133 ## code:: Event[\a -> 1, \b -> 2] :: || code:: (a: 1, b: 2) ::
136 subsection:: creating Arrays with key-value pairs
138 ## instead of writing: || you can write:
139 ## code:: [\a, 1, \b, 2] :: || code:: [a: 1, b: 2] ::
142 subsection:: creating arithmetic series
144 ## instead of writing: || you can write:
145 ## code:: Array.series(16,1,1) :: or code:: series(1,nil,16) :: || code:: (1..16) ::
146 ## code:: Array.series(6,1,2) :: or code:: series(1,3,11) :: || code:: (1,3..11) ::
149 subsection:: accessing subranges of Arrays
151 ## instead of writing: || you can write:
152 ## code:: a.copyRange(4,8) :: || code:: a[4..8] ::
153 ## code:: a.copyToEnd(4) :: || code:: a[4..] ::
154 ## code:: a.copyFromStart(4) :: || code:: a[..4] ::
157 section:: Other shortcuts
159 subsection:: multiple assignment
161 ## instead of writing: || you can write:
162 ## code:: x = z.at(0); y = z.at(1); :: || code:: # x, y = z; ::
165 subsection:: accessing environment variables
167 ## instead of writing: || you can write:
168 ## code:: 'myName'.envirGet :: || code:: ~myName ::
169 ## code:: 'myName'.envirSet(9); :: || code:: ~myName = 9; ::
172 subsection:: shorthand for Symbols
174 ## instead of writing: || you can write:
175 ## code:: 'mySymbol' :: || code:: \mySymbol ::
178 subsection:: creating a Ref
180 ## instead of writing: || you can write:
181 ## code:: Ref.new(thing) :: || code:: `thing ::