8000 Update cheatsheets/index.md · wuhaixing/scala.github.com@fa3249f · GitHub
[go: up one dir, main page]

Skip to content

Commit fa3249f

Browse files
Update cheatsheets/index.md
1 parent 04e2179 commit fa3249f

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

cheatsheets/index.md

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
---
22
layout: cheatsheet
33
title: Scalacheat
4-
by: Brendan O'Connor
4+
by: (initial version: Brendan O'Connor) HamsterofDea
55
about: Thanks to <a href="http://brenocon.com/">Brendan O'Connor</a>, this cheatsheet aims to be a quick reference of Scala syntactic constructions. Licensed by Brendan O'Connor under a CC-BY-SA 3.0 license.
66
---
77

88
###### Contributed by {{ page.by }}
99

10-
| | |
10+
|note: "this is a lie" means i gave a simplified explanation and left out details which will be covered later | |
1111
| ------ | ------ |
12-
| <h2 id="declarations">declarations</h2> | |
12+
| <h2 id="declarations">Basic declarations</h2> | |
1313
| `var x = 5`<br>`x = 6` | mutable variable, value can be changed later |
1414
| <span class="label success">Good</span> `val x = 5`<br> <span class="label important">Bad</span> `x=6` | immutable, cannot be changed |
1515
| `var x: Double = 5` | explicit type. if not provided, compiler will pick one (more later) |
@@ -19,13 +19,53 @@ about: Thanks to <a href="http://brenocon.com/">Brendan O'Connor</a>, this cheat
1919
| `def x(foo:String):String = return foo+"hello world"` | method declaration with simple parameter |
2020
| `def x(foo:String, bar:String):String = return foo+"hello world"+bar` | method declaration with two parameters |
2121
| `def x:String = {val x = 1;return "hello world"+1}` | multiple statements need {} around the code |
22-
| `def x = "hello world"`|return keyword and return type declaration are optional. default return value = last value in code block
23-
| `def x {print("hello world")}` | method without "=" means the method has no return type/return type is void (this is a lie to keep things simple, more later) |
22+
| `def x = "hello world"`|return keyword and return type declaration are optional, but if a method contains return, the return type *must* be specified explicitly. default return value = last value in code block.
2423
| `def x = {def y = 7;y}` | nested declarations are possible|
2524
| `class Foo`| class declaration - nested declaration also possible|
2625
| `class Foo(var x:String, val y:Int)`| class declaration with 2 public fields, one mutable, one immutable. constructor is automatically generated. only new Foo("1",2) is possible|
2726
| `class Foo {var x = 5;val y = 6}`|class like above, but with default constructor, only new Foo() is possible|
2827
| `class Foo {def x = 5}`|class with default constructor and one method|
28+
| `val x = {class y(val z: String); new y("hello").z}`<br><span class="label important">Bad</span>`val foo = new y("does not work outside the block above")`| everything can be nested in anything, but everything can only be accessed in its scope|
29+
| <h2 id="typeinference">Declaring functions</h2> | |
30+
| `(i:Int) => i+1`|creates a function.|
31+
| `var func = (i:Int) => i+1`|creates a function and stores it in a variable|
32+
| `func(5)`|executing the function above|
33+
| `def func = (i:Int) => i+1`|creates a function each time the method is called and returns that function, not i+1|
34+
| `lazy val x = expensiveOperation()`|the expensive operation is executed once as soon as the value of x is needed, not before|
35+
| <h2 id="typeinference">Return types and type inference</h2> | |
36+
| `val x = "hello"`|the compiler always picks the most specific type possible, in this case java.lang.String|
37+
| `val x:Serializable = "hello"`|you can always specify a more general one|
38+
| `def x {print("hello world")}` | method without "=" means the method has no return type/return type is void (this is a lie) |
39+
| `def x:Unit = {...}`<br>`def x() {...}`|leaving out the "=" at a method declaration is the same as specifying "Unit"|
40+
| `val blocks = {{{{5}}}}`|every block has a return type that is passed back to the next outer block|
41+
| `val block = if (a) foo else bar`|almost everything is an expression and thus, has a return type. this includes if-else-structures|
42+
|`def x = if (System.currentTimeMillis() % 2 == 0) Integer.valueOf(1) else java.lang.Double.valueOf(2)`|here, the compiler picks the most specific supertype of both Integer and Double which is java.lang.Number (this is a lie)|
43+
|`def x(i:Int):Int = if (i==0) 1 else i*x(i-1)`|recursive methods need an explicit return type. fail.|
44+
|`val func:(Int) => String = (i:Int) => i.toString`|just so you know the syntax of a type of a function :)|
45+
|`def takesFunction(f:(Int) => String) = f(5)`| method that takes the function above as a parameter and calls it. compiler figures out the return type "string" for you.|
46+
|`def method(i:Int) = t.toString;val func = method _`|appending an "_" converts any method into a function|
47+
|`takesFunction(method)`|is also possible, the compiler does the conversion for you in obvious cases|
48+
| <h2 id="collections">Scala Collections</h2> | |
49+
|`Set(1,2,3), Buffer(1,2,3), ArrayBuffer(1,2,3), ListBuffer(1,2,3), List(1,2,3), Array(1,2,3),Vector(1,2,3), Map(1 -> "a", 2 -> "b")`|simple collection creations. scala has mutable and immutable collections.|
50+
|`mutableColl += elem`|add element to a collection|
51+
|`mutableColl -= elem`|remove element|
52+
|`mutableColl ++= elems`|add elements|
53+
|`mutableColl --= elems`|remove elements|
54+
|`elem +=: mutableColl`|adds element at the beginning of a collection|
55+
|`mutableColl :+= elem`|adds element at the end of a collection|
56+
|`mutableColl(0)`|read access by index|
57+
|`mutableColl(0) = 1`|write access by index|
58+
|`coll + elem`|create new collection that has all elements of coll and elem|
59+
|`coll - elem`|create new collection that has all elements of coll except elem|
60+
|`coll ++ elems`|create new collection that has all elements of coll and elems|
61+
|`coll -- elems`|create new collection that has all elements of coll except elems|
62+
|`coll :+ elem`|create new collection that has all elements of coll and elem at the end|
63+
|`elem +: coll`|create new collection that has all elements of coll and elem at the beginning|
64+
|`immutableColl += elem`<br>`immutableColl -= elem`<br>`immutableColl ++= elems`<br>`immutableColl --= elems`<br>`elem +=: immutableColl`<br>`immutableColl :+= elem|same as the operations without "=", but works only if "immutableColl is a var, not a val. the created collection is assigned to "immutableColl".|
65+
|`def isEven(i:Int= if (i%2==0) true else false`<br>`val evenNumbers:List[Int] = List(1,2,3,4).filter(isEven)`|scala collections are a major epic win. they have ~100 methods which operate on the data of a collection and there is *absolutely nothing* you cannot do with them.|
66+
|`val evenNumbers:List[Int] = List(1,2,3,4).filter((i:Int)=> i%2==0)`|same as above, just shorter|
67+
|`val evenNumbers = List(1,2,3,4).filter(i => i%2==0)`|same as above, just shorter. you can skip the () if there is only one parameter|
68+
|`val evenNumbers = List(1,2,3,4).filter(_ % 2 == 0)`|same as above, just shorter. you can skip part before "=>" if you use a parameter only once and replace the parameter usage by "_"|
2969
| <h2 id="functions">functions</h2> | |
3070
| <span class="label success">Good</span> `def f(x: Int) = { x*x }`<br> <span class="label important">Bad</span> `def f(x: Int) { x*x }` | define function <br> hidden error: without = it's a Unit-returning procedure; causes havoc |
3171
| <span class="label success">Good</span> `def f(x: Any) = println(x)`<br> <span class="label important">Bad</span> `def f(x) = println(x)` | define function <br> syntax error: need types for every arg. |
@@ -85,7 +125,7 @@ about: Thanks to <a href="http://brenocon.com/">Brendan O'Connor</a>, this cheat
85125
| `object O extends D { ... }` | define a singleton. (module-like) |
86126
| `trait T { ... }`<br>`class C extends T { ... }`<br>`class C extends D with T { ... }` | traits.<br>interfaces-with-implementation. no constructor params. [mixin-able]({{ site.baseurl }}/tutorials/tour/mixin-class-composition.html).
87127
| `trait T1; trait T2`<br>`class C extends T1 with T2`<br>`class C extends D with T1 with T2` | multiple traits. |
88-
| `class C extends D { override def f = ...}` | must declare method overrides. |
128+
| `class C extends D { override def f = ...}` | must declare method overrides. |
89129
| `new java.io.File("f")` | create object. |
90130
| <span class="label important">Bad</span> `new List[Int]`<br> <span class="label success">Good</span> `List(1,2,3)` | type error: abstract type<br>instead, convention: callable factory shadowing the type |
91131
| `classOf[String]` | class literal. |

0 commit comments

Comments
 (0)
0