diff --git a/_cheatsheets/index.md b/_cheatsheets/index.md index 67d4c59144..ce320d1f49 100644 --- a/_cheatsheets/index.md +++ b/_cheatsheets/index.md @@ -25,16 +25,16 @@ languages: [ba, fr, ja, pl, pt-br, zh-cn, th, ru]
var x = 5
x=6
var x = 5
x = 6
val x = 5
x=6
val x = 5
x = 6
var x: Double = 5
def f(x: Int) = { x * x }
def f(x: Int) { x * x }
=
it’s a procedure returning Unit
; causes havoc. Deprecated in Scala 2.13.def f(x: Any) = println(x)
def f(x) = println(x)
type R = Double
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(2 *)
(1 to 5).map(* 2)
2 * _
for sanity’s sake instead.(1 to 5).map(x => x * x)
(1 to 5).map { x =>
@@ -78,7 +74,7 @@ languages: [ba, fr, ja, pl, pt-br, zh-cn, th, ru]
println(y)
y
}
(1 to 5) filter {
@@ -86,49 +82,49 @@ languages: [ba, fr, ja, pl, pt-br, zh-cn, th, ru]
} map {
_ * 2
}
def compose(g: R => R, h: R => R) =
(x: R) => g(h(x))
val f = compose(_ * 2, _ - 1)
val zscore =
+ val zscore =
(mean: R, sd: R) =>
- (x:R) =>
+ (x: R) =>
(x - mean) / sd
- currying, obvious syntax.
+ Currying, obvious syntax.
def zscore(mean:R, sd:R) =
- (x:R) =>
+ def zscore(mean: R, sd: R) =
+ (x: R) =>
(x - mean) / sd
- currying, obvious syntax
+ Currying, obvious syntax.
def zscore(mean:R, sd:R)(x:R) =
+ def zscore(mean: R, sd: R)(x: R) =
(x - mean) / sd
- currying, sugar syntax. but then:
+ Currying, sugar syntax. But then:
val normer =
zscore(7, 0.4) _
def mapmake[T](g: T => T)(seq: List[T]) =
seq.map(g)
5.+(3); 5 + 3
(1 to 5) map (_ * 2)
def sum(args: Int*) =
+ def sum(args: Int*) =
args.reduceLeft(_+_)
- varargs.
+ Varargs.
import scala.collection._
import scala.collection.Vector
import scala.collection.{Vector, Sequence}
import scala.collection.{Vector => Vec28}
import java.util.{Date => _, _}
java.util
except Date
.package pkg
package pkg {
@@ -156,7 +152,7 @@ languages: [ba, fr, ja, pl, pt-br, zh-cn, th, ru]
}
package object pkg {
...
}
(1, 2, 3)
Tuple3
)Tuple3
).var (x, y, z) = (1, 2, 3)
var x, y, z = (1, 2, 3)
var xs = List(1, 2, 3)
xs(2)
1 :: List(2, 3)
1 to 5
same as 1 until 6
1 to 10 by 2
()
void
in C and Java.void
in C and Java.if (check) happy else sad
if (check) happy
if (check) happy else ()
while (x < 5) {
+ while (x < 5) {
println(x)
x += 1
}
- while loop.
+ While loop.
do {
println(x)
x += 1
} while (x < 5)
import scala.util.control.Breaks._
+
breakable {
- for (x <- xs) {
- if (Math.random < 0.1)
+ for (x <- xs) {
+ if (Math.random < 0.1)
break
}
}
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
+ yield x * y
(xs zip ys) map {
case (x, y) => x * y
}
for (x <- xs; y <- ys)
-yield x * y
+ yield x * y
xs flatMap { x =>
ys map { y =>
x * y
}
}
for (x <- xs; y <- ys) {
+ for (x <- xs; y <- ys) {
val div = x / y.toFloat
println("%d/%d = %.1f".format(x, y, div))
}
- for comprehension: imperative-ish
sprintf-style
+ For-comprehension: imperative-ish.
sprintf
style.
for (i <- 1 to 5) {
+ for (i <- 1 to 5) {
println(i)
}
- for comprehension: iterate including the upper bound
+ For-comprehension: iterate including the upper bound.
for (i <- 1 until 5) {
+ for (i <- 1 until 5) {
println(i)
}
- for comprehension: iterate omitting the upper bound
+ For-comprehension: iterate omitting the upper bound.
(xs zip ys) map {
- case (x, y) => x * y
+ case (x, y) => x * y
}
(xs zip ys) map {
(x, y) => x * y
}
v42
is interpreted as a name matching any Int value, and “42” is printed.v42
, and “Not 42” is printed.`v42`
with backticks is interpreted as the existing val v42
, 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.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 bodyx
is only available in class body.class C(val x: R)
var c = new C(4)
c.x
class C(var x: R) {
@@ -337,65 +334,65 @@ yield x * y
private var secret = 1
def this = this(42)
}new {
...
}
abstract class D { ... }
class C extends D { ... }
class D(var x: R)
class C(x: R) extends D(x)
object O extends D { ... }
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
class C extends D { override def f = ...}
new java.io.File("f")
new List[Int]
List(1, 2, 3)
classOf[String]
x.isInstanceOf[String]
x.asInstanceOf[String]
x: String
Some(42)
None
Option(null) == None
-Option(obj.unsafeMethod)
Some(null) != None
+ val optStr: Option[String] = None
@@ -426,26 +425,23 @@ Option(obj.unsafeMethod)val upper = for {
- name <- request.getParameter("name")
- trimmed <- Some(name.trim)
+ name <- request.getParameter("name")
+ trimmed <- Some(name.trim)
if trimmed.length != 0
- upper <- Some(trimmed.toUpperCase)
+ upper <- Some(trimmed.toUpperCase)
} yield upper
println(upper.getOrElse(""))
option.map(f(_))
@@ -454,7 +450,7 @@ println(upper.getOrElse(""))option.flatMap(f(_))
@@ -463,7 +459,7 @@ println(upper.getOrElse(""))optionOfOption.flatten
@@ -472,7 +468,7 @@ println(upper.getOrElse(""))option.foreach(f(_))
@@ -481,7 +477,7 @@ println(upper.getOrElse(""))option.fold(y)(f(_))
@@ -490,7 +486,7 @@ println(upper.getOrElse(""))option.collect {
@@ -498,12 +494,11 @@ println(upper.getOrElse(""))
option match {
- case Some(x)
- if f.isDefinedAt(x) => ...
- case Some(_) => None
- case None => None
+ case Some(x) if f.isDefinedAt(x) => ...
+ case Some(_) => None
+ case None => None
}
- option.isDefined
@@ -512,7 +507,7 @@ println(upper.getOrElse(""))true
if not empty.option.isEmpty
@@ -521,7 +516,7 @@ println(upper.getOrElse(""))true
if empty.option.nonEmpty
@@ -530,7 +525,7 @@ println(upper.getOrElse(""))true
if not empty.option.size
@@ -539,7 +534,7 @@ println(upper.getOrElse(""))0
if empty, otherwise 1
.option.orElse(Some(y))
@@ -548,7 +543,7 @@ println(upper.getOrElse(""))option.getOrElse(y)
@@ -557,7 +552,7 @@ println(upper.getOrElse(""))option.get
@@ -566,7 +561,7 @@ println(upper.getOrElse(""))option.orNull
@@ -575,7 +570,7 @@ println(upper.getOrElse(""))null
if empty.option.filter(f)
@@ -584,7 +579,7 @@ println(upper.getOrElse(""))option.filterNot(f(_))
@@ -593,25 +588,27 @@ println(upper.getOrElse(""))option.exists(f(_))
same as
option match {
case Some(x) if f(x) => true
- case _ => false
+ case Some(_) => false
+ case None => false
}
false
if empty.option.forall(f(_))
same as
option match {
case Some(x) if f(x) => true
- case None => false
+ case Some(_) => false
+ case None => true
}
true
if empty.option.contains(y)
@@ -620,7 +617,7 @@ println(upper.getOrElse(""))false
if empty.