Binary file handouts/pep-ho.pdf has changed
--- a/handouts/pep-ho.tex Fri Jan 19 14:09:08 2018 +0000
+++ b/handouts/pep-ho.tex Fri Feb 23 22:26:42 2018 +0000
@@ -6,10 +6,65 @@
%cheat sheet
%http://worldline.github.io/scala-cheatsheet/
+% case class, apply, unappy
+% see https://medium.com/@thejasbabu/scala-pattern-matching-9c9e73ba9a8a
+
\begin{document}
\section*{A Crash-Course in Scala}
+
+Scala is a programming language that combines functional and
+object-oriented programming-styles. It has received quite a bit of
+attention in the last five or so years. One reason for this attention
+is that, like the Java programming language, Scala compiles to the
+Java Virtual Machine (JVM) and therefore Scala programs can run under
+MacOSX, Linux and Windows.\footnote{There are also experimental
+ backends for Android and JavaScript; and also work is under way to
+ have a native compiler, see
+ \url{https://github.com/scala-native/scala-native}.} Unlike Java,
+however, Scala often allows programmers to write very concise and
+elegant code. Some therefore say: ``Scala is the better
+Java''.\footnote{\url{https://www.slideshare.net/maximnovak/joy-of-scala}}
+Also a number of companies (the Guardian, Twitter, Coursera,
+FourSquare, LinkedIn to name a few) either use Scala exclusively in
+production code, or at least to some substantial degree. Scala seems
+also to be useful in job-interviews (in Data Science) according to
+this anecdotal report
+
+\begin{quote}\small
+\url{https://techcrunch.com/2016/06/14/scala-is-the-new-golden-child/}
+\end{quote}
+
+\noindent
+The official Scala compiler can be downloaded from
+
+\begin{quote}
+\url{http://www.scala-lang.org}
+\end{quote}
+
+\noindent
+A ready-made bundle with the Eclipse IDE is at
+
+\begin{quote}
+\url{http://scala-ide.org/download/sdk.html}
+\end{quote}
+
+\noindent
+When developing Scala programs, I personally prefer to use Emacs
+or Sublime as my environment, since they provide an easy access
+to the Scala REPL (see below). But it is also possible to work
+completely on the command line and also with heavy-duty IDEs
+like Eclipse of IntelliJ. There is even an online editor and
+environment for developing Scala programs called ScalaFiddle
+
+\begin{quote}
+\url{https://scalafiddle.io}
+\end{quote}
+
+
+
+
\subsection*{The Very Basics}
One advantage of Scala over Java is that it includes an interpreter (a
@@ -17,6 +72,7 @@
\underline{R}ead-\underline{E}val-\underline{P}rint-\underline{L}oop)
with which you can run and test small code-snippets without the need
of a compiler. This helps a lot with interactively developing
+programs. This is really the preferred way of writing small Scala
programs. Once you installed Scala, you can start the interpreter by
typing on the command line:
--- a/handouts/scala-ho.tex Fri Jan 19 14:09:08 2018 +0000
+++ b/handouts/scala-ho.tex Fri Feb 23 22:26:42 2018 +0000
@@ -15,21 +15,23 @@
\section*{A Crash-Course on Scala}
Scala is a programming language that combines functional and
-object-oriented programming-styles. It has received quite a
-bit of attention in the last five years or so. One reason for
-this attention is that, like the Java programming language,
-Scala compiles to the Java Virtual Machine (JVM) and therefore
-Scala programs can run under MacOSX, Linux and
-Windows.\footnote{There are also experimental backends for
-Android and JavaScript; and also work is under way to have a
-native compiler, see \url{https://github.com/scala-native/scala-native}.} Unlike Java, however, Scala often
-allows programmers to write very concise and elegant code.
-Some therefore say: Scala is the much better Java. A number of
-companies, The Guardian, Twitter, Coursera, FourSquare,
-LinkedIn to name a few, either use Scala exclusively in
-production code, or at least to some substantial degree. It
-also seems to be useful in job-interviews (in Data Science)
-according to this annectotical report
+object-oriented programming-styles. It has received quite a bit of
+attention in the last five or so years. One reason for this attention
+is that, like the Java programming language, Scala compiles to the
+Java Virtual Machine (JVM) and therefore Scala programs can run under
+MacOSX, Linux and Windows.\footnote{There are also experimental
+ backends for Android and JavaScript; and also work is under way to
+ have a native compiler, see
+ \url{https://github.com/scala-native/scala-native}.} Unlike Java,
+however, Scala often allows programmers to write very concise and
+elegant code. Some therefore say: ``Scala is the better
+Java''.\footnote{\url{https://www.slideshare.net/maximnovak/joy-of-scala}}
+Also a number of companies (the Guardian, Twitter, Coursera,
+FourSquare, LinkedIn to name a few) either use Scala exclusively in
+production code, or at least to some substantial degree. Scala seems
+also to be useful in job-interviews (in Data Science) according to
+this anecdotal report
+
\begin{quote}
\url{https://techcrunch.com/2016/06/14/scala-is-the-new-golden-child/}
@@ -50,6 +52,18 @@
\url{http://scala-ide.org/download/sdk.html}
\end{quote}
+\noindent
+When developing Scala programs, I personally prefer to use Emacs
+or Sublime as my environment, since they provide an easy access
+to the Scala REPL (see below). But it is also possible to work
+completely on the command line and also with heavy-duty IDEs
+like Eclipse of IntelliJ. There is even an online editor and
+environment for developing Scala programs called ScalaFiddle
+
+\begin{quote}
+\url{https://scalafiddle.io}
+\end{quote}
+
Why do I use Scala in the AFL module? Actually, you can do
\emph{any} part of the coursework in \emph{any} programming
language you like. I use Scala for showing you code during the
--- a/marking1/drumb_test2.scala Fri Jan 19 14:09:08 2018 +0000
+++ b/marking1/drumb_test2.scala Fri Feb 23 22:26:42 2018 +0000
@@ -1,3 +1,5 @@
+
+// paste in the actual values
val urban_prices1 = CW6c.get_prices(List("BIDU"), 2004 to 2008)
val urban_prices2 = CW6c.get_prices(List("GOOG", "AAPL"), 2010 to 2012)
--- a/marking3/bf1c_test.scala Fri Jan 19 14:09:08 2018 +0000
+++ b/marking3/bf1c_test.scala Fri Feb 23 22:26:42 2018 +0000
@@ -4,6 +4,14 @@
assert(start("[-]", Map(0 -> 100)) == Map(0 -> 0))
assert(start("[->+<]", Map(0 -> 10)) == Map(0 -> 0, 1 -> 10))
assert(start("[>>+>>+<<<<-]", Map(0 -> 42)) == Map(0 -> 0, 2 -> 42, 4 -> 42))
-assert(start("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]
- <-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.""", Map()) == Map(0 -> 0, 5 -> 33, 1 -> 0, 6 -> 10, 2 -> 72, 3 -> 100, 4 -> 87))
+
+val hello = """++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.
+ +++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."""
+assert(start(hello, Map()) == Map(0 -> 0, 5 -> 33, 1 -> 0, 6 -> 10, 2 -> 72, 3 -> 100, 4 -> 87))
+
assert(start("+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]", Map()) == Map(0 -> 0, 1 -> 58, 2 -> 32))
+
+val hello2 = """++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.!!!!!!!!!"""
+
+assert(start(hello2, Map()) == Map(0 -> 0, 5 -> 33, 1 -> 0, 6 -> 10, 2 -> 72, 3 -> 100, 4 -> 87))
+
--- a/progs/lecture1.scala Fri Jan 19 14:09:08 2018 +0000
+++ b/progs/lecture1.scala Fri Feb 23 22:26:42 2018 +0000
@@ -302,9 +302,32 @@
time_needed(10, for (n <- list.par) yield n + 42)
-
+// mutable vs immutable factorial
+def fact_mu(n: Long): Long = {
+ var result : Long = 1
+ var i = 1
+ while (i <= n) {
+ result = result * i
+ i = i + 1
+ }
+ result
+}
+def fact_im(num: Long): Long = {
+ if (num == 1) num else
+ num * fact_im(num - 1)
+}
+def test() = {
+ for (i <- (0 to 10).par) yield {
+ val l1 = for (n <- (List.fill(100000)(20 to 21)).flatten.par) yield fact_mu(n)
+ val l2 = for (n <- (List.fill(100000)(20 to 21)).flatten.par) yield fact_im(n)
+ l1.sum - l2.sum
+ }
+}
+
+test().sum
+println(l1.sum - l2.sum)
// Webpages
//==========
@@ -456,3 +479,67 @@
//
+
+
+
+
+
+
+
+// advantage of case classes
+//
+case class Participant(name: String, score: Int, active: Boolean)
+
+val ps = Seq(Participant("Jack", 34, true),
+ Participant("Tom", 51, true),
+ Participant("Bob", 90, false))
+
+ps.filter(_.score < 50).
+ filter(_.active).
+ map(_.copy(active = false))
+
+
+
+// another example why state is bad...does not work yet
+
+// for measuring time
+def time_needed[T](n: Int, code: => T) = {
+ val start = System.nanoTime()
+ for (i <- (0 to n)) code
+ val end = System.nanoTime()
+ (end - start) / 1.0e9
+}
+
+def santa_state(plan: List[Char]) : Int = {
+
+ var floor = 0
+
+ for (i <- plan.par) {
+ if (i == '(') {
+ floor += 1
+ } else {
+ floor -= 1
+ }
+ }
+
+ floor
+}
+
+def i(c: Char) = if (c == '(') 1 else -1
+
+def santa_imutable(plan: List[Char]) : Int =
+ plan.par.map(i(_)).reduce(_ + _)
+
+santa_state("(((((()))))".toList)
+santa_imutable("(((((()))))".toList)
+
+def randomString(length: Int) =
+ List.fill(length)((40 + scala.util.Random.nextInt(2)).toChar)
+
+
+santa_state(randomString(100))
+
+val large_string = randomString(3000000)
+
+time_needed(10, santa_state(large_string))
+time_needed(10, santa_imutable(large_string))
--- a/progs/lecture3.scala Fri Jan 19 14:09:08 2018 +0000
+++ b/progs/lecture3.scala Fri Feb 23 22:26:42 2018 +0000
@@ -509,3 +509,20 @@
// I like best about Scala that it lets me often write
// concise, readable code.
+
+
+// You can define your own while loop
+
+
+def my_while(condition: => Boolean)(block: => Unit): Unit =
+ if (condition) { block ; my_while(condition) { block } } else { }
+
+
+var x = 10
+my_while (x > 0) {
+ println(s"$x") ; x = x - 1
+}
+
+
+`symbol
+`symbol`