diff --git a/_overviews/cheatsheets/index.md b/_overviews/cheatsheets/index.md index b59d3fcdcf..6ccaad378d 100644 --- a/_overviews/cheatsheets/index.md +++ b/_overviews/cheatsheets/index.md @@ -26,15 +26,15 @@ permalink: /cheatsheets/index.html
var x = 5
var x = 5
x=6
val x = 5
x=6
val x = 5
x=6
var x: Double = 5
var x: Double = 5
def f(x: Int) = { x*x }
def f(x: Int) { x*x }
def f(x: Int) = { x * x }
def f(x: Int) { x * x }
def f(x: Any) = println(x)
def f(x) = println(x)
def f(x: Any) = println(x)
def f(x) = println(x)
type R = Double
type R = Double
def f(x: R)
vs.def f(x: => R)
def f(x: R)
vs.def f(x: => R)
(x:R) => x*x
(x:R) => x * x
(1 to 5).map(_*2)
vs.(1 to 5).reduceLeft( _+_ )
(1 to 5).map(_ * 2)
vs.(1 to 5).reduceLeft( _ + _ )
(1 to 5).map( x => x*x )
(1 to 5).map( x => x * x )
(1 to 5).map(2*)
(1 to 5).map(*2)
2*_
for sanity’s sake instead.(1 to 5).map(2 *)
(1 to 5).map(* 2)
2 * _
for sanity’s sake instead.(1 to 5).map { x => val y=x*2; println(y); y }
(1 to 5).map { x =>
+ val y = x * 2
+ println(y)
+ y
+}
(1 to 5) filter {_%2 == 0} map {_*2}
(1 to 5) filter {
+ _ % 2 == 0
+} map {
+ _ * 2
+}
def compose(g:R=>R, h:R=>R) = (x:R) => g(h(x))
val f = compose({_*2}, {_-1})
def compose(g: R => R, h: R => R) =
+ (x: R) => g(h(x))
val f = compose(_ * 2, _ - 1)
val zscore = (mean:R, sd:R) => (x:R) => (x-mean)/sd
val zscore =
+ (mean: R, sd: R) =>
+ (x:R) =>
+ (x - mean) / sd
def zscore(mean:R, sd:R) = (x:R) => (x-mean)/sd
def zscore(mean:R, sd:R) =
+ (x:R) =>
+ (x - mean) / sd
def zscore(mean:R, sd:R)(x:R) = (x-mean)/sd
def zscore(mean:R, sd:R)(x:R) =
+ (x - mean) / sd
val normer = zscore(7, 0.4) _
val normer =
+ zscore(7, 0.4) _
def mapmake[T](g:T=>T)(seq: List[T]) = seq.map(g)
def mapmake[T](g: T => T)(seq: List[T]) =
+ seq.map(g)
5.+(3); 5 + 3
(1 to 5) map (_*2)
5.+(3); 5 + 3
(1 to 5) map (_ * 2)
def sum(args: Int*) = args.reduceLeft(_+_)
def sum(args: Int*) =
+ args.reduceLeft(_+_)
import scala.collection._
import scala.collection._
import scala.collection.Vector
import scala.collection.{Vector, Sequence}
import scala.collection.Vector
import scala.collection.{Vector, Sequence}
import scala.collection.{Vector => Vec28}
import scala.collection.{Vector => Vec28}
import java.util.{Date => _, _}
import java.util.{Date => _, _}
package pkg
at start of file package pkg { ... }
package pkg
package pkg {
+ ...
+}
package object pkg {
+ ...
+}
(1,2,3)
(1, 2, 3)
Tuple3
)var (x,y,z) = (1,2,3)
var (x, y, z) = (1, 2, 3)
var x,y,z = (1,2,3)
var x, y, z = (1, 2, 3)
var xs = List(1,2,3)
var xs = List(1, 2, 3)
xs(2)
xs(2)
1 :: List(2,3)
1 :: List(2, 3)
1 to 5
same as 1 until 6
1 to 10 by 2
1 to 5
same as 1 until 6
1 to 10 by 2
()
(empty parens)()
void
in C and Java.if (check) happy else sad
if (check) happy else sad
if (check) happy
+ if (check) happy
if (check) happy else ()
if (check) happy else ()
while (x < 5) { println(x); x += 1}
while (x < 5) {
+ println(x)
+ x += 1
+}
do { println(x); x += 1} while (x < 5)
do {
+ println(x)
+ x += 1
+} while (x < 5)
for (x <- xs if x%2 == 0) yield x*10
+ for (x <- xs if x%2 == 0)
+yield x * 10
xs.filter(_%2 == 0).map(_*10)
xs.filter(_%2 == 0).map( _ * 10)
for ((x,y) <- xs zip ys) yield x*y
+ for ((x, y) <- xs zip ys)
+yield x * y
(xs zip ys) map { case (x,y) => x*y }
(xs zip ys) map {
+ case (x, y) => x * y
+}
for (x <- xs; y <- ys) yield x*y
+ for (x <- xs; y <- ys)
+yield x * y
xs flatMap {x => ys map {y => x*y}}
xs flatMap { x =>
+ ys map { y =>
+ x * y
+ }
+}
for (x <- xs; y <- ys) {
- println("%d/%d = %.1f".format(x, y, x/y.toFloat))
+ val div = x / y.toFloat
+ println("%d/%d = %.1f".format(x, y, div))
}
(xs zip ys) map { case (x,y) => x*y }
(xs zip ys) map( (x,y) => x*y )
(xs zip ys) map {
+ case (x, y) => x * y
+}
(xs zip ys) map {
+ (x, y) => x * y
+}
val v42 = 42
Some(3) match {
- case Some(v42) => println("42")
- case _ => println("Not 42")
+ case Some(v42) => println("42")
+ case _ => println("Not 42")
}
val v42 = 42
Some(3) match {
- case Some(`v42`) => println("42")
- case _ => println("Not 42")
+ case Some(`v42`) => println("42")
+ case _ => println("Not 42")
}
v42
, and “Not 42” is printed.v42
, and “Not 42” is printed.val UppercaseVal = 42
Some(3) match {
- case Some(UppercaseVal) => println("42")
- case _ => println("Not 42")
+ case Some(UppercaseVal) => println("42")
+ case _ => println("Not 42")
}
UppercaseVal
is treated as an existing val, rather than a new pattern variable, because it starts with an uppercase letter. Thus, the value contained within UppercaseVal
is checked against 3
, and “Not 42” is printed.UppercaseVal
is treated as an existing val, rather than a new pattern variable, because it starts with an uppercase letter. Thus, the value contained within UppercaseVal
is checked against 3
, and “Not 42” is printed.class C(x: R)
x
is only available in class bodyclass C(x: R)
x
is only available in class bodyclass C(val x: R)
var c = new C(4)
c.x
class C(val x: R)
var c = new C(4)
c.x
new{ ... }
new {
+ ...
+}
abstract class D { ... }
abstract class D { ... }
class C extends D { ... }
class C extends D { ... }
class D(var x: R)
class C(x: R) extends D(x)
class D(var x: R)
class C(x: R) extends D(x)
object O extends D { ... }
object O extends D { ... }
trait T { ... }
class C extends T { ... }
class C extends D with T { ... }
trait T { ... }
class C extends T { ... }
class C extends D with T { ... }
trait T1; trait T2
class C extends T1 with T2
class C extends D with T1 with T2
trait T1; trait T2
class C extends T1 with T2
class C extends D with T1 with T2
class C extends D { override def f = ...}
class C extends D { override def f = ...}
new java.io.File("f")
new java.io.File("f")
new List[Int]
List(1,2,3)
new List[Int]
List(1, 2, 3)
classOf[String]
classOf[String]
x.isInstanceOf[String]
x.isInstanceOf[String]
x.asInstanceOf[String]
x.asInstanceOf[String]
x: String
x: String