--- a/progs/lecture2.scala Mon Nov 11 13:24:12 2019 +0000
+++ b/progs/lecture2.scala Mon Nov 11 14:04:22 2019 +0000
@@ -1,6 +1,38 @@
// Scala Lecture 2
//=================
+// For-Comprehensions Again
+//==========================
+
+// the first produces a result, while the second does not
+for (n <- List(1, 2, 3, 4, 5)) yield n * n
+
+
+for (n <- List(1, 2, 3, 4, 5)) println(n)
+
+
+// String Interpolations
+//=======================
+
+val n = 3
+println("The square of " + n + " is " + square(n) + ".")
+
+println(s"The square of ${n} is ${square(n)}.")
+
+
+// helpful for debugging purposes
+//
+// "The most effective debugging tool is still careful thought,
+// coupled with judiciously placed print statements."
+// — Brian W. Kernighan, in Unix for Beginners (1979)
+
+
+def gcd_db(a: Int, b: Int) : Int = {
+ println(s"Function called with ${a} and ${b}.")
+ if (b == 0) a else gcd_db(b, a % b)
+}
+
+gcd_db(48, 18)
// The Option Type
@@ -61,46 +93,54 @@
-// For-Comprehensions Again
-//==========================
+// operations on options
-// the first produces a result, while the second does not
-for (n <- List(1, 2, 3, 4, 5)) yield n * n
+val lst = List(None, Some(1), Some(2), None, Some(3))
-
-for (n <- List(1, 2, 3, 4, 5)) println(n)
+lst.flatten
-
-// String Interpolations
-//=======================
+Some(1).get
+None.get
-val n = 3
-println("The square of " + n + " is " + square(n) + ".")
-
-println(s"The square of ${n} is ${square(n)}.")
+Some(1).isDefined
+None.isDefined
-// helpful for debugging purposes
-//
-// "The most effective debugging tool is still careful thought,
-// coupled with judiciously placed print statements."
-// — Brian W. Kernighan, in Unix for Beginners (1979)
+val ps = List((3, 0), (3, 2), (4, 2), (2, 0), (1, 0), (1, 1))
+
+// division where possible
+
+for ((x, y) <- ps) yield {
+ if (y == 0) None else Some(x / y)
+}
+
+// getOrElse is for setting a default value
+
+val lst = List(None, Some(1), Some(2), None, Some(3))
+
+for (x <- lst) yield x.getOrElse(0)
+
+
-def gcd_db(a: Int, b: Int) : Int = {
- println(s"Function called with ${a} and ${b}.")
- if (b == 0) a else gcd_db(b, a % b)
-}
-
-gcd_db(48, 18)
+// This may not look any better than working with null in Java, but to
+// see the value, you have to put yourself in the shoes of the
+// consumer of the get_me_an_int function, and imagine you didn't
+// write that function.
+//
+// In Java, if you didn't write this function, you'd have to depend on
+// the Javadoc of the get_me_an_int. If you didn't look at the Javadoc,
+// you might not know that get_me_an_int could return a null, and your
+// code could potentially throw a NullPointerException.
-// Asserts/Testing
-//=================
+
+// even Scala is not immune to problems like this:
-assert(gcd(48, 18) == 6)
+List(5,6,7,8,9).indexOf(7)
+List(5,6,7,8,9).indexOf(10)
+List(5,6,7,8,9)(-1)
-assert(gcd(48, 18) == 5, "The gcd test failed")
@@ -148,7 +188,6 @@
def square(x: Int): Int = x * x
-
val lst = (1 to 10).toList
lst.map(x => (double(x), square(x)))
@@ -217,6 +256,45 @@
+// if you like verbosity, you can full-specify the literal.
+// Don't go telling that to people, though
+(1 to 100).filter((x: Int) => x % 2 == 0).sum
+
+// As x is known to be an Int anyway, you can omit that part
+(1 to 100).filter(x => x % 2 == 0).sum
+
+// As each parameter (only x in this case) is passed only once
+// you can use the wizardy placeholder syntax
+(1 to 100).filter(_ % 2 == 0).sum
+
+// But if you want to re-use your literal, you can also put it in a value
+// In this case, explicit types are required because there's nothing to infer from
+val isEven = (x: Int) => x % 2 == 0
+(1 to 100).filter(isEven).sum
+
+
+
+// Option Type again
+//===================
+
+// a function that turns strings into numbers (similar to .toInt)
+Integer.parseInt("12u34")
+
+
+def get_me_an_int(s: String) : Option[Int] =
+ Try(Some(Integer.parseInt(s))).getOrElse(None)
+
+val lst = List("12345", "foo", "5432", "bar", "x21", "456")
+for (x <- lst) yield get_me_an_int(x)
+
+// summing up all the numbers
+
+lst.map(get_me_an_int).flatten.sum
+lst.map(get_me_an_int).flatten.sum
+
+lst.flatMap(get_me_an_int).sum
+
+
// Map type (upper-case)
@@ -259,103 +337,6 @@
-// Option type (again)
-//=====================
-
-// remember, in Java if something unusually happens,
-// you return null;
-//
-// in Scala you use Option
-// - if the value is present, you use Some(value)
-// - if no value is present, you use None
-
-
-List(7,2,3,4,5,6).find(_ < 4)
-List(5,6,7,8,9).find(_ < 4)
-
-// operations on options
-
-val lst = List(None, Some(1), Some(2), None, Some(3))
-
-lst.flatten
-
-Some(1).get
-None.get
-
-Some(1).isDefined
-None.isDefined
-
-
-None.isDefined
-
-val ps = List((3, 0), (3, 2), (4, 2), (2, 0), (1, 0), (1, 1))
-
-for ((x, y) <- ps) yield {
- if (y == 0) None else Some(x / y)
-}
-
-// getOrElse is for setting a default value
-
-val lst = List(None, Some(1), Some(2), None, Some(3))
-
-for (x <- lst) yield x.getOrElse(0)
-
-
-
-
-// error handling with Option (no exceptions)
-//
-// Try(something).getOrElse(what_to_do_in_an_exception)
-//
-import scala.util._
-import io.Source
-
-
-Source.fromURL("""http://www.inf.ucl.ac.uk/staff/urbanc/""").mkString
-
-Try(Source.fromURL("""http://www.inf.kcl.ac.uk/staff/urbanc/""").mkString).getOrElse("")
-
-Try(Some(Source.fromURL("""http://www.inf.kcl.ac.uk/staff/urbanc/""").mkString)).getOrElse(None)
-
-
-// a function that turns strings into numbers (similar to .toInt)
-Integer.parseInt("12u34")
-
-
-def get_me_an_int(s: String) : Option[Int] =
- Try(Some(Integer.parseInt(s))).getOrElse(None)
-
-val lst = List("12345", "foo", "5432", "bar", "x21", "456")
-for (x <- lst) yield get_me_an_int(x)
-
-// summing up all the numbers
-
-lst.map(get_me_an_int).flatten.sum
-lst.map(get_me_an_int).flatten.sum
-
-
-lst.flatMap(get_me_an_int).map(_.toString)
-
-
-// This may not look any better than working with null in Java, but to
-// see the value, you have to put yourself in the shoes of the
-// consumer of the get_me_an_int function, and imagine you didn't
-// write that function.
-//
-// In Java, if you didn't write this function, you'd have to depend on
-// the Javadoc of the get_me_an_int. If you didn't look at the Javadoc,
-// you might not know that get_me_an_int could return a null, and your
-// code could potentially throw a NullPointerException.
-
-
-
-// even Scala is not immune to problems like this:
-
-List(5,6,7,8,9).indexOf(7)
-List(5,6,7,8,9).indexOf(10)
-List(5,6,7,8,9)(-1)
-
-
// Pattern Matching
//==================
@@ -807,18 +788,4 @@
-// if you like verbosity, you can full-specify the literal.
-// Don't go telling that to people, though
-(1 to 100).filter((x: Int) => x % 2 == 0).sum
-// As x is known to be an Int anyway, you can omit that part
-(1 to 100).filter(x => x % 2 == 0).sum
-
-// As each parameter (only x in this case) is passed only once
-// you can use the wizardy placeholder syntax
-(1 to 100).filter(_ % 2 == 0).sum
-
-// But if you want to re-use your literal, you can also put it in a value
-// In this case, explicit types are required because there's nothing to infer from
-val isEven: (x: Int) => x % 2 == 0
-(1 to 100).filter(isEven).sum
--- a/slides/slides02.tex Mon Nov 11 13:24:12 2019 +0000
+++ b/slides/slides02.tex Mon Nov 11 14:04:22 2019 +0000
@@ -107,98 +107,191 @@
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c]
-\frametitle{Assignments}
-
-Don't change anything with the templates!\bigskip
-
-Avoid at all costs:
+ \frametitle{Discussion Forum}
+
+ \large
+ ``Since we cant use \code{var}s I was wondering if we could use a stack?''
+ \bigskip\bigskip\bigskip\bigskip
-\begin{itemize}
-\item \texttt{var}
-\item \texttt{return}
-\item \texttt{ListBuffer}
-\item \texttt{mutable}
-\item \texttt{.par}
-\end{itemize}\pause\bigskip
-
-
-\mbox{}\hfill\textit{``Scala --- \underline{S}lowly \underline{c}ompiled
-\underline{a}cademic \underline{la}nguage''}\smallskip\\
-\mbox{}\hfill\textit{ --- a joke(?) found on Twitter}\bigskip
+ \small
+ My \pcode{collatz} and \pcode{collatz_max} functions are 4 loc each.
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[t]
-\frametitle{Email: Hate 'val'}
+ \frametitle{Email: Hate 'val'}
+
+ \mbox{}\\[-22mm]\mbox{}
+
+ \begin{center}
+ \begin{bubble}[10.5cm]
+ Subject: \textbf{Hate '\textbf{\texttt{val}}'}\hfill 01:00 AM\medskip\\
+
+ Hello Mr Urban,\medskip\\
+
+ I just wanted to ask, how are we suppose to work
+ with the completely useless \textbf{\texttt{val}}, that can’t be changed ever? Why is
+ this rule active at all? I’ve spent 4 hours not thinking on the
+ coursework, but how to bypass this annoying rule. What’s the whole
+ point of all these coursework, when we can’t use everything Scala
+ gives us?!?\medskip\\
+
+ Regards.\\
+ \mbox{}\hspace{5mm}\textcolor{black!50}{<<deleted>>}\\
+ \end{bubble}
+ \end{center}
+
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\begin{frame}[c]
-\mbox{}\\[-25mm]\mbox{}
+ \mbox{}\\[-25mm]\mbox{}
\begin{center}
\begin{bubble}[10.5cm]
- Subject: \textbf{Hate '\textbf{\texttt{val}}'}\hfill 01:00 AM\medskip\\
-
- Hello Mr Urban,\medskip\\
+ Subject: \textbf{Re: Hate '\textbf{\texttt{val}}'}\hfill 01:02 AM\bigskip\bigskip\\
- I just wanted to ask, how are we suppose to work
- with the completely useless \textbf{\texttt{val}}, that can’t be changed ever? Why is
- this rule active at all? I’ve spent 4 hours not thinking on the
- coursework, but how to bypass this annoying rule. What’s the whole
- point of all these coursework, when we can’t use everything Scala
- gives us?!?\medskip\\
-
- Regards.\\
- \mbox{}\hspace{5mm}\textcolor{black!50}{<<deleted>>}\\
+ \textcolor{black!70}{
+ \textit{\large<<my usual rant about fp\ldots\\ concurrency bla bla\ldots{} better programs
+ yada>>}}\bigskip\bigskip\bigskip
+
+ PS: What are you trying to do where you desperately want to use \texttt{var}?
\end{bubble}
\end{center}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\def\firstcircle{(0,0) circle (1.4cm)}
-\def\secondcircle{(0:2cm) circle (1.4cm)}
-\colorlet{circle edge}{blue!50}
-\colorlet{circle area}{blue!20}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\begin{frame}[c,fragile]
+
+\begin{textblock}{6}(0.5,0.5)
+\begin{bubble}[11.5cm]
+ \small
+ Subject: \textbf{Re: Re: Hate '\textbf{\texttt{val}}'}\hfill 01:04 AM\medskip\\
-\tikzset{filled/.style={fill=circle area, draw=circle edge, thick},
- outline/.style={draw=circle edge, thick}}
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\begin{frame}[c,fragile]
-\frametitle{Par: Intersections}
+ \textbf{Right now my is\_legal function works fine:}
+
+\footnotesize\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
+ def is_legal(dim: Int, path: Path)(x: Pos): Boolean = {
+ var boolReturn = false
+ if(x._1 > dim || x._2 > dim || x._1 < 0 || x._2 < 0) {
+ else { var breakLoop = false
+ if(path == Nil) { boolReturn = true }
+ else { for(i <- 0 until path.length) {
+ if(breakLoop == false) {
+ if(path(i) == x) {
+ boolReturn = true
+ breakLoop = true
+ }
+ else { boolReturn = false }
+ } else breakLoop
+ }
+ }
+ boolReturn
+ }
+\end{lstlisting}
+\end{bubble}
+\end{textblock}
-\begin{textblock}{6}(1,2)
-\begin{tikzpicture}
- \draw[outline] \firstcircle node {$A$};
- \node[anchor=south] at (current bounding box.north)
- {$A = \{1,2,3,\ldots,1000\}$};
-\end{tikzpicture}
+\begin{textblock}{6}(8.2,11.8)
+\begin{bubble}[5.5cm]\footnotesize\bf
+\ldots{}but I can’t make it work with boolReturn being val. What approach would
+you recommend in this case, and is using var in this case justified?
+\end{bubble}
\end{textblock}
-\begin{textblock}{6}(8,2)
-\begin{tikzpicture}
- \draw[outline] \secondcircle node {$B$};
- \node[anchor=south] at (current bounding box.north)
- {$B = \{1,5,9,13,\ldots,997\}$};
-\end{tikzpicture}
+\only<2>{
+\begin{textblock}{6}(0.3,11.8)
+ \begin{bubble}[3.1cm]
+ \textbf{Me:}
+ \raisebox{-12mm}{\includegraphics[scale=0.08]{../pics/throwup.jpg}}
+ \end{bubble}
+\end{textblock}}
+
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\begin{frame}[t,fragile]
+
+\mbox{}\\[-25mm]\mbox{}
+
+\begin{textblock}{6}(0.5,2)
+ \begin{bubble}[11.5cm]
+ Subject: \textbf{Re: Re: Re: Hate '\textbf{\texttt{val}}'}\hfill 01:06 AM\bigskip\\
+ \small
+
+ OK. So you want to make sure that the \texttt{x}-position is not outside the
+ board....and furthermore you want to make sure that the \texttt{x}-position
+ is not yet in the path list. How about something like\bigskip
+
+\footnotesize\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
+ def is_legal(dim: Int, path: Path)(x: Pos): Boolean =
+ ...<<some board conditions>>... && !path.contains(x)
+\end{lstlisting}\bigskip
+
+ \small Does not even contain a \texttt{val}.
+ \end{bubble}
+\end{textblock}
+
+\begin{textblock}{6}(7,12)
+\footnotesize\textcolor{black!50}{(This is all on one line)}
\end{textblock}
-\begin{textblock}{6}(3.3,9)
-\begin{tikzpicture}
- \begin{scope}
- \clip \firstcircle;
- \fill[filled] \secondcircle;
- \end{scope}
- \draw[outline] \firstcircle node {$A$};
- \draw[outline] \secondcircle node {$B$};
- \node[anchor=north] at (current bounding box.south)
- {How many elements are in $A \cap B$?};
-\end{tikzpicture}
-\end{textblock}
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\begin{frame}[t,fragile]
+
+\mbox{}\\[-15mm]\mbox{}
+
+\begin{textblock}{6}(1,3)
+ \begin{bubble}[10.5cm]
+ Subject: \textbf{Re: Re: Re: Re: Hate '\textbf{\texttt{val}}'}\hfill 11:02 AM\bigskip\bigskip\\
+
+ THANK YOU! You made me change my coding perspective. Because of you,
+ I figured out the next one\ldots
+ \end{bubble}
+\end{textblock}
+\only<2>{
+\begin{textblock}{6}(0.3,11.8)
+ \begin{bubble}[3.1cm]
+ \textbf{Me:}
+ \raisebox{-12mm}{\includegraphics[scale=0.15]{../pics/happy.jpg}}
+ \end{bubble}
+\end{textblock}}
+
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\begin{frame}[c]
+\frametitle{Assignments}
+
+Don't change any names or types in the templates!\bigskip
+
+Avoid at all costs:
+
+\begin{itemize}
+\item \code{var}
+\item \code{return}
+\item \texttt{ListBuffer}
+\item \texttt{mutable}
+\item \texttt{.par}
+\end{itemize}\bigskip\bigskip
+
+I cannot think of a good reason to use stacks.
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -285,37 +378,23 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[t]
-\frametitle{Why Scala? No null!}
-
-
-\begin{itemize}
-\item \large {\bf You can avoid \textcolor{blue}{\texttt{null}}}:
-\end{itemize}
+\frametitle{Option Type}
-\begin{textblock}{6}(1,5)
- \begin{bubble}[10.5cm]\small
- ``I call it my billion-dollar mistake. It was the invention of
- the null reference in 1965. At that time, I was designing the
- first comprehensive type system for references in an object
- oriented language (ALGOL W). My goal was to ensure that all use
- of references should be absolutely safe, with checking performed
- automatically by the compiler. But I couldn't resist the
- temptation to put in a null reference, simply because it was so
- easy to implement. This has led to innumerable errors,
- vulnerabilities, and system crashes, which have probably caused
- a billion dollars of pain and damage in the last forty years.''
- \hfill Sir Tony (Hoare)
-\end{bubble}
-\end{textblock}
-
-\begin{textblock}{5}(12.5,1.9)
-\includegraphics[scale=0.05]{../pics/hoare.jpg}\\
-\end{textblock}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\begin{frame}[t]
+\frametitle{Higher-Order Functions}
+
+
+
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c]
\frametitle{\begin{tabular}{c}\\[0cm]\alert{Questions?}\end{tabular}}