From d6c7f7744eeb1478a2d0486eb35a83bce9d23c16 Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Wed, 24 Jul 2019 22:33:07 +0200 Subject: [PATCH 01/18] whitespace, dots, formatting --- _cheatsheets/index.md | 104 +++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 53 deletions(-) diff --git a/_cheatsheets/index.md b/_cheatsheets/index.md index 67d4c59144..24f6bb84ee 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

Good
x=6
- variable +
var x = 5

Good
x = 6
+ variable. -
val x = 5

Bad
x=6
- constant +
val x = 5

Bad
x = 6
+ constant.
var x: Double = 5
- explicit type + explicit type. functions @@ -57,15 +57,15 @@ languages: [ba, fr, ja, pl, pt-br, zh-cn, th, ru] call-by-value
call-by-name (lazy parameters) -
(x:R) => x * x
+
(x: R) => x * x
anonymous function -
(1 to 5).map(_ * 2)
vs.
(1 to 5).reduceLeft( _ + _ )
+
(1 to 5).map(_ * 2)
vs.
(1 to 5).reduceLeft(_ + _)
anonymous function: underscore is positionally matched arg. -
(1 to 5).map( x => x * x )
+
(1 to 5).map(x => x * x)
anonymous function: to use an arg twice, have to name it. @@ -94,15 +94,15 @@ languages: [ba, fr, ja, pl, pt-br, zh-cn, th, ru] anonymous functions: to pass in multiple blocks, need outer parens. -
val zscore = 
+      
val zscore =
   (mean: R, sd: R) =>
-    (x:R) => 
+    (x:R) =>
       (x - mean) / sd
currying, obvious syntax.
def zscore(mean:R, sd:R) =
-  (x:R) => 
+  (x:R) =>
     (x - mean) / sd
currying, obvious syntax @@ -126,7 +126,7 @@ languages: [ba, fr, ja, pl, pt-br, zh-cn, th, ru] infix sugar. -
def sum(args: Int*) = 
+      
def sum(args: Int*) =
   args.reduceLeft(_+_)
