updated
authorChristian Urban <urbanc@in.tum.de>
Sun, 03 Nov 2019 14:42:17 +0000
changeset 308 e86add5a6961
parent 307 3c7ac7836e4f
child 309 b192bc772613
updated
progs/lecture1.scala
slides/Point.scala
slides/slides01.pdf
slides/slides01.tex
--- a/progs/lecture1.scala	Sat Nov 02 21:23:42 2019 +0000
+++ b/progs/lecture1.scala	Sun Nov 03 14:42:17 2019 +0000
@@ -53,6 +53,10 @@
 1 :: 2 :: 3 :: Nil
 List(1, 2, 3) ::: List(4, 5, 6)
 
+// also
+List(1, 2, 3) ++ List(4, 5, 6)
+
+
 // Equality in Scala is structural
 //=================================
 val a = "Dave"
@@ -68,8 +72,8 @@
 
 n1 == n2
 
-// this applies to "concrete" values;
-// you cannot compare functions
+// this applies to "concrete" values...pretty much everything;
+// but for example you cannot compare functions (later)
 
 
 // Printing/Strings
@@ -108,6 +112,8 @@
 "hello".toList.tail
 1.toDouble
 
+1L 
+// a long
 
 // useful list methods
 
@@ -212,15 +218,6 @@
   else n + n
 }
 
-def another_silly(x: Int, y: String) : String = {
-  if (x <= 12) y
-  else x.toString
-}
-
-another_silly(2, "two")
-another_silly(12, "twelf")
-another_silly(20, "twenty")
-
 
 // If-Conditionals
 //=================
@@ -231,7 +228,6 @@
 def fact(n: Int) : Int = 
   if (n == 0) 1 else n * fact(n - 1)
 
-
 fact(5)
 fact(150)
 
@@ -304,7 +300,9 @@
 // the same for files
 Try(Some(Source.fromFile("text.txt").mkString)).getOrElse(None)
 
-// function reading something from files...
+
+
+// how to implement a function for reading something from files...
 
 def get_contents(name: String) : List[String] = 
   Source.fromFile(name).getLines.toList
@@ -403,7 +401,8 @@
 for (p <- lst) yield p._1 + p._2 
 
 
-// general pattern of for-yield
+// general pattern of for-yield 
+// (yield can be several lines)
 
 for (p <- ...) yield {
   // potentially complicated
@@ -443,15 +442,15 @@
 // BTW: a roundabout way of printing out a list, say
 val lst = ('a' to 'm').toList
 
-for (n <- lst) println(n)
+for (i <- (0 until lst.length)) println(lst(i))
 
-for (i <- (0 until lst.length)) println(lst(i))
 
 // Why not just? Why making your life so complicated?
 for (c <- lst) println(c)
 
 // Aside: concurrency 
-// (scala <<..parallel_collections...>> -Yrepl-class-based)
+// scala -Yrepl-class-based -cp scala-parallel-collections_2.13-0.2.0.jar 
+
 for (n <- (1 to 10)) println(n)
 
 import scala.collection.parallel.CollectionConverters._
@@ -467,41 +466,38 @@
   (end - start) / 1.0e9
 }
 
-
 val list = (1 to 1000000).toList
 time_needed(10, for (n <- list) yield n + 42)
 time_needed(10, for (n <- list.par) yield n + 42)
 
-val list = (1 to 1000000).toList.map(BigInt(_))
+// ...but par does not make everything faster
+
 list.sum
 list.par.sum
-list.par.reduce(_ + _)
-list.par.aggregate(BigInt(0))(_ + _, _ + _)
 
 time_needed(10, list.sum)
 time_needed(10, list.par.sum)
-time_needed(10, list.par.reduce(_ + _))
-time_needed(10, list.par.aggregate(BigInt(0))(_ + _, _ + _))
 
 
-// Just for "Fun": Mutable vs Immutable
-//=======================================
+// Mutable vs Immutable
+//======================
 //
+// Remember:
 // - no vars, no ++i, no +=
 // - no mutable data-structures (no Arrays, no ListBuffers)
 
-
+// But what the heck....
 // Q: Count how many elements are in the intersections of two sets?
 // A; IMPROPER WAY (mutable counter)
 
 def count_intersection(A: Set[Int], B: Set[Int]) : Int = {
   var count = 0
-  for (x <- A; if (B contains x)) count += 1 
+  for (x <- A.par; if (B contains x)) count += 1 
   count
 }
 
-val A = (1 to 1000).toSet
-val B = (1 to 1000 by 4).toSet
+val A = (0 to 999).toSet
+val B = (0 to 999 by 4).toSet
 
 count_intersection(A, B)
 
@@ -515,7 +511,7 @@
 count_intersection2(A, B)
 
 
-//another example
+//another bad example
 def test() = {
   var cnt = 0
   for(i <- (1 to 1000000).par) cnt += 1
@@ -524,20 +520,6 @@
 
 test()
 
-//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
-}
-
-val A = (1 to 1000000).toSet
-val B = (1 to 1000000 by 4).toSet
-
-time_needed(10, count_intersection(A, B))
-time_needed(10, count_intersection2(A, B))
-
 
 // Further Information
 //=====================
--- a/slides/Point.scala	Sat Nov 02 21:23:42 2019 +0000
+++ b/slides/Point.scala	Sun Nov 03 14:42:17 2019 +0000
@@ -1,1 +1,1 @@
-class Point(val x: Int, val y: Int)
+case class Point(val x: Int, val y: Int)
Binary file slides/slides01.pdf has changed
--- a/slides/slides01.tex	Sat Nov 02 21:23:42 2019 +0000
+++ b/slides/slides01.tex	Sun Nov 03 14:42:17 2019 +0000
@@ -1,3 +1,4 @@
+% !TEX program = xelatex
 \documentclass[dvipsnames,14pt,t,xelatex]{beamer}
 \usepackage{../slides}
 \usepackage{../graphics}
