--- a/LINKS Thu Nov 09 16:34:08 2017 +0000
+++ b/LINKS Fri Nov 10 00:56:12 2017 +0000
@@ -1,3 +1,6 @@
+scala -Yrepl-class-based
+
+
why functional programming matters
http://queue.acm.org/detail.cfm?id=2038036
--- a/progs/collatz_sol.scala Thu Nov 09 16:34:08 2017 +0000
+++ b/progs/collatz_sol.scala Fri Nov 10 00:56:12 2017 +0000
@@ -16,16 +16,5 @@
}
-// some testing harness...5 Mio pushes the envelope
-
-val bnds = List(2, 10, 100, 1000, 10000, 100000,
- 77000, 90000, 1000000, 5000000)
-
-for (bnd <- bnds) {
- val (steps, max) = collatz_max(bnd)
- println(s"In the range of 1 - ${bnd} the number ${max} needs the maximum steps of ${steps}")
}
-
-}
-
--- a/progs/drumb_sol.scala Thu Nov 09 16:34:08 2017 +0000
+++ b/progs/drumb_sol.scala Fri Nov 10 00:56:12 2017 +0000
@@ -10,14 +10,6 @@
val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI",
"DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "GGP", "HCP")
-// (1) The function below should obtain the first trading price
-// for a stock symbol by using the query
-//
-// http://ichart.yahoo.com/table.csv?s=<<symbol>>&a=0&b=1&c=<<year>>&d=1&e=1&f=<<year>>
-//
-// and extracting the first January Adjusted Close price in a year.
-
-
import io.Source
import scala.util._
@@ -35,10 +27,6 @@
get_first_price("FB", 2014)
-// Complete the function below that obtains all first prices
-// for the stock symbols from a portfolio for the given
-// range of years
-
def get_prices(portfolio: List[String], years: Range): List[List[Option[Double]]] =
for (year <- years.toList) yield
for (symbol <- portfolio) yield get_first_price(symbol, year)
@@ -50,9 +38,6 @@
val tt = get_prices(List("BIDU"), 2004 to 2008)
-// (2) The first function below calculates the change factor (delta) between
-// a price in year n and a price in year n+1. The second function calculates
-// all change factors for all prices (from a portfolio).
def get_delta(price_old: Option[Double], price_new: Option[Double]) : Option[Double] = {
(price_old, price_new) match {
@@ -70,13 +55,6 @@
val d = get_deltas(p)
val ttd = get_deltas(tt)
-// (3) Write a function that given change factors, a starting balance and a year
-// calculates the yearly yield, i.e. new balanace, according to our dump investment
-// strategy. Another function calculates given the same data calculates the
-// compound yield up to a given year. Finally a function combines all
-// calculations by taking a portfolio, a range of years and a start balance
-// as arguments.
-
def yearly_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = {
val somes = data(year).flatten
@@ -95,9 +73,6 @@
}
}
-yearly_yield(d, 100, 0)
-//compound_yield(d.take(6), 100, 0)
-
def investment(portfolio: List[String], years: Range, start_balance: Long): Long = {
compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0)
}
@@ -106,8 +81,8 @@
//test cases for the two portfolios given above
-//println("Real data: " + investment(rstate_portfolio, 1978 to 2017, 100))
-//println("Blue data: " + investment(blchip_portfolio, 1978 to 2017, 100))
+println("Real data: " + investment(rstate_portfolio, 1978 to 2017, 100))
+println("Blue data: " + investment(blchip_portfolio, 1978 to 2017, 100))
}
--- a/progs/lecture1.scala Thu Nov 09 16:34:08 2017 +0000
+++ b/progs/lecture1.scala Fri Nov 10 00:56:12 2017 +0000
@@ -13,7 +13,6 @@
// (you cannot reassign values: z = 9 will give an error)
-
// Hello World
//=============
@@ -97,7 +96,7 @@
"1,2,3,4,5".split(",").mkString("\n")
"1,2,3,4,5".split(",3,").mkString("\n")
-// Types
+// Types (slide)
//=======
/* Scala is a strongly typed language
@@ -124,16 +123,16 @@
println("""">\n<"""")
/* in Java
-val lyrics = "Baa, Baa, Black Sheep \n" +
- "Have you any wool? \n" +
- "Yes, sir, yes sir \n" +
- "Three bags full"
+val lyrics = "Sun dips down, the day has gone \n" +
+ "Witches, wolves and giants yawn \n" +
+ "Queen and dragon, troll and gnome \n" +
+ "Baddy buddies head for home"
*/
-val lyrics = """Baa, Baa, Black Sheep
- |Have you any wool?
- |Yes, sir, yes sir
- |Three bags full""".stripMargin
+val lyrics = """Sun dips down, the day has gone
+ |Witches, wolves and giants yawn
+ |Queen and dragon, troll and gnome
+ |Baddy buddies head for home""".stripMargin
println(lyrics)
@@ -276,25 +275,73 @@
for (n <- (1 to 10)) println(n)
-// concurrency (ONLY WORKS IN SCALA 2.11.8, not in SCALA 2.12.0)
+
+
+
+
+// concurrency (ONLY WORKS IN SCALA 2.11.8, not in SCALA 2.12)
+// (would need to have this wrapped into a function, or
+// REPL called with scala -Yrepl-class-based)
for (n <- (1 to 10)) println(n)
for (n <- (1 to 10).par) println(n)
// for measuring time
-def time_needed[T](i: Int, code: => T) = {
+def time_needed[T](n: Int, code: => T) = {
val start = System.nanoTime()
- for (j <- 1 to i) code
+ for (i <- (0 to n)) code
val end = System.nanoTime()
- ((end - start) / i / 1.0e9) + " secs"
+ (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)
+// Problems with mutability and parallel computations
+//====================================================
+
+def count_intersection(A: Set[Int], B: Set[Int]) : Int = {
+ var count = 0
+ for (x <- A; if (B contains x)) count += 1
+ count
+}
+
+val A = (1 to 1000).toSet
+val B = (1 to 1000 by 4).toSet
+
+count_intersection(A, B)
+
+// but do not try to add .par to the for-loop above
+
+
+//propper parallel version
+def count_intersection2(A: Set[Int], B: Set[Int]) : Int =
+ A.par.count(x => B contains x)
+
+count_intersection2(A, B)
+
+
+//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))
+
+
+
+
// Webpages
//==========
@@ -302,10 +349,13 @@
// obtaining a webpage
val url = """https://nms.kcl.ac.uk/christian.urban/"""
-val url = """http://api.postcodes.io/postcodes/CR84LQ"""
Source.fromURL(url)("ISO-8859-1").mkString
+// another example
+//val url = """http://api.postcodes.io/postcodes/CR84LQ"""
+
+
// a function for looking up constituency data
def consty_lookup(pcode: String) : String = {
val url = "http://api.postcodes.io/postcodes/" + pcode
@@ -350,7 +400,7 @@
// naive version of crawl - searches until a given depth,
// visits pages potentially more than once
-def crawl(url: String, n: Int): Unit = {
+def crawl(url: String, n: Int) : Unit = {
if (n == 0) ()
else {
println(s"Visiting: $n $url")
Binary file slides/slides01.pdf has changed
--- a/slides/slides01.tex Thu Nov 09 16:34:08 2017 +0000
+++ b/slides/slides01.tex Fri Nov 10 00:56:12 2017 +0000
@@ -55,7 +55,7 @@
\begin{tabular}{l}
\mbox{}\hspace{-1mm}\includegraphics[scale=0.36]{../pics/twitter.png}\\[-1mm]
\includegraphics[scale=0.30]{../pics/linked.png}\\
-\includegraphics[scale=0.30]{../pics/guardian.jpg}\\[-3mm]
+%\includegraphics[scale=0.30]{../pics/guardian.jpg}\\[-3mm]
\mbox{}\hspace{-2mm}\includegraphics[scale=0.38]{../pics/morgan.png}\\[-3mm]
\includegraphics[scale=0.30]{../pics/suisse.png}\\
{\large\bf ...}
@@ -93,7 +93,7 @@
\small
alternatives:\\
-Elm, Haskell, Ocaml, F\sharp, Erlang, ML, Lisp (Racket), \ldots
+Elm, Haskell, Ocaml, F$\sharp$, Erlang, ML, Lisp (Racket), \ldots
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -114,8 +114,6 @@
\textbf{\large Java}
\end{textblock}
-
-
\begin{textblock}{6}(13,13.4)
\textbf{\large Scala}
\end{textblock}
@@ -148,7 +146,7 @@
%
\mbox{}\\[3.5cm]
\small
-Scala, Elm, Haskell, Ocaml, F\sharp, Erlang, ML, Lisp (Racket), \ldots
+Scala, Elm, Haskell, Ocaml, F$\sharp$, Erlang, ML, Lisp (Racket), \ldots
\only<3>{
\begin{textblock}{6}(2,6.2)
@@ -289,10 +287,10 @@
\end{textblock}}
\only<3>{
- \begin{textblock}{5}(1,8)
- \begin{bubble}[2.1cm]
- \footnotesize{}in Java or C++\\
- \end{bubble}
+ \begin{textblock}{14.2}(1,12.3)
+ In FP: Once a variable is created, it is assigned a value and then
+ never changed again $\Rightarrow$ no synchronisation\smallskip\\
+ \small\textcolor{gray}{(Andrew's second favourite feature of C++)}
\end{textblock}}
\end{frame}
@@ -380,23 +378,219 @@
{\lstset{language=Java}\fontsize{7}{8}\selectfont
\texttt{\lstinputlisting{URLReader.java}}}
-\only<2>{
-\begin{textblock}{5}(12,2)
-\includegraphics[scale=0.50]{../pics/skeleton.jpg}\\
-\end{textblock}}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c]
+\frametitle{Coursework}
+
+\begin{itemize}
+\item sorry, I might have been a bit wordy:\\
+ CW description is 7 pages, but
+ I only needed \mbox{< 100} loc for all the CW\bigskip
+
+\item there is email feedback when pushing code to github\bigskip
+
+\item we want you to learn FP: \alert{no vars}, no mutable
+ datastructures, e.g.~\texttt{ListBuffer}
+\end{itemize}
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+\begin{frame}[c, fragile]
+\frametitle{The Joy of Immutability}
+
+\begin{itemize}
+\item If you need to manipulate some data in a list say, then you make
+ a new list with the updated values, rather than revise the original
+ list. Easy!\medskip
+
+ {\small
+ \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
+ val old_list = List(1, 2, 3, 5)
+ val new_list = 0 :: old_list
+ \end{lstlisting}}
+
+\item You do not have to be defensive about who can access the data
+ (concurrency, lazyness).
+\end{itemize}
+\end{frame}
+
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\begin{frame}[t]
+\frametitle{Email: Hate 'val'}
+
+\mbox{}\\[-25mm]\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{}
+
+\begin{center}
+ \begin{bubble}[10.5cm]
+ Subject: \textbf{Re: Hate '\textbf{\texttt{val}}'}\hfill 01:02 AM\bigskip\bigskip\\
+
+ \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}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\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\\
+
+ \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}(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}
+
+\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}
+
+\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{Conclusion}
\begin{itemize}
\item Scala is still under heavy development\\ (the compiler is terribly slow)\medskip
\item {\bf\url{http://www.scala-lang.org/}}\bigskip
-\item it is a rather \textbf{\alert{deep}} language\ldots i.e.~gives you a lot of
- rope to shoot yourself\bigskip\bigskip
+
+\item it is a rather \textbf{\alert{deep}} language\ldots i.e.~gives
+ you a lot of rope to shoot yourself\bigskip
+\item learning functional programming is not easy\ldots{}when you have
+ spent all of your career thinking in a procedural way it is hard to
+ change\bigskip\medskip
+
\item hope you have fun with the coursework
\end{itemize}
\end{frame}
@@ -404,9 +598,18 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c]
-\frametitle{\begin{tabular}{c}\\[3cm]\alert{Questions?}\end{tabular}}
+\frametitle{\begin{tabular}{c}\\[0cm]\alert{Questions?}\end{tabular}}
-\mbox{}
+\begin{center}
+ \begin{tabular}[t]{@{}l@{}l@{}}
+ \includegraphics[scale=0.1]{../pics/mand4.png} &
+ \raisebox{1.2mm}{\includegraphics[scale=0.1]{../pics/mand3.png}}
+ \end{tabular}
+\end{center}
+
+\begin{center}
+My Scala Office Hours: Thursdays 11 -- 13
+\end{center}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}
@@ -419,3 +622,10 @@
%%% TeX-master: t
%%% End:
+
+“Functional programming is often regarded as the best-kept secret of
+scientific modelers, mathematicians, artificial intelligence
+researchers, financial institutions, graphic designers, CPU designers,
+compiler programmers, and telecommunications engineers.”
+
+The Wikipedia F# page
\ No newline at end of file
--- a/slides/slides03.tex Thu Nov 09 16:34:08 2017 +0000
+++ b/slides/slides03.tex Fri Nov 10 00:56:12 2017 +0000
@@ -206,6 +206,13 @@
\end{bubble}
\end{textblock}
+\only<2>{
+\begin{textblock}{6}(0.3,11.8)
+ \begin{bubble}[3.1cm]
+ \textbf{Me:} \includegraphics[scale=0.08]{../pics/happy.jpg}
+ \end{bubble}
+\end{textblock}}
+
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%