varargs. @@ -209,7 +209,7 @@ languages: [ba, fr, ja, pl, pt-br, zh-cn, th, ru] conditional sugar. -
while (x < 5) { 
+      
while (x < 5) {
   println(x)
   x += 1
 }
@@ -224,59 +224,60 @@ languages: [ba, fr, ja, pl, pt-br, zh-cn, th, ru]
import scala.util.control.Breaks._
+
 breakable {
-  for (x <- xs) {
-    if (Math.random < 0.1)
+  for (x <- xs) {
+    if (Math.random < 0.1)
       break
   }
 }
break. (slides) -
for (x <- xs if x%2 == 0)
-yield x * 10
+
for (x <- xs if x % 2 == 0)
+  yield x * 10

same as
-
xs.filter(_%2 == 0).map( _ * 10)
- for comprehension: filter/map +
xs.filter(_ % 2 == 0).map(_ * 10)
+ for comprehension: filter/map.
for ((x, y) <- xs zip ys)
-yield x * y
+ yield x * y

same as
(xs zip ys) map {
   case (x, y) => x * y
 }
- for comprehension: destructuring bind + for comprehension: destructuring bind.
for (x <- xs; y <- ys)
-yield x * y
+ yield x * y

same as
xs flatMap { x =>
   ys map { y =>
     x * y
   }
 }
- for comprehension: cross product + for comprehension: cross product. -
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. pattern matching @@ -284,7 +285,7 @@ yield x * y
Good
(xs zip ys) map {
-  case (x, y) => x * y 
+  case (x, y) => x * y
 }

Bad
(xs zip ys) map {
   (x, y) => x * y
 }
@@ -306,7 +307,7 @@ yield x * y
case `v42` => println("42") case _ => println("Not 42") }
- ”`v42`” with backticks is interpreted as the existing val
v42
, and “Not 42” is printed. + ”`v42`” with backticks is interpreted as the existing val v42, and “Not 42” is printed. Good
@@ -379,7 +380,7 @@ yield x * y
Bad
new List[Int]

Good
List(1, 2, 3)
- type error: abstract type
instead, convention: callable factory shadowing the type + type error: abstract type
instead, convention: callable factory shadowing the type.
classOf[String]
@@ -387,15 +388,15 @@ yield x * y
x.isInstanceOf[String]
- type check (runtime) + type check (runtime).
x.asInstanceOf[String]
- type cast (runtime) + type cast (runtime).
x: String
- ascription (compile time) + ascription (compile time). @@ -404,16 +405,16 @@ yield x * y
Some(42)
- Construct a non empty optional value + Construct a non empty optional value.
None
- The singleton empty optional value + The singleton empty optional value.
Option(null) == None
 Option(obj.unsafeMethod)
- Null-safe optional value factory + Null-safe optional value factory.
val optStr: Option[String] = None
@@ -426,26 +427,23 @@ Option(obj.unsafeMethod) request.getParameter("name") val upper = name.map { _.trim -} -.filter { +} filter { _.length != 0 -} -.map { +} map { _.toUpperCase } -println(upper.getOrElse("")) - - Pipeline style +println(upper.getOrElse("")) + Pipeline style.
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(""))
- for-comprehension syntax + for-comprehension syntax.
option.map(f(_))
@@ -454,7 +452,7 @@ println(upper.getOrElse("")) case Some(x) => Some(f(x)) case None => None } - Apply a function on the optional value + Apply a function on the optional value.
option.flatMap(f(_))
@@ -463,7 +461,7 @@ println(upper.getOrElse("")) case Some(x) => f(x) case None => None } - Same as map but function must return an optional value + Same as map but function must return an optional value.
optionOfOption.flatten
@@ -472,7 +470,7 @@ println(upper.getOrElse("")) case Some(Some(x)) => Some(x) case _ => None } - Extract nested option + Extract nested option.
option.foreach(f(_))
@@ -490,7 +488,7 @@ println(upper.getOrElse("")) case Some(x) => f(x) case None => y } - Apply function on optional value, return default if empty + Apply function on optional value, return default if empty.
option.collect {

From a29722f419e75d3c26bb648e914f3ab565ee7df1 Mon Sep 17 00:00:00 2001
From: Ivan Kuchin 
Date: Wed, 24 Jul 2019 22:34:20 +0200
Subject: [PATCH 02/18] note about import scala.language.postfixOps

---
 _cheatsheets/index.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/_cheatsheets/index.md b/_cheatsheets/index.md
index 24f6bb84ee..07b5db34e2 100644
--- a/_cheatsheets/index.md
+++ b/_cheatsheets/index.md
@@ -70,7 +70,7 @@ languages: [ba, fr, ja, pl, pt-br, zh-cn, th, ru]
     
     
       Good
(1 to 5).map(2 *)

Bad
(1 to 5).map(* 2)
- anonymous function: bound infix method.
Use 2 * _ for sanity’s sake instead. + anonymous function: bound infix method.
Use 2 * _ for sanity’s sake instead.
Will require import scala.language.postfixOps in the future.
(1 to 5).map { x =>

From 5c0315e974deb8b8ec416cebe63cca70f5e22781 Mon Sep 17 00:00:00 2001
From: Ivan Kuchin 
Date: Wed, 24 Jul 2019 22:34:37 +0200
Subject: [PATCH 03/18] note about Option(null) != Some(null)

---
 _cheatsheets/index.md | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/_cheatsheets/index.md b/_cheatsheets/index.md
index 07b5db34e2..6ddef0c715 100644
--- a/_cheatsheets/index.md
+++ b/_cheatsheets/index.md
@@ -413,7 +413,10 @@ breakable {
     
     
       
Option(null) == None
-Option(obj.unsafeMethod)
+Option(obj.unsafeMethod)
+ but +
Some(null) != None
+ Null-safe optional value factory. From 02c498173ead0ff40beaca5a7883d7b83886d400 Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Wed, 24 Jul 2019 22:35:03 +0200 Subject: [PATCH 04/18] note about foreach returning Unit --- _cheatsheets/index.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/_cheatsheets/index.md b/_cheatsheets/index.md index 6ddef0c715..a1cc65fc58 100644 --- a/_cheatsheets/index.md +++ b/_cheatsheets/index.md @@ -481,8 +481,9 @@ println(upper.getOrElse(""))
option match {
   case Some(x) => f(x)
   case None    => ()
-}
- Apply a procedure on optional value +} +() + Apply a procedure on optional value. Returns Unit
option.fold(y)(f(_))
From cd381e233d93e9acfd6e3794c841040443ed716e Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Wed, 24 Jul 2019 22:35:55 +0200 Subject: [PATCH 05/18] fix false to true in return of None.forall --- _cheatsheets/index.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_cheatsheets/index.md b/_cheatsheets/index.md index a1cc65fc58..f649e39144 100644 --- a/_cheatsheets/index.md +++ b/_cheatsheets/index.md @@ -610,8 +610,8 @@ println(upper.getOrElse(""))
option.forall(f(_))
same as
option match {
-  case Some(x) if f(x) => true
-  case None            => false
+  case Some(x) => f(x)
+  case None    => true
 }
Apply predicate on optional value or true if empty From 3644e6858d425007782579879eeea6b54e0d00c6 Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Wed, 24 Jul 2019 22:36:47 +0200 Subject: [PATCH 06/18] change examples of flatten and collect --- _cheatsheets/index.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/_cheatsheets/index.md b/_cheatsheets/index.md index f649e39144..7d5312bb38 100644 --- a/_cheatsheets/index.md +++ b/_cheatsheets/index.md @@ -470,8 +470,8 @@ println(upper.getOrElse(""))
optionOfOption.flatten
same as
optionOfOption match {
-  case Some(Some(x)) => Some(x)
-  case _             => None
+  case Some(x: Option[_]) => x
+  case None               => None
 }
Extract nested option. @@ -502,8 +502,7 @@ println(upper.getOrElse(""))
option match {
   case Some(x)
     if f.isDefinedAt(x) => ...
-  case Some(_)          => None
-  case None             => None
+  case _                => None
 }
Apply partial pattern match on optional value From 46e3670ce5a1e6a348a323bc5c1db6c58741f407 Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Wed, 24 Jul 2019 23:09:15 +0200 Subject: [PATCH 07/18] change example for exists --- _cheatsheets/index.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_cheatsheets/index.md b/_cheatsheets/index.md index 7d5312bb38..9cc18d83b5 100644 --- a/_cheatsheets/index.md +++ b/_cheatsheets/index.md @@ -600,8 +600,8 @@ println(upper.getOrElse(""))
option.exists(f(_))
same as
option match {
-  case Some(x) if f(x) => true
-  case _               => false
+  case Some(x) => f(x)
+  case None    => false
 }
Apply predicate on optional value or false if empty From 397aaa001abcd221526210d258406f339fa0a180 Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Wed, 24 Jul 2019 22:52:55 +0200 Subject: [PATCH 08/18] more whitespace, dots, formatting --- _cheatsheets/index.md | 167 +++++++++++++++++++++--------------------- 1 file changed, 83 insertions(+), 84 deletions(-) diff --git a/_cheatsheets/index.md b/_cheatsheets/index.md index 9cc18d83b5..d817125af0 100644 --- a/_cheatsheets/index.md +++ b/_cheatsheets/index.md @@ -26,15 +26,15 @@ languages: [ba, fr, ja, pl, pt-br, zh-cn, th, ru]
var x = 5

Good
x = 6
- variable. + Variable.
val x = 5

Bad
x = 6
- constant. + Constant.
var x: Double = 5
- explicit type. + Explicit type. functions @@ -42,35 +42,35 @@ languages: [ba, fr, ja, pl, pt-br, zh-cn, th, ru] Good
def f(x: Int) = { x * x }

Bad
def f(x: Int)   { x * x }
- define function
hidden error: without = it’s a Unit-returning procedure; causes havoc + Define function.
Hidden error: without = it’s a Unit-returning procedure; causes havoc. Good
def f(x: Any) = println(x)

Bad
def f(x) = println(x)
- define function
syntax error: need types for every arg. + Define function.
Syntax error: need types for every arg.
type R = Double
- type alias + Type alias.
def f(x: R)
vs.
def f(x: => R)
- call-by-value
call-by-name (lazy parameters) + Call-by-value.

Call-by-name (lazy parameters).
(x: R) => x * x
- anonymous function + Anonymous function.
(1 to 5).map(_ * 2)
vs.
(1 to 5).reduceLeft(_ + _)
- anonymous function: underscore is positionally matched arg. + Anonymous function: underscore is positionally matched arg.
(1 to 5).map(x => x * x)
- anonymous function: to use an arg twice, have to name it. + Anonymous function: to use an arg twice, have to name it. Good
(1 to 5).map(2 *)

Bad
(1 to 5).map(* 2)
- anonymous function: bound infix method.
Use 2 * _ for sanity’s sake instead.
Will require import scala.language.postfixOps in the future. + Anonymous function: bound infix method.
Use 2 * _ for sanity’s sake instead.
Will require import scala.language.postfixOps in the future.
(1 to 5).map { x =>
@@ -78,7 +78,7 @@ languages: [ba, fr, ja, pl, pt-br, zh-cn, th, ru]
   println(y)
   y
 }
- anonymous function: block style returns last expression. + Anonymous function: block style returns last expression.
(1 to 5) filter {
@@ -86,49 +86,49 @@ languages: [ba, fr, ja, pl, pt-br, zh-cn, th, ru]
 } map {
   _ * 2
 }
- anonymous functions: pipeline style. (or parens too). + Anonymous functions: pipeline style (or parens too).
def compose(g: R => R, h: R => R) =
   (x: R) => g(h(x))

val f = compose(_ * 2, _ - 1)
- anonymous functions: to pass in multiple blocks, need outer parens. + Anonymous functions: to pass in multiple blocks, need outer parens.
val zscore =
   (mean: R, sd: R) =>
     (x:R) =>
       (x - mean) / sd
- currying, obvious syntax. + Currying, obvious syntax.
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) =
   (x - mean) / sd
- currying, sugar syntax. but then: + Currying, sugar syntax. But then:
val normer =
   zscore(7, 0.4) _
- need trailing underscore to get the partial, only for the sugar version. + Need trailing underscore to get the partial, only for the sugar version.
def mapmake[T](g: T => T)(seq: List[T]) =
   seq.map(g)
- generic type. + Generic type.
5.+(3); 5 + 3

(1 to 5) map (_ * 2)
- infix sugar. + Infix sugar.
def sum(args: Int*) =
   args.reduceLeft(_+_)
- varargs. + Varargs. packages @@ -136,19 +136,19 @@ languages: [ba, fr, ja, pl, pt-br, zh-cn, th, ru]
import scala.collection._
- wildcard import. + Wildcard import.
import scala.collection.Vector

import scala.collection.{Vector, Sequence}
- selective import. + Selective import.
import scala.collection.{Vector => Vec28}
- renaming import. + Renaming import.
import java.util.{Date => _, _}
- import all from java.util except Date. + Import all from java.util except Date. At start of file:
package pkg

Packaging by scope:
package pkg {
@@ -156,7 +156,7 @@ languages: [ba, fr, ja, pl, pt-br, zh-cn, th, ru]
 }

Package singleton:
package object pkg {
   ...
 }
- declare a package. + Declare a package. data structures @@ -164,35 +164,35 @@ languages: [ba, fr, ja, pl, pt-br, zh-cn, th, ru]
(1, 2, 3)
- tuple literal. (Tuple3) + Tuple literal (Tuple3).
var (x, y, z) = (1, 2, 3)
- destructuring bind: tuple unpacking via pattern matching. + Destructuring bind: tuple unpacking via pattern matching. Bad
var x, y, z = (1, 2, 3)
- hidden error: each assigned to the entire tuple. + Hidden error: each assigned to the entire tuple.
var xs = List(1, 2, 3)
- list (immutable). + List (immutable).
xs(2)
- paren indexing. (slides) + Paren indexing (slides).
1 :: List(2, 3)
- cons. + Cons.
1 to 5
same as
1 until 6

1 to 10 by 2
- range sugar. + Range sugar.
()
- Empty parens is singleton value of the Unit type
Equivalent to void in C and Java. + Empty parens is singleton value of the Unit type.
Equivalent to void in C and Java. control constructs @@ -200,27 +200,27 @@ languages: [ba, fr, ja, pl, pt-br, zh-cn, th, ru]
if (check) happy else sad
- conditional. + Conditional.
if (check) happy

same as
if (check) happy else ()
- conditional sugar. + Conditional sugar.
while (x < 5) {
   println(x)
   x += 1
 }
- while loop. + While loop.
do {
   println(x)
   x += 1
 } while (x < 5)
- do while loop. + Do-while loop.
import scala.util.control.Breaks._
@@ -231,14 +231,14 @@ breakable {
       break
   }
 }