@@ -38,8 +39,8 @@
     Email:  & christian.urban at kcl.ac.uk\\
     Office: & N\liningnums{7.07} (North Wing, Bush House)\\
     Slides \& Code: & KEATS\medskip\\
-    Office Hours: &  Mondays 12:00 -- 14:00\\
-           & \alert{except next week: Tuesday}
+    Office Hours: &  Thursdays 12:00 -- 14:00\\
+    Additionally: & (for Scala) Tuesdays 10:45 -- 11:45\\ 
   \end{tabular}
   \end{center}
 
@@ -82,11 +83,11 @@
 
 
 \begin{textblock}{6}(2,12)
-\begin{bubble}[9cm]
+\begin{bubble}[9.4cm]
   \small
-  developed since 2004 by Martin Odersky\\
+  developed since 2004 by Martin Odersky
   (he was behind Generic Java which was included in Java 5
-  \ldots I am using it maybe since 2008?)
+  \ldots I am using Scala since maybe 2008?)
 \end{bubble}
 \end{textblock}
 
@@ -116,27 +117,27 @@
   \textcolor{gray}{(also JavaScript, native X86 in the works)}\medskip
 \item integrates seamlessly with Java\medskip
 \item combines \underline{\bf functional} and {\bf object-oriented} programming\bigskip
-\item it is a bit on the ``mathematical'' side\\
+\item it is a bit on the ``theory'' / ``mathematical'' side\\
   \textcolor{gray}{(no pointers, no \texttt{null}, but expressions)}
   
 \item often one can write very concise and elegant code
 \end{itemize}\bigskip\medskip  
 
-\small
-alternatives:\\
-Elm, Haskell, Ocaml, F$\sharp$, Erlang, ML, Lisp (Racket), \ldots
+%\small
+%alternatives:\\
+%Elm, Haskell, Ocaml, F$\sharp$, Erlang, ML, Lisp (Racket), \ldots
 
 \end{frame}
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\begin{frame}[c]
+\begin{frame}[t]
 \frametitle{Java vs Scala}
 
-{\lstset{language=java}\fontsize{12}{12}\selectfont
+{\lstset{language=java,numbers=right}\fontsize{12}{12}\selectfont
 \texttt{\lstinputlisting{Point.java}}}
 
-\rule{11cm}{0.3mm}\\[-2mm]
+\rule{11cm}{0.3mm}\\[-3mm]
 
 {\lstset{language=scala}\fontsize{12}{12}\selectfont
 \texttt{\lstinputlisting{Point.scala}}}
@@ -145,7 +146,7 @@
 \textbf{\large Java}
 \end{textblock}
 
-\begin{textblock}{6}(13,13.6)
+\begin{textblock}{6}(13,14.3)
 \textbf{\large Scala}
 \end{textblock}  
 
@@ -169,6 +170,7 @@
 \frametitle{First Steps: Scala Tools}
 
 \begin{itemize}
+\item contains a REPL  
 \item I use VS Code and a Scala extension (M'place)
 \begin{center}  
 \includegraphics[scale=0.10]{../pics/vscode.png}\\[-10mm]\mbox{}
@@ -364,7 +366,7 @@
 \only<3>{
   \begin{textblock}{14.2}(1,13.5)
     In FP: Once a variable is created, it is assigned a value and then
-    never changed again $\Rightarrow$ no synchronisation\smallskip\\
+    never changed again $\Rightarrow$ no synchronisation needed\smallskip\\
     %%\small\textcolor{gray}{(Andrew's second favourite feature of C++)}
   \end{textblock}}
 
@@ -462,15 +464,15 @@
 
 \begin{itemize}
 \item Sorry, I might have been a bit wordy:\\
-  CW description is 7 pages, but
-  I only needed \mbox{< 100} loc for \emph{all} the CW6.\bigskip
+  Part 6 of CW description is 7 pages, but
+  I only needed \mbox{< 100} loc for \emph{all} Part 6.\bigskip
 
-\item there is email feedback when pushing code to github\medskip
+\item there is feedback when pushing code to github\medskip
 
 \item there are \texttt{jar}-files you can use to test my implementation\bigskip
   
-\item we want you to learn FP: \alert{\bf no vars}, no mutable
-  data-structures, e.g.~no \texttt{Arrays}, no \texttt{ListBuffer}
+\item we want you to learn FP!\\ \alert{\bf no vars}, no mutable
+  data-structures\\ e.g.~no \texttt{Arrays}, no \texttt{ListBuffer}
 \end{itemize}
 \end{frame}
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -489,6 +491,7 @@
   \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
     val old_list = List(1, 2, 3, 5)
     val new_list = 0 :: old_list
+                // -> List(0, 1, 2, 3, 4, 5)
   \end{lstlisting}}  
   
 \item You do not have to be defensive about who can access the data.
@@ -504,7 +507,7 @@
 \begin{frame}[t]
 \frametitle{Email: Hate 'val'}
 
-\mbox{}\\[-25mm]\mbox{}
+\mbox{}\\[-23mm]\mbox{}
 
 \begin{center}
   \begin{bubble}[10.5cm]
@@ -686,8 +689,8 @@
 \end{center}
 
 \begin{center}
-  My Office Hours: Mondays 12 -- 14\\
-  except next week: Tuesday 12 -- 14
+  My Office Hours: Thursdays 12 -- 14\\
+  And specifically for Scala: Tuesday 10:45 -- 11:45
 \end{center}
 \end{frame}
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%