moved scala => _scala

pull/2/head
Igor Chubin 7 years ago
parent bdd633799c
commit 89a1d33993

@ -0,0 +1,4 @@
# source:
#
# http://docs.scala-lang.org/cheatsheets/

@ -0,0 +1,54 @@
// conditional
if (check) happy else sad
// conditional sugar
if (check) happy // same as
if (check) happy else ()
// while loop
while (x < 5) { println(x); x += 1}
// do while loop.
do { println(x); x += 1} while (x < 5)
// break
// slides: https://www.slideshare.net/Odersky/fosdem-2009-1013261/21
import scala.util.control.Breaks._
breakable {
for (x <- xs) {
if (Math.random < 0.1) break
}
}
// for comprehension: filter/map
for (x <- xs if x%2 == 0) yield x*10
// same as
xs.filter(_%2 == 0).map(_*10)
// for comprehension: destructuring bind
for ((x,y) <- xs zip ys) yield x*y
// same as
(xs zip ys) map { case (x,y) => x*y }
// for comprehension: cross product
for (x <- xs; y <- ys) yield x*y
// same as
xs flatMap {x => ys map {y => x*y}}
// for comprehension: imperative-ish sprintf-style
// http://docs.oracle.com/javase/6/docs/api/java/util/Formatter.html#syntax
for (x <- xs; y <- ys) {
println("%d/%d = %.1f".format(x, y, x/y.toFloat))
}
// for comprehension: iterate including the upper bound
for (i <- 1 to 5) {
println(i)
}
// for comprehension: iterate omitting the upper bound
for (i <- 1 until 5) {
println(i)
}

@ -0,0 +1,29 @@
// tuple literal. (Tuple3)
(1,2,3)
// destructuring bind: tuple unpacking via pattern matching.
var (x,y,z) = (1,2,3)
// hidden error: each assigned to the entire tuple.
// BAD
var x,y,z = (1,2,3)
// list (immutable).
var xs = List(1,2,3)
// paren indexing. (slides)
// more on it: https://www.slideshare.net/Odersky/fosdem-2009-1013261/27
xs(2)
// cons.
1 :: List(2,3)
// range sugar.
1 to 5 // same as
1 until 6
1 to 10 by 2
// sole member of the Unit type (like C/Java void).
() //(empty parens)

@ -0,0 +1,71 @@
// define function
// GOOD
def f(x: Int) = { x*x }
// BAD
// hidden error: without = its a Unit-returning procedure; causes havoc
def f(x: Int) { x*x }
// define function
// GOOD
def f(x: Any) = println(x)
// BAD
// syntax error: need types for every arg.
def f(x) = println(x)
// type alias
type R = Double
// call-by-value
def f(x: R)
// call-by-name (lazy parameters)
def f(x: => R)
// anonymous function
(x:R) => x*x
// anonymous function: underscore is positionally matched arg.
(1 to 5).map(_*2) // vs.
(1 to 5).reduceLeft( _+_ )
// anonymous function: to use an arg twice, have to name it.
(1 to 5).map( x => x*x )
// anonymous function: bound infix method. Use 2*_ for sanitys sake instead.
// GOOD
(1 to 5).map(2*)
// BAD
(1 to 5).map(*2)
// anonymous function: block style returns last expression.
(1 to 5).map { x => val y=x*2; println(y); y }
// anonymous functions: pipeline style. (or parens too).
(1 to 5) filter {_%2 == 0} map {_*2}
// anonymous functions: to pass in multiple blocks, need outer parens.
def compose(g:R=>R, h:R=>R) = (x:R) => g(h(x))
val f = compose({_*2}, {_-1})
// currying, obvious syntax.
val zscore = (mean:R, sd:R) => (x:R) => (x-mean)/sd
// currying, obvious syntax
def zscore(mean:R, sd:R) = (x:R) => (x-mean)/sd
// currying, sugar syntax. but then:
def zscore(mean:R, sd:R)(x:R) = (x-mean)/sd
// need trailing underscore to get the partial, only for the sugar version.
val normer = zscore(7, 0.4) _
// generic type.
def mapmake[T](g:T=>T)(seq: List[T]) = seq.map(g)
// infix sugar.
5.+(3); 5 + 3
(1 to 5) map (_*2)
// varargs
def sum(args: Int*) = args.reduceLeft(_+_)

@ -0,0 +1,76 @@
// constructor params - private
class C(x: R) // same as
class C(private val x: R)
var c = new C(4)
// constructor params - public
class C(val x: R)
var c = new C(4)
c.x
class C(var x: R) {
// constructor is class body
assert(x > 0, "positive please")
//
// declare a public member
var y = x
//
// declare a gettable but not settable member
val readonly = 5
//
// declare a private member
private var secret = 1
//
// alternative constructor
def this = this(42)
}
// anonymous class
new{ ... }
// define an abstract class. (non-createable)
abstract class D { ... }
// define an inherited class.
class C extends D { ... }
class D(var x: R)
// inheritance and constructor params. (wishlist: automatically pass-up params by default)
class C(x: R) extends D(x)
// define a singleton. (module-like)
object O extends D { ... }
// traits.
trait T { ... }
// interfaces-with-implementation. no constructor params
// mixin-able: http://docs.scala-lang.org/tutorials/tour/mixin-class-composition.html
class C extends T { ... }
class C extends D with T { ... }
// multiple traits.
trait T1; trait T2
class C extends T1 with T2
class C extends D with T1 with T2
// must declare method overrides.
class C extends D { override def f = ...}
// create object.
new java.io.File("f")
new List[Int] // BAD type error: abstract type
List(1,2,3) // GOOD instead, convention: callable factory shadowing the type
// class literal.
classOf[String]
// type check (runtime)
x.isInstanceOf[String]
// type cast (runtime)
x.asInstanceOf[String]
// ascription (compile time)
x: String

@ -0,0 +1,18 @@
// wildcard import.
import scala.collection._
// selective import.
import scala.collection.Vector
import scala.collection.{Vector, Sequence}
// renaming import.
import scala.collection.{Vector => Vec28}
// import all from java.util except Date.
import java.util.{Date => _, _}
// declare a package.
package pkg // at start of file
package pkg { ... }

@ -0,0 +1,29 @@
// use case in function args for pattern matching.
(xs zip ys) map { case (x,y) => x*y } // GOOD
(xs zip ys) map( (x,y) => x*y ) // BAD
// "v42" is interpreted as a name matching any Int value, and "42" is printed.
// BAD
val v42 = 42
Some(3) match {
case Some(v42) => println("42")
case _ => println("Not 42")
}
// "`v42`" with backticks is interpreted as the existing val v42, and “Not 42” is printed.
// GOOD
val v42 = 42
Some(3) match {
case Some(`v42`) => 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.
// GOOD
val UppercaseVal = 42
Some(3) match {
case Some(UppercaseVal) => println("42")
case _ => println("Not 42")
}

@ -0,0 +1,11 @@
// variables
var x = 5
// constant
// GOOD
val x = 5
// BAD
x=6
// explicit type
var x: Double = 5

@ -0,0 +1,9 @@
// to compile: scalac HelloWorld.scala
// or scalac -d classes HelloWorld.scala
// to execute: scala HelloWorld
// or scala -cp classes HelloWorld
object HelloWorld {
def main(args: Array[String]) {
println("Hello, world!")
}
}
Loading…
Cancel
Save