- break. (slides) + Break (slides).
for (x <- xs if x % 2 == 0)
   yield x * 10

same as
xs.filter(_ % 2 == 0).map(_ * 10)
- for comprehension: filter/map. + For comprehension: filter/map.
for ((x, y) <- xs zip ys)
@@ -247,7 +247,7 @@ breakable {
       
(xs zip ys) map {
   case (x, y) => x * y
 }
- for comprehension: destructuring bind. + For-comprehension: destructuring bind.
for (x <- xs; y <- ys)
@@ -258,26 +258,26 @@ breakable {
     x * y
   }
 }
- for comprehension: cross product. + For-comprehension: cross product.
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) {
   println(i)
 }
- for comprehension: iterate including the upper bound. + For-comprehension: iterate including the upper bound.
for (i <- 1 until 5) {
   println(i)
 }
- for comprehension: iterate omitting the upper bound. + For-comprehension: iterate omitting the upper bound. pattern matching @@ -289,7 +289,7 @@ breakable { }

Bad
(xs zip ys) map {
   (x, y) => x * y
 }
- use case in function args for pattern matching. + Use case in function args for pattern matching. Bad
@@ -298,7 +298,7 @@ breakable { case v42 => println("42") case _ => println("Not 42") } - “v42” is interpreted as a name matching any Int value, and “42” is printed. + v42 is interpreted as a name matching any Int value, and “42” is printed. Good
@@ -307,7 +307,7 @@ breakable { case `v42` => println("42") case _ => println("Not 42") } - ”`v42`” with backticks is interpreted as the existing val v42, and “Not 42” is printed. + `v42` with backticks is interpreted as the existing val v42, and “Not 42” is printed. Good
@@ -316,7 +316,7 @@ breakable { case 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. object orientation @@ -324,11 +324,11 @@ breakable {
class C(x: R)
- constructor params -
x
is only available in class body + Constructor params - x is only available in class body.
class C(val x: R)

var c = new C(4)

c.x
- constructor params - automatic public member defined + Constructor params - automatic public member defined.
class C(var x: R) {
@@ -338,65 +338,65 @@ breakable {
   private var secret = 1
   def this = this(42)
 }
- constructor is class body
declare a public member
declare a gettable but not settable member
declare a private member
alternative constructor + Constructor is class body.
Declare a public member.
Declare a gettable but not settable member.
Declare a private member.
Alternative constructor.
new {
   ...
 }
- anonymous class + Anonymous class.
abstract class D { ... }
- define an abstract class. (non-createable) + Define an abstract class (non-createable).
class C extends D { ... }
- define an inherited class. + Define an inherited class.
class D(var x: R)

class C(x: R) extends D(x)
- inheritance and constructor params. (wishlist: automatically pass-up params by default) + Inheritance and constructor params (wishlist: automatically pass-up params by default).
object O extends D { ... }
- define a singleton. (module-like) + Define a singleton (module-like).
trait T { ... }

class C extends T { ... }

class C extends D with T { ... }
- traits.
interfaces-with-implementation. no constructor params. mixin-able. + Traits.
Interfaces-with-implementation. No constructor params. mixin-able.
trait T1; trait T2

class C extends T1 with T2

class C extends D with T1 with T2
- multiple traits. + Multiple traits.
class C extends D { override def f = ...}
- must declare method overrides. + Must declare method overrides.
new java.io.File("f")
- create object. + Create object. Bad
new List[Int]

Good
List(1, 2, 3)
- type error: abstract type
instead, convention: callable factory shadowing the type. + Type error: abstract type.
Instead, convention: callable factory shadowing the type.
classOf[String]
- class literal. + Class literal.
x.isInstanceOf[String]
- type check (runtime). + Type check (runtime).
x.asInstanceOf[String]
- type cast (runtime). + Type cast (runtime).
x: String
- ascription (compile time). + Ascription (compile time). @@ -415,8 +415,7 @@ breakable {
Option(null) == None
 Option(obj.unsafeMethod)
but -
Some(null) != None
- +
Some(null) != None
Null-safe optional value factory. @@ -446,7 +445,7 @@ println(upper.getOrElse("")) upper <- Some(trimmed.toUpperCase) } yield upper println(upper.getOrElse("")) - for-comprehension syntax. + For-comprehension syntax.
option.map(f(_))
@@ -483,7 +482,7 @@ println(upper.getOrElse("")) case None => () } () - Apply a procedure on optional value. Returns Unit + Apply a procedure on optional value. Returns Unit.
option.fold(y)(f(_))
@@ -504,7 +503,7 @@ println(upper.getOrElse("")) if f.isDefinedAt(x) => ... case _ => None } - Apply partial pattern match on optional value + Apply partial pattern match on optional value.
option.isDefined
@@ -513,7 +512,7 @@ println(upper.getOrElse("")) case Some(_) => true case None => false } - True if not empty + true if not empty.
option.isEmpty
@@ -522,7 +521,7 @@ println(upper.getOrElse("")) case Some(_) => false case None => true } - True if empty + true if empty.
option.nonEmpty
@@ -531,7 +530,7 @@ println(upper.getOrElse("")) case Some(_) => true case None => false } - True if not empty + true if not empty.
option.size
@@ -540,7 +539,7 @@ println(upper.getOrElse("")) case Some(_) => 1 case None => 0 } - Zero if empty, otherwise one + 0 if empty, otherwise 1.
option.orElse(Some(y))
@@ -549,7 +548,7 @@ println(upper.getOrElse("")) case Some(x) => Some(x) case None => Some(y) } - Evaluate and return alternate optional value if empty + Evaluate and return alternate optional value if empty.
option.getOrElse(y)
@@ -558,7 +557,7 @@ println(upper.getOrElse("")) case Some(x) => x case None => y } - Evaluate and return default value if empty + Evaluate and return default value if empty.
option.get
@@ -567,7 +566,7 @@ println(upper.getOrElse("")) case Some(x) => x case None => throw new Exception } - Return value, throw exception if empty + Return value, throw exception if empty.
option.orNull
@@ -576,7 +575,7 @@ println(upper.getOrElse("")) case Some(x) => x case None => null } - Return value, null if empty + Return value, null if empty.
option.filter(f)
@@ -585,7 +584,7 @@ println(upper.getOrElse("")) case Some(x) if f(x) => Some(x) case _ => None } - Optional value satisfies predicate + Optional value satisfies predicate.
option.filterNot(f(_))
@@ -594,7 +593,7 @@ println(upper.getOrElse("")) case Some(x) if !f(x) => Some(x) case _ => None } - Optional value doesn't satisfy predicate + Optional value doesn't satisfy predicate.
option.exists(f(_))
@@ -603,7 +602,7 @@ println(upper.getOrElse("")) case Some(x) => f(x) case None => false } - Apply predicate on optional value or false if empty + Apply predicate on optional value or false if empty.
option.forall(f(_))
@@ -612,7 +611,7 @@ println(upper.getOrElse("")) case Some(x) => f(x) case None => true } - Apply predicate on optional value or true if empty + Apply predicate on optional value or true if empty.
option.contains(y)
@@ -621,7 +620,7 @@ println(upper.getOrElse("")) case Some(x) => x == y case None => false } - Checks if value equals optional value or false if empty + Checks if value equals optional value or false if empty. From 05626b971a7fa3aa269f8077dd9606388d75adec Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Thu, 25 Jul 2019 17:45:48 +0200 Subject: [PATCH 09/18] fix weird looking Unit-returning --- _cheatsheets/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_cheatsheets/index.md b/_cheatsheets/index.md index d817125af0..649db8acd3 100644 --- a/_cheatsheets/index.md +++ b/_cheatsheets/index.md @@ -42,7 +42,7 @@ languages: [ba, fr, ja, pl, pt-br, zh-cn, th, ru] Good
def f(x: Int) = { x * x }

