-
Notifications
You must be signed in to change notification settings - Fork 2
/
cheatsheet.html.erb
381 lines (344 loc) · 12.2 KB
/
cheatsheet.html.erb
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<%
def c(code, linkys={})
code.gsub!(" "," ")
code.gsub!(/\b(def) /){"<span class='hl1'>#{$1}</span> "}
code.gsub!(/(\[bad.?\])/) { "<span class=bad>#{$1}</span>" }
code.gsub!(/(\[good.?\])/) { "<span class=good>#{$1}</span>" }
code.gsub!("\n","<br>")
linkys.each_pair do |text, link|
code.gsub!(text) { "<a href='#{link}'>" + text + "</a>" }
end
"<span class='cod'>" + code + "</span>"
end
def cmp(s)
" <span style='font-size:90%'><i>#{s}</i></span>"
end
%>
<head>
<style>
body { color: #292929;}
.cod { color: #116; }
.cod a { text-decoration: none }
.cod a:hover { text-decoration: underline }
td a { text-decoration: none;}
td a:hover { text-decoration: underline;}
.bad { font-size:80%; color: #522;}
.good { font-size:80%; color: #252;}
/*.hl1 { color: #248;}*/
.subheader { padding-top:0.5em; font-weight: bold; }
td { vertical-align: top; padding-left:4px; padding-right:2px;
border-width: 1px 0 0 0;
border-style: solid;
border-color: gray white gray white;}
ol { margin-top:0; margin-bottom:0;}
</style>
<title>cheat sheet for scala syntax</title>
<body>
<b>Cheat sheet for scala syntax</b>
<b><a href=http://anyall.org/scalacheat/>anyall.org/scalacheat</a></b>
by <a href=http://anyall.org/>brendan</a>
<br>Most should work in the interactive interpreter. Language coverage is still pretty incomplete.
<br>
Source on <a href=http://github.com/brendano/scalacheat>github</a>,
pull requests welcome!
<table style="margin-top:5px" cellspacing=0>
<!-- <tr>
<th>scala</th>
<th>explanation -->
<%############################################################%>
<tr><td class=subheader colspan=2>variables
<tr>
<td><%=c"var x = 5"%>
<td>variable
</tr>
<tr>
<td><%=c"val x = 5"%> <br><%=c"[bad!] x = 6"%>
<td>constant
</tr>
<tr>
<td><%=c"var x: Double = 5"%>
<td>explicit type
</tr>
<%############################################################%>
<tr><td class=subheader colspan=2>functions
<tr>
<td><%=c"[good] def f(x: Int) = { x*x }"%><br>
<%=c"[bad!] def f(x: Int) { x*x }"%>
<td>define function <br>hidden error: without = it's a <%=c"Unit"%>-returning procedure; causes havoc
<tr>
<td><%=c"[good] def f(x: Any) = println(x)"%><br>
<%=c"[bad!] def f(x) = println(x)"%>
<td>define function <br>syntax error: need types for every arg.
<tr>
<td><%=c"type R = Double"%> <td>type alias
<tr>
<td><%=c "def f(x: R)" %> <%=cmp "vs"%><br>
<%=c "def f(x: => R)" %>
<td>call-by-value <%=cmp "vs"%><br>call-by-name (<a href=http://scala.sygneca.com/faqs/language#what-s-the-difference-between-a-lazy-argument-a-no-arg-function-argument-and-a-lazy-value>lazy parameters</a>)
<tr>
<td><%=c "(x:R) => x*x" %>
<td>anonymous function
<tr>
<td><%=c"(1 to 5).map( _*2 )"%><br>
<%=c"(1 to 5).reduceLeft( _+_ )"%>
<td>anonymous function: underscore is positionally matched arg.
<tr>
<td><%=c"(1 to 5).map( x => x*x )"%>
<td>anonymous function: to use an arg twice, have to name it.
<tr>
<td><%=c"[good] (1 to 5).map(2*)"%><br>
<%=c"[bad!] (1 to 5).map(*2)"%>
<td>anonymous function: bound infix method. Use <%=c"2*_"%> for sanity's sake instead.
<tr>
<td><%=c"(1 to 5).map { val x=_*2; println(x); x }"%>
<td>anonymous function: block style returns last expression
<!-- examples: <a href=http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk/src/library/scala/concurrent/ops.scala>scala.concurrency.ops</a>, <a href=http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk/src/library/scala/util/control/Breaks.scala>scala.util.control.Breaks</a> -->
<tr>
<td><%=c"(1 to 5) filter {_%2 == 0} map {_*2}"%>
<td>anonymous functions: pipeline style. (or parens too)
<tr>
<td>
<%=c"def compose(g:R=>R, h:R=>R) = (x:R) => g(h(x))"%><br>
<%=c"val f = compose({_*2}, {_-1})"%>
<td>anonymous functions: to pass in multiple blocks, need outer parens
<tr>
<td><%=c"val zscore = (mean:R, sd:R) => (x:R) => (x-mean)/sd"%>
<td>currying, obvious syntax
<tr>
<td><%=c"def zscore(mean:R, sd:R) = (x:R) => (x-mean)/sd"%>
<td>currying, obvious syntax
<tr>
<td><%=c"def zscore(mean:R, sd:R)(x:R) = (x-mean)/sd"%>
<td>currying, sugar syntax. but then:
<tr>
<td><%=c"val normer = zscore(7, 0.4)_"%>
<td>need trailing underscore to get the partial, only for the sugar version.
<tr>
<td><%=c"def mapmake[T](g:T=>T)(seq: List[T]) = seq.map(g)"%>
<td>generic type
<tr>
<td><%=c"5.+(3); 5 + 3"%><br><%=c"(1 to 5) map (_*2)"%>
<td>infix sugar
<tr>
<td><%=c"def sum(args: Int*) = args.reduceLeft(_+_)"%>
<td>varargs
<tr>
<td>
<%=c"def foo(bar: Int = 42, baz: Int = 7) = { ... }"%><br>
<%=c"foo()"%><br>
<%=c"foo(baz = 13)"%>
<td>default args
<tr>
<td>
<%=c"def foo(bar: Int, baz: Int) = { ... }"%><br>
<%=c"foo(bar = 42, baz = 13)"%>
<td>named args
<%############################################################%>
<tr><td class=subheader colspan=2>packages
<tr>
<td><%=c"import scala.collection._"%>
<td>wildcard import
<tr>
<td>
<%=c"import scala.collection.Vector"%><br>
<%=c"import scala.collection.{Vector, Sequence}"%>
<td>selective import
<tr>
<td><%=c"import scala.collection.{Vector => Vec28}"%>
<td>renaming import
<tr>
<td><%=c"package pkg"%> <i>at start of file</i><br>
<%=c"package pkg { ... }"%>
<td>declare a package
<%############################################################%>
<tr><td class=subheader colspan=2>data structures
<tr>
<td><%=c"(1,2,3)"%>
<td>tuple literal (Tuple3)
<tr>
<td><%=c"var (x,y,z) = (1,2,3)"%>
<td>destructuring bind: tuple unpacking via pattern matching
<tr>
<td><%=c"[bad!] var x,y,z = (1,2,3)"%>
<td>hidden error: each assigned to the entire tuple
<tr>
<td><%=c"var xs = List(1,2,3)"%><br>
<td>list (immutable)
<tr><td><%=c"xs(2)"%> <td>paren indexing (<a href=http://www.slideshare.net/Odersky/fosdem-2009-1013261/27>slides</a>)
<tr>
<td><%=c"1 :: List(2,3)"%>
<td>cons
<tr>
<td><%=c"1 to 5"%> <%=cmp"same as"%> <%=c"1 until 6"%><br>
<%=c"1 to 10 by 2"%>
<td>range sugar
<tr><td><%=c"()"%> <%=cmp "(empty parens)"%> <td>sole member of the <%=c"Unit"%> type (like C/Java void)
<%############################################################%>
<tr><td class=subheader colspan=2>control constructs
<tr>
<td><%=c"if (check) happy else sad"%>
<td>conditional
</tr>
<tr>
<td><%=c"if (check) happy"%> <%=cmp "same as"%><br><%=c"if (check) happy else ()"%>
<td>conditional sugar
</tr>
<tr><td><%=c"var x = 0"%>
<tr><td><%=c"while (x < 5) { println(x); x += 1}"%> <td>while loop
<tr><td><%=c"do { println(x); x += 1} while (x < 5)"%> <td>do while loop
<tr><td><%=c(
"import scala.util.control.Breaks._
breakable {
for (x <- xs) {
if (Math.random < 0.1) break
}
}", "scala.util.control.Breaks._" => "http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk/src/library/scala/util/control/Breaks.scala")%> <td>break
(<a href="http://www.slideshare.net/Odersky/fosdem-2009-1013261/21">slides</a>)
<!-- TODO this example breaks on <%=c'continue'%> -->
<tr>
<td>
<%=c"val xs = List.range(1,11)"%><br><%=c"val ys = List.range(1,11)"%>
<tr><td>
<%=c"for (x <- xs if x%2 == 0) yield x*10"%> <%=cmp "same as"%><br>
<%=c"xs.filter(_%2 == 0).map(_*10)"%>
<td>for comprehension: filter/map
<tr>
<td>
<%=c"for ((x,y) <- xs zip ys) yield x*y"%> <%=cmp "same as"%><br>
<%=c"(xs zip ys) map { case (x,y) => x*y }"%>
<td>for comprehension: destructuring bind
<tr>
<td>
<%=c"for (x <- xs; y <- ys) yield x*y"%> <%=cmp "same as"%><br>
<%=c"xs flatMap {x => ys map {y => x*y}}"%>
<td>for comprehension: cross product
<tr>
<td>
<%=c(
%|for (x <- xs; y <- ys) {
println("%d/%d = %.1f".format(x,y, x*y))
}|, "format" => "http://java.sun.com/javase/6/docs/api/java/lang/String.html#format(java.lang.String,%20java.lang.Object...)")%>
<td>
for comprehension: imperative-ish<br>
<a href="http://java.sun.com/javase/6/docs/api/java/util/Formatter.html#syntax">sprintf-style</a>
<%############################################################%>
<tr><td class=subheader colspan=2>pattern matching
<tr>
<td>
<%=c"[good] (xs zip ys) map { case (x,y) => x*y }"%><br>
<%=c"[bad!] (xs zip ys) map( (x,y) => x*y )"%>
<td>
use <%=c"case"%> in function args for pattern matching
</tr>
<tr><td> <td>TODO <a href=http://www.codecommit.com/blog/scala/case-classes-are-cool>case classes</a>, match...
<%############################################################%>
<tr><td class=subheader colspan=2>object orientation
<tr>
<td><%=c"class C(x: R)"%> <%=cmp"same as"%><br><%=c"class C(private val x: R)"%><br><%=c"var c = new C(4)"%>
<td>constructor params - private
<tr>
<td><%=c %|\
class C(val x: R)
var c = new C(4)
c.x
| %>
<td>constructor params - public
<tr>
<td><%=c %|\
class C(var x: R) {
assert(x > 0, "positive please")
var y = x
val readonly = 5
private var secret = 1
def this = this(42)
}|%>
<td>
<br>
constructor is class body<br>
declare a public member<br>
declare a gettable but not settable member<br>
declare a private member
<br>
alternative constructor
<tr>
<td><%=c"new { ... }"%>
<td>anonymous class (TODO extension syntax)
<tr>
<td><%=c"abstract class D { ... }"%>
<td>define an abstract class. (non-createable)
<tr>
<td><%=c"class C extends D { ... }"%>
<td>define an inherited class.
<tr>
<td>
<%=c"class D(var x: R)"%><br>
<%=c"class C(x: R) extends D(x)"%>
<td>inheritance and constructor params. (wishlist: automatically pass-up params by default)
<tr>
<td><%=c"object O extends D { ... }"%>
<td>define a singleton. (module-like)
<tr>
<td><%=c"trait T { ... }"%><br>
<%=c"class C extends T { ... }"%><br>
<%=c"class C extends D with T { ... }"%>
<td>traits.<br>interfaces-with-implementation. no constructor params. <a href="http://www.scala-lang.org/node/117">mixin-able</a>.
<tr>
<td>
<%=c"trait T1; trait T2"%><br>
<%=c"class C extends T1 with T2"%><br>
<%=c"class C extends D with T1 with T2"%>
<td>multiple traits
<tr>
<td><%=c"class C extends D { override def f = ...}"%>
<td>must declare method overrides
<tr>
<td><%=c'new java.io.File("f")'%>
<td>create object
<tr>
<td><%=c"[bad!] new List[Int]"%><br><%=c"[good] List(1,2,3)"%>
<td>type error: abstract type<br>
instead, convention: callable factory shadowing the type
<!-- <tr>
<td><%=c"class C { class D }"%><br><%=c"C#D"%> <td>TODO doesn't work (should refer to inner class) -->
<tr>
<td>TODO
<td>self types
<tr>
<td><%=c"classOf[String]"%> <td>class literal
<tr>
<td><%=c"x.isInstanceOf[String]"%> <td>type check (runtime)
<tr>
<td><%=c"x.asInstanceOf[String]"%> <td>type cast (runtime)
<%############################################################%>
<tr><td class=subheader colspan=2>more
<tr>
<td>implicit
<td>TODO conversions
<tr>
<td>TODO
<td>try/catch
<tr>
<td><%=c"<s>hi {name}</s>".gsub('<','<').gsub('>','>')%> <td>xml literal with expression interpolation
<tr>
<td>TODO <%=c"Option"%>
<td>guarded type-safe nulls (like <a href="http://www.haskell.org/all_about_monads/html/maybemonad.html">Maybe</a> or
<a href="http://andand.rubyforge.org/">andand</a>)
<tr>
<td>TODO <td>unapply
<%############################################################%>
<tr>
<td colspan=2 class=subheader>advanced generics
<tr><td>TODO tricky esoteric stuff
</table>
<hr>
designed for scala 2.8 and 2.9.
portions adapted from A. Sundararajan's java vs. scala cheat sheet:
<a href=http://blogs.sun.com/sundararajan/entry/scala_for_java_programmers>(1)</a>
<a href=http://blogs.sun.com/sundararajan/entry/scala_for_java_programmers_part>(2)</a>
<br>
also check out: <a href=http://www.slideshare.net/Odersky/fosdem-2009-1013261>slides: fosdem-2009 (odersky)</a>, <a href=http://www.scala-lang.org/docu/files/ScalaByExample.pdf>book: scala by example (odersky)</a>
<br>
Copyright Brendan O'Connor, licensed <a href=http://creativecommons.org/licenses/by-sa/3.0/>CC BY-SA</a>.
<hr>
<div id="disqus_thread"></div><script type="text/javascript" src="http://disqus.com/forums/anyall/embed.js"></script><noscript><a href="http://anyall.disqus.com/?url=ref">View the discussion thread.</a></noscript><a href="http://disqus.com" class="dsq-brlink">blog comments powered by <span class="logo-disqus">Disqus</span></a>