From 65ecb85becc5873ad934bf373b25a621dcb70f82 Mon Sep 17 00:00:00 2001 From: Igor Chubin Date: Sun, 28 May 2017 21:19:10 +0000 Subject: [PATCH] moved scala/ to _scala/ --- sheets/scala/:main | 4 -- sheets/scala/ControlStructures | 54 ------------------------ sheets/scala/DataStructures | 29 ------------- sheets/scala/Functions | 71 ------------------------------- sheets/scala/ObjectOrientation | 76 ---------------------------------- sheets/scala/Packages | 18 -------- sheets/scala/PatternMatching | 29 ------------- sheets/scala/Variables | 11 ----- sheets/scala/hello | 9 ---- 9 files changed, 301 deletions(-) delete mode 100644 sheets/scala/:main delete mode 100644 sheets/scala/ControlStructures delete mode 100644 sheets/scala/DataStructures delete mode 100644 sheets/scala/Functions delete mode 100644 sheets/scala/ObjectOrientation delete mode 100644 sheets/scala/Packages delete mode 100644 sheets/scala/PatternMatching delete mode 100644 sheets/scala/Variables delete mode 100644 sheets/scala/hello diff --git a/sheets/scala/:main b/sheets/scala/:main deleted file mode 100644 index 4c2df01..0000000 --- a/sheets/scala/:main +++ /dev/null @@ -1,4 +0,0 @@ -# source: -# -# http://docs.scala-lang.org/cheatsheets/ - diff --git a/sheets/scala/ControlStructures b/sheets/scala/ControlStructures deleted file mode 100644 index 67f81d5..0000000 --- a/sheets/scala/ControlStructures +++ /dev/null @@ -1,54 +0,0 @@ -// 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) -} - - diff --git a/sheets/scala/DataStructures b/sheets/scala/DataStructures deleted file mode 100644 index 36957ea..0000000 --- a/sheets/scala/DataStructures +++ /dev/null @@ -1,29 +0,0 @@ -// 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) - - diff --git a/sheets/scala/Functions b/sheets/scala/Functions deleted file mode 100644 index 1416026..0000000 --- a/sheets/scala/Functions +++ /dev/null @@ -1,71 +0,0 @@ -// define function -// GOOD -def f(x: Int) = { x*x } -// BAD -// hidden error: without = it’s 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 sanity’s 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(_+_) - - diff --git a/sheets/scala/ObjectOrientation b/sheets/scala/ObjectOrientation deleted file mode 100644 index bd9939e..0000000 --- a/sheets/scala/ObjectOrientation +++ /dev/null @@ -1,76 +0,0 @@ -// 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 diff --git a/sheets/scala/Packages b/sheets/scala/Packages deleted file mode 100644 index 2594621..0000000 --- a/sheets/scala/Packages +++ /dev/null @@ -1,18 +0,0 @@ -// 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 { ... } - - diff --git a/sheets/scala/PatternMatching b/sheets/scala/PatternMatching deleted file mode 100644 index 7153248..0000000 --- a/sheets/scala/PatternMatching +++ /dev/null @@ -1,29 +0,0 @@ -// 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") -} - diff --git a/sheets/scala/Variables b/sheets/scala/Variables deleted file mode 100644 index 509257e..0000000 --- a/sheets/scala/Variables +++ /dev/null @@ -1,11 +0,0 @@ -// variables -var x = 5 - -// constant -// GOOD -val x = 5 -// BAD -x=6 - -// explicit type -var x: Double = 5 diff --git a/sheets/scala/hello b/sheets/scala/hello deleted file mode 100644 index 19373e6..0000000 --- a/sheets/scala/hello +++ /dev/null @@ -1,9 +0,0 @@ -// 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!") - } -}