Bad
def f(x: Int)   { x * x }
- Define function.
Hidden error: without = it’s a Unit-returning procedure; causes havoc. + Define function.
Hidden error: without = it’s a procedure returning Unit; causes havoc. Good
def f(x: Any) = println(x)

Bad
def f(x) = println(x)
From 03fe9c0b2215daef0f8b7ab4f936ec6e0f831d79 Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Thu, 25 Jul 2019 17:51:23 +0200 Subject: [PATCH 10/18] remove postfixOps example --- _cheatsheets/index.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/_cheatsheets/index.md b/_cheatsheets/index.md index 649db8acd3..f294456e7b 100644 --- a/_cheatsheets/index.md +++ b/_cheatsheets/index.md @@ -68,10 +68,6 @@ languages: [ba, fr, ja, pl, pt-br, zh-cn, th, ru]
(1 to 5).map(x => x * x)
Anonymous function: to use an arg twice, have to name it. - - Good
(1 to 5).map(2 *)

Bad
(1 to 5).map(* 2)
- Anonymous function: bound infix method.
Use 2 * _ for sanity’s sake instead.
Will require import scala.language.postfixOps in the future. -
(1 to 5).map { x =>
   val y = x * 2

From 2fb91c3773d3bbd319093d269733b7ea339af3c5 Mon Sep 17 00:00:00 2001
From: Ivan Kuchin 
Date: Thu, 25 Jul 2019 17:53:21 +0200
Subject: [PATCH 11/18] insert space between colon and type in last few places

---
 _cheatsheets/index.md | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/_cheatsheets/index.md b/_cheatsheets/index.md
index f294456e7b..efa42be5a6 100644
--- a/_cheatsheets/index.md
+++ b/_cheatsheets/index.md
@@ -92,18 +92,18 @@ languages: [ba, fr, ja, pl, pt-br, zh-cn, th, ru]
     
       
val zscore =
   (mean: R, sd: R) =>
-    (x:R) =>
+    (x: R) =>
       (x - mean) / sd
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. -
def zscore(mean:R, sd:R)(x:R) =
+      
def zscore(mean: R, sd: R)(x: R) =
   (x - mean) / sd
Currying, sugar syntax. But then: From 63f5d9f6fe44ccfcf5ffe168aff247c2afff1308 Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Thu, 25 Jul 2019 17:54:15 +0200 Subject: [PATCH 12/18] last missed "For comprehension" => "For-comprehension" --- _cheatsheets/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_cheatsheets/index.md b/_cheatsheets/index.md index efa42be5a6..046f30a08b 100644 --- a/_cheatsheets/index.md +++ b/_cheatsheets/index.md @@ -234,7 +234,7 @@ breakable { yield x * 10

same as
xs.filter(_ % 2 == 0).map(_ * 10)
- For comprehension: filter/map. + For-comprehension: filter/map.
for ((x, y) <- xs zip ys)

From 6ee41782d44747a3144318995b3d89ace8bbece6 Mon Sep 17 00:00:00 2001
From: Ivan Kuchin 
Date: Thu, 25 Jul 2019 17:58:16 +0200
Subject: [PATCH 13/18] mark sprintf function with 

---
 _cheatsheets/index.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/_cheatsheets/index.md b/_cheatsheets/index.md
index 046f30a08b..88f996bfdb 100644
--- a/_cheatsheets/index.md
+++ b/_cheatsheets/index.md
@@ -261,7 +261,7 @@ breakable {
   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) {

From 472b9eb18a8622e6e85f07d24a75a95a1e6b3094 Mon Sep 17 00:00:00 2001
From: Ivan Kuchin 
Date: Sat, 27 Jul 2019 19:34:08 +0200
Subject: [PATCH 14/18] revert example for flatten

---
 _cheatsheets/index.md | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/_cheatsheets/index.md b/_cheatsheets/index.md
index 88f996bfdb..cf8d438811 100644
--- a/_cheatsheets/index.md
+++ b/_cheatsheets/index.md
@@ -465,8 +465,8 @@ println(upper.getOrElse(""))
optionOfOption.flatten
same as
optionOfOption match {
-  case Some(x: Option[_]) => x
-  case None               => None
+  case Some(Some(x)) => Some(x)
+  case _             => None
 }
Extract nested option. From 295b1363fb000c0c794e627fbfef13248fe2965a Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Sat, 27 Jul 2019 19:41:00 +0200 Subject: [PATCH 15/18] expand and unify style of collect, exists and forall examples --- _cheatsheets/index.md | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/_cheatsheets/index.md b/_cheatsheets/index.md index cf8d438811..4d332f5172 100644 --- a/_cheatsheets/index.md +++ b/_cheatsheets/index.md @@ -495,9 +495,9 @@ println(upper.getOrElse(""))
}
same as
option match {
-  case Some(x)
-    if f.isDefinedAt(x) => ...
-  case _                => None
+  case Some(x) if f.isDefinedAt(x) => ...
+  case Some(_)                     => None
+  case None                        => None
 }
Apply partial pattern match on optional value. @@ -595,8 +595,9 @@ println(upper.getOrElse(""))
option.exists(f(_))
same as
option match {
-  case Some(x) => f(x)
-  case None    => false
+  case Some(x) if f(x) => true
+  case Some(_)         => false
+  case None            => false
 }
Apply predicate on optional value or false if empty. @@ -604,8 +605,9 @@ println(upper.getOrElse(""))
option.forall(f(_))
same as
option match {
-  case Some(x) => f(x)
-  case None    => true
+  case Some(x) if f(x) => true
+  case Some(_)         => false
+  case None            => true
 }
Apply predicate on optional value or true if empty. From e212ccc482e33153c027ecdecd04f9fd31192654 Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Sat, 27 Jul 2019 19:47:17 +0200 Subject: [PATCH 16/18] remove returning Unit in the end of foreach example --- _cheatsheets/index.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/_cheatsheets/index.md b/_cheatsheets/index.md index 4d332f5172..4e0ad64cdf 100644 --- a/_cheatsheets/index.md +++ b/_cheatsheets/index.md @@ -476,8 +476,7 @@ println(upper.getOrElse(""))
option match {
   case Some(x) => f(x)
   case None    => ()
-}
-()
+} Apply a procedure on optional value. Returns Unit. From f67f26d69c0e0e6d048e57e04237a92ca0c5945f Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Mon, 29 Jul 2019 17:58:33 +0200 Subject: [PATCH 17/18] add note about procedure syntax been deprecated in 2.13 --- _cheatsheets/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_cheatsheets/index.md b/_cheatsheets/index.md index 4e0ad64cdf..a4b2dfdf26 100644 --- a/_cheatsheets/index.md +++ b/_cheatsheets/index.md @@ -42,7 +42,7 @@ languages: [ba, fr, ja, pl, pt-br, zh-cn, th, ru] Good
def f(x: Int) = { x * x }

Bad
def f(x: Int)   { x * x }
- Define function.
Hidden error: without = it’s a procedure returning Unit; causes havoc. + Define function.
Hidden error: without = it’s a procedure returning Unit; causes havoc. Deprecated in Scala 2.13. Good
def f(x: Any) = println(x)

Bad
def f(x) = println(x)
From f1da6cd0a8b6adf0738fc8b3cd969a247be92dc8 Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Tue, 30 Jul 2019 11:27:03 +0200 Subject: [PATCH 18/18] remove note about foreach returning Unit --- _cheatsheets/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_cheatsheets/index.md b/_cheatsheets/index.md index a4b2dfdf26..ce320d1f49 100644 --- a/_cheatsheets/index.md +++ b/_cheatsheets/index.md @@ -477,7 +477,7 @@ println(upper.getOrElse("")) case Some(x) => f(x) case None => () } - Apply a procedure on optional value. Returns Unit. + Apply a procedure on optional value.
option.fold(y)(f(_))