| author | Christian Urban <christian.urban@kcl.ac.uk> | 
| Wed, 09 Nov 2022 18:40:57 +0000 | |
| changeset 434 | 1eb11d787af7 | 
| parent 423 | 554278cd4b70 | 
| child 438 | a02e9efd7bc9 | 
| permissions | -rw-r--r-- | 
| 197 | 1 | % !TEX program = xelatex | 
| 123 | 2 | \documentclass{article}
 | 
| 423 | 3 | \usepackage{../styles/style}
 | 
| 4 | \usepackage{../styles/langs}
 | |
| 272 | 5 | \usepackage{tikz}
 | 
| 6 | \usepackage{pgf}
 | |
| 123 | 7 | \usepackage{marvosym}
 | 
| 184 | 8 | \usepackage{boxedminipage}
 | 
| 123 | 9 | |
| 352 | 10 | \lstset{escapeinside={/*!}{!*/}}
 | 
| 11 | \newcommand{\annotation}[1]{\hfill\footnotesize{}#1}
 | |
| 272 | 12 | |
| 352 | 13 | \usepackage{menukeys}
 | 
| 335 | 14 | |
| 15 | ||
| 123 | 16 | %cheat sheet | 
| 17 | %http://worldline.github.io/scala-cheatsheet/ | |
| 18 | ||
| 181 | 19 | % case class, apply, unapply | 
| 170 | 20 | % see https://medium.com/@thejasbabu/scala-pattern-matching-9c9e73ba9a8a | 
| 21 | ||
| 191 | 22 | % the art of programming | 
| 23 | % https://www.youtube.com/watch?v=QdVFvsCWXrA | |
| 24 | ||
| 25 | % functional programming in Scala | |
| 26 | %https://www.amazon.com/gp/product/1449311032/ref=as_li_ss_tl?ie=UTF8&tag=aleottshompag-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=1449311032 | |
| 27 | ||
| 197 | 28 | % functional programming in C | 
| 191 | 29 | %https://www.amazon.com/gp/product/0201419505/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=0201419505&linkCode=as2&tag=aleottshompag-20 | 
| 30 | ||
| 31 | %speeding through haskell | |
| 32 | %https://openlibra.com/en/book/download/speeding-through-haskell | |
| 33 | ||
| 34 | % fp books --- ocaml | |
| 35 | % http://courses.cms.caltech.edu/cs134/cs134b/book.pdf | |
| 36 | % http://alexott.net/en/fp/books/ | |
| 37 | ||
| 257 | 38 | %John Hughes’ simple words: | 
| 39 | %A combinator is a function which builds program fragments | |
| 40 | %from program fragments. | |
| 41 | ||
| 42 | ||
| 301 | 43 | %explain graph colouring program (examples from) | 
| 264 | 44 | %https://www.metalevel.at/prolog/optimization | 
| 45 | ||
| 46 | % nice example for map and reduce using Harry potter characters | |
| 47 | % https://www.matthewgerstman.com/map-filter-reduce/ | |
| 48 | ||
| 333 | 49 | % interesting talk about differences in Java and Scala | 
| 50 | % Goto'19 conference ; about differences in type-system | |
| 51 | % https://www.youtube.com/watch?v=e6n-Ci8V2CM | |
| 264 | 52 | |
| 329 | 53 | % Timing | 
| 54 | % | |
| 55 | % xs.map(x => (x, xs.count(_==x))) | |
| 56 | % | |
| 57 | % vs xs.groupBy(identity) | |
| 58 | % | |
| 59 | % first is quadratic, while second is linear. | |
| 60 | ||
| 61 | % contrast map with a for loop in imperative languages | |
| 62 | % | |
| 63 | % Let’s use a simple example of calculating sales tax on an array of | |
| 64 | % prices. | |
| 65 | % | |
| 66 | % const prices = [19.99, 4.95, 25, 3.50]; | |
| 67 | % let new_prices = []; | |
| 68 | % | |
| 69 | %       for(let i=0; i < prices.length; i++) {
 | |
| 70 | % new_prices.push(prices[i] * 1.06); | |
| 71 | % } | |
| 72 | % | |
| 73 | % We can achieve the same results using .map(): | |
| 74 | % | |
| 75 | % const prices = [19.99, 4.95, 25, 3.50]; | |
| 76 | % let new_prices = prices.map(price => price * 1.06); | |
| 77 | % | |
| 78 | % The syntax above is condensed so let’s walk through it a bit. The | |
| 79 | % .map() method takes a callback, which can be thought of as a function. | |
| 80 | % That’s what is between the parentheses. The variable price is the name | |
| 81 | % that will be used to identify each value. Since there’s only one | |
| 82 | % input, we can omit the usual parentheses around the parameters. | |
| 83 | ||
| 84 | % potentially a worked example? Tetris in scala.js | |
| 85 | % | |
| 86 | % https://medium.com/@michael.karen/learning-modern-javascript-with-tetris-92d532bcd057 | |
| 87 | % | |
| 88 | % Scala videos | |
| 89 | % https://www.youtube.com/user/DrMarkCLewis | |
| 90 | ||
| 334 | 91 | |
| 92 | %% https://alvinalexander.com/downloads/HelloScala-FreePreview.pdf | |
| 93 | %% | |
| 94 | %% Section 10 about strings; interpolations and multiline strings | |
| 95 | ||
| 343 | 96 | % Easy installation | 
| 97 | %https://alexarchambault.github.io/posts/2020-09-21-cs-setup.html | |
| 98 | ||
| 423 | 99 | % scala libraries | 
| 100 | %https://index.scala-lang.org | |
| 101 | ||
| 102 | ||
| 103 | % Learning functional programming is an opportunity to discover a new | |
| 104 | % way to represent programs, to approach problems, and to think about | |
| 105 | % languages. While programming with a functional language is still | |
| 106 | % fundamentally similar to programming with any other type of language | |
| 107 | % (examples of others being imperative or logic), it represents | |
| 108 | % programs and algorithms through distinct forms of abstraction and | |
| 109 | % gives you a new toolset with which to solve programming | |
| 110 | % problems. Additionally, many of the techniques of functional | |
| 111 | % programming are beginning to permeate new mainstream languages, so | |
| 112 | % taking the time now to develop a thorough understanding of them is | |
| 113 | % an investment which will pay great dividends. | |
| 114 | ||
| 115 | ||
| 334 | 116 | |
| 335 | 117 | % Exact colors from NB | 
| 118 | \usepackage[breakable]{tcolorbox}
 | |
| 119 | \definecolor{incolor}{HTML}{303F9F}
 | |
| 120 | \definecolor{outcolor}{HTML}{D84315}
 | |
| 121 | \definecolor{cellborder}{HTML}{CFCFCF}
 | |
| 122 | \definecolor{cellbackground}{HTML}{F7F7F7}
 | |
| 334 | 123 | |
| 335 | 124 | |
| 125 | ||
| 123 | 126 | \begin{document}
 | 
| 395 | 127 | \fnote{\copyright{} Christian Urban, King's College London, 2017, 2018, 2019, 2020, 2021}
 | 
| 123 | 128 | |
| 335 | 129 | %\begin{tcolorbox}[breakable,size=fbox,boxrule=1pt,pad at break*=1mm,colback=cellbackground,colframe=cellborder]
 | 
| 130 | % abd | |
| 131 | %\end{tcolorbox}
 | |
| 132 | ||
| 125 | 133 | \section*{A Crash-Course in Scala}
 | 
| 123 | 134 | |
| 182 | 135 | \mbox{}\hfill\textit{``Scala --- \underline{S}lowly \underline{c}ompiled 
 | 
| 136 | \underline{a}cademic \underline{la}nguage''}\smallskip\\
 | |
| 192 | 137 | \mbox{}\hfill\textit{ --- a joke(?) found on Twitter}\bigskip
 | 
| 195 | 138 | |
| 139 | \subsection*{Introduction}
 | |
| 140 | ||
| 178 | 141 | \noindent | 
| 170 | 142 | Scala is a programming language that combines functional and | 
| 143 | object-oriented programming-styles. It has received quite a bit of | |
| 181 | 144 | attention in the last five or so years. One reason for this attention is | 
| 145 | that, like the Java programming language, Scala compiles to the Java | |
| 146 | Virtual Machine (JVM) and therefore Scala programs can run under MacOSX, | |
| 195 | 147 | Linux and Windows. Because of this it has also access to | 
| 181 | 148 | the myriads of Java libraries. Unlike Java, however, Scala often allows | 
| 186 | 149 | programmers to write very concise and elegant code. Some therefore say | 
| 182 | 150 | ``Scala is the better Java''.\footnote{from
 | 
| 188 | 151 | \url{https://www.slideshare.net/maximnovak/joy-of-scala}} 
 | 
| 152 | ||
| 191 | 153 | A number of companies---the Guardian, Twitter, Coursera, FourSquare, | 
| 154 | Netflix, LinkedIn, ITV to name a few---either use Scala exclusively in | |
| 188 | 155 | production code, or at least to some substantial degree. Scala seems | 
| 156 | also useful in job-interviews (especially in data science) according to | |
| 157 | this anecdotal report | |
| 170 | 158 | |
| 181 | 159 | \begin{quote}
 | 
| 160 | \url{http://techcrunch.com/2016/06/14/scala-is-the-new-golden-child}
 | |
| 170 | 161 | \end{quote}
 | 
| 162 | ||
| 163 | \noindent | |
| 164 | The official Scala compiler can be downloaded from | |
| 165 | ||
| 166 | \begin{quote}
 | |
| 195 | 167 | \url{http://www.scala-lang.org}\medskip
 | 
| 168 | \end{quote}
 | |
| 170 | 169 | |
| 395 | 170 | \noindent\alert | 
| 171 | Just make sure you are downloading the ``battle tested'' version of | |
| 172 | Scala \textbf{2.13} This is the one I am going to use in the lectures and
 | |
| 173 | in the coursework. The newer Scala 3.0 \& 3.1 still have some | |
| 174 | features not fully implemented.\bigskip | |
| 175 | ||
| 170 | 176 | \noindent | 
| 265 | 177 | If you are interested, there are also experimental backends of Scala | 
| 195 | 178 | for producing code under Android (\url{http://scala-android.org}); for
 | 
| 179 | generating JavaScript code (\url{https://www.scala-js.org}); and there
 | |
| 180 | is work under way to have a native Scala compiler generating X86-code | |
| 181 | (\url{http://www.scala-native.org}). Though be warned these backends
 | |
| 182 | are still rather beta or even alpha. | |
| 183 | ||
| 184 | \subsection*{VS Code and Scala}
 | |
| 185 | ||
| 184 | 186 | I found a convenient IDE for writing Scala programs is Microsoft's | 
| 181 | 187 | \textit{Visual Studio Code} (VS Code) which runs under MacOSX, Linux and
 | 
| 269 
3ef2542207c4
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
265diff
changeset | 188 | obviously Windows.\footnote{\ldots{}unlike \emph{Microsoft Visual Studio}---note
 | 
| 191 | 189 | the minuscule difference in the name---which is a heavy-duty, | 
| 269 
3ef2542207c4
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
265diff
changeset | 190 | Windows-only IDE\ldots{}jeez, with all their money could they not have come
 | 
| 191 | 191 | up with a completely different name for a complete different project? | 
| 192 | For the pedantic, Microsoft Visual Studio is an IDE, whereas Visual | |
| 269 
3ef2542207c4
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
265diff
changeset | 193 | Studio Code is considered to be a \emph{source code editor}. Anybody knows what the
 | 
| 191 | 194 | difference is?} It can be downloaded for free from | 
| 181 | 195 | |
| 196 | \begin{quote}
 | |
| 197 | \url{https://code.visualstudio.com}
 | |
| 198 | \end{quote}
 | |
| 199 | ||
| 200 | \noindent | |
| 201 | and should already come pre-installed in the Department (together with | |
| 195 | 202 | the Scala compiler). Being a project that just started in 2015, VS Code is | 
| 189 | 203 | relatively new and thus far from perfect. However it includes a | 
| 182 | 204 | \textit{Marketplace} from which a multitude of extensions can be
 | 
| 184 | 205 | downloaded that make editing and running Scala code a little easier (see | 
| 206 | Figure~\ref{vscode} for my setup).
 | |
| 181 | 207 | |
| 208 | \begin{figure}[t]
 | |
| 184 | 209 | \begin{boxedminipage}{\textwidth}  
 | 
| 181 | 210 | \begin{center}  
 | 
| 211 | \includegraphics[scale=0.15]{../pics/vscode.png}\\[-10mm]\mbox{}
 | |
| 212 | \end{center}
 | |
| 195 | 213 | \caption{My installation of VS Code includes the following
 | 
| 400 | 214 |   packages from Marketplace: \textbf{Scala Syntax (official)} 0.5.4,
 | 
| 215 |   \textbf{Code Runner} 0.11.6, \textbf{Code Spell Checker} 2.0.12,
 | |
| 216 |   \textbf{Rewrap} 1.14.0 and \textbf{Subtle Match
 | |
| 195 | 217 |   Brackets} 3.0.0. I have also bound the keys \keys{Ctrl} \keys{Ret} to the
 | 
| 218 | action ``Run-Selected-Text-In-Active-Terminal'' in order to quickly | |
| 272 | 219 | evaluate small code snippets in the Scala REPL. I use the internal | 
| 400 | 220 |   terminal to run Scala 2.13.6.\label{vscode}}
 | 
| 184 | 221 | \end{boxedminipage}
 | 
| 181 | 222 | \end{figure}  
 | 
| 223 | ||
| 184 | 224 | What I like most about VS Code is that it provides easy access to the | 
| 186 | 225 | Scala REPL. But if you prefer another editor for coding, it is also | 
| 226 | painless to work with Scala completely on the command line (as you might | |
| 227 | have done with \texttt{g++} in the earlier part of PEP). For the
 | |
| 195 | 228 | lazybones among us, there are even online editors and environments for | 
| 197 | 229 | developing and running Scala programs: \textit{ScalaFiddle}
 | 
| 230 | and \textit{Scastie} are two of them. They require zero setup 
 | |
| 231 | (assuming you have a browser handy). You can access them at | |
| 181 | 232 | |
| 233 | \begin{quote}
 | |
| 195 | 234 |   \url{https://scalafiddle.io}\\
 | 
| 235 |   \url{https://scastie.scala-lang.org}\medskip
 | |
| 181 | 236 | \end{quote}
 | 
| 237 | ||
| 195 | 238 | \noindent | 
| 197 | 239 | But you should be careful if you use them for your coursework: they | 
| 240 | are meant to play around, not really for serious work. | |
| 241 | ||
| 269 
3ef2542207c4
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
265diff
changeset | 242 | As one might expect, Scala can be used with the heavy-duty IDEs Eclipse and IntelliJ. | 
| 182 | 243 | A ready-made Scala bundle for Eclipse is available from | 
| 170 | 244 | |
| 245 | \begin{quote}
 | |
| 246 | \url{http://scala-ide.org/download/sdk.html}
 | |
| 247 | \end{quote}
 | |
| 248 | ||
| 249 | \noindent | |
| 191 | 250 | Also IntelliJ includes plugins for Scala. \underline{\textbf{BUT}}, 
 | 
| 251 | I do \textbf{not} recommend the usage of either Eclipse or IntelliJ for PEP: these IDEs
 | |
| 182 | 252 | seem to make your life harder, rather than easier, for the small | 
| 269 
3ef2542207c4
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
265diff
changeset | 253 | programs that we will write in this module. They are really meant to be used | 
| 
3ef2542207c4
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
265diff
changeset | 254 | when you have a million-lines codebase than with our small | 
| 
3ef2542207c4
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
265diff
changeset | 255 | ``toy-programs''\ldots{}for example why on earth am I required to create a
 | 
| 182 | 256 | completely new project with several subdirectories when I just want to | 
| 272 | 257 | try out 20-lines of Scala code? Your mileage may vary though.~\texttt{;o)}
 | 
| 182 | 258 | |
| 259 | \subsection*{Why Functional Programming?}
 | |
| 260 | ||
| 186 | 261 | Before we go on, let me explain a bit more why we want to inflict upon | 
| 262 | you another programming language. You hopefully have mastered Java and | |
| 333 | 263 | C++\ldots{}the world should be your oyster, no? Well, matters are not as
 | 
| 264 | simple as one might wish. We do require Scala in PEP, but actually we do | |
| 265 | not religiously care whether you learn Scala---after all it is just a | |
| 266 | programming language (albeit a nifty one IMHO). What we do care about is | |
| 267 | that you learn about \textit{functional programming}. Scala is just the
 | |
| 268 | vehicle for that. Still, you need to learn Scala well enough to get good | |
| 269 | marks in PEP, but functional programming could perhaps equally be taught | |
| 270 | with Haskell, F\#, SML, Ocaml, Kotlin, Clojure, Scheme, Elm and many | |
| 271 | other functional programming languages. | |
| 272 | ||
| 273 | %Your friendly lecturer just | |
| 274 | %happens to like Scala and the Department agreed that it is a good idea | |
| 275 | %to inflict Scala upon you. | |
| 182 | 276 | |
| 277 | Very likely writing programs in a functional programming language is | |
| 183 | 278 | quite different from what you are used to in your study so far. It | 
| 279 | might even be totally alien to you. The reason is that functional | |
| 280 | programming seems to go against the core principles of | |
| 272 | 281 | \textit{imperative programming} (which is what you do in Java and C/C++
 | 
| 183 | 282 | for example). The main idea of imperative programming is that you have | 
| 277 | 283 | some form of \emph{state} in your program and you continuously change
 | 
| 284 | this state by issuing some commands---for example for updating a field | |
| 285 | in an array or for adding one to a variable and so on. The classic | |
| 286 | example for this style of programming is a \texttt{for}-loop in C/C++.
 | |
| 287 | Consider the snippet: | |
| 182 | 288 | |
| 289 | \begin{lstlisting}[language=C,numbers=none]
 | |
| 184 | 290 | for (int i = 10; i < 20; i++) { 
 | 
| 269 
3ef2542207c4
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
265diff
changeset | 291 | //...do something with i... | 
| 184 | 292 | } | 
| 182 | 293 | \end{lstlisting}
 | 
| 294 | ||
| 184 | 295 | \noindent Here the integer variable \texttt{i} embodies the state, which
 | 
| 296 | is first set to \texttt{10} and then increased by one in each
 | |
| 297 | loop-iteration until it reaches \texttt{20} at which point the loop
 | |
| 298 | exits. When this code is compiled and actually runs, there will be some | |
| 186 | 299 | dedicated space reserved for \texttt{i} in memory. This space of
 | 
| 188 | 300 | typically 32 bits contains \texttt{i}'s current value\ldots\texttt{10}
 | 
| 269 
3ef2542207c4
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
265diff
changeset | 301 | at the beginning, and then the content will be overwritten with | 
| 191 | 302 | new content in every iteration. The main point here is that this kind of | 
| 277 | 303 | updating, or overwriting, of memory is 25.806\ldots or \textbf{THE ROOT OF
 | 
| 191 | 304 | ALL EVIL}!! | 
| 186 | 305 | |
| 306 | \begin{center}
 | |
| 307 | \includegraphics[scale=0.25]{../pics/root-of-all-evil.png}
 | |
| 308 | \end{center}  
 | |
| 309 | ||
| 182 | 310 | |
| 311 | \noindent | |
| 312 | \ldots{}Well, it is perfectly benign if you have a sequential program
 | |
| 313 | that gets run instruction by instruction...nicely one after another. | |
| 314 | This kind of running code uses a single core of your CPU and goes as | |
| 184 | 315 | fast as your CPU frequency, also called clock-speed, allows. The problem | 
| 316 | is that this clock-speed has not much increased over the past decade and | |
| 317 | no dramatic increases are predicted for any time soon. So you are a bit | |
| 277 | 318 | stuck. This is unlike previous generations of developers who could rely | 
| 278 | 319 | upon the fact that approximately every 2 years their code would run | 
| 320 | twice as fast because the clock-speed of their CPUs got twice as fast. | |
| 333 | 321 | |
| 277 | 322 | Unfortunately this does not happen any more nowadays. To get you out of | 
| 323 | this dreadful situation, CPU producers pile more and more cores into | |
| 324 | CPUs in order to make them more powerful and potentially make software | |
| 325 | faster. The task for you as developer is to take somehow advantage of | |
| 326 | these cores by running as much of your code as possible in parallel on | |
| 333 | 327 | as many cores you have available (typically 4 or more in modern laptops | 
| 328 | and sometimes much more on high-end machines). In this situation | |
| 329 | \textit{mutable} variables like \texttt{i} in the C-code above are evil,
 | |
| 330 | or at least a major nuisance: Because if you want to distribute some of | |
| 331 | the loop-iterations over several cores that are currently idle in your | |
| 332 | system, you need to be extremely careful about who can read and | |
| 333 | overwrite the variable \texttt{i}.\footnote{If you are of the mistaken
 | |
| 334 | belief that nothing nasty can happen to \texttt{i} inside the
 | |
| 335 | \texttt{for}-loop, then you need to go back over the C++ material.}
 | |
| 336 | Especially the writing operation is critical because you do not want | |
| 337 | that conflicting writes mess about with \texttt{i}. Take my word: an
 | |
| 338 | untold amount of misery has arisen from this problem. The catch is that | |
| 339 | if you try to solve this problem in C/C++ or Java, and be as defensive | |
| 340 | as possible about reads and writes to \texttt{i}, then you need to
 | |
| 341 | synchronise access to it. The result is that very often your program | |
| 342 | waits more than it runs, thereby defeating the point of trying to run | |
| 343 | the program in parallel in the first place. If you are less defensive, | |
| 344 | then usually all hell breaks loose by seemingly obtaining random | |
| 345 | results. And forget the idea of being able to debug such code. | |
| 182 | 346 | |
| 184 | 347 | The central idea of functional programming is to eliminate any state | 
| 195 | 348 | from programs---or at least from the ``interesting bits'' of the | 
| 349 | programs. Because then it is easy to parallelise the resulting | |
| 350 | programs: if you do not have any state, then once created, all memory | |
| 351 | content stays unchanged and reads to such memory are absolutely safe | |
| 352 | without the need of any synchronisation. An example is given in | |
| 353 | Figure~\ref{mand} where in the absence of the annoying state, Scala
 | |
| 354 | makes it very easy to calculate the Mandelbrot set on as many cores of | |
| 355 | your CPU as possible. Why is it so easy in this example? Because each | |
| 356 | pixel in the Mandelbrot set can be calculated independently and the | |
| 357 | calculation does not need to update any variable. It is so easy in | |
| 358 | fact that going from the sequential version of the Mandelbrot program | |
| 359 | to the parallel version can be achieved by adding just eight | |
| 360 | characters---in two places you have to add \texttt{.par}. Try the same
 | |
| 272 | 361 | in C/C++ or Java! | 
| 182 | 362 | |
| 363 | \begin{figure}[p]
 | |
| 184 | 364 | \begin{boxedminipage}{\textwidth}
 | 
| 187 | 365 | |
| 191 | 366 | A Scala program for generating pretty pictures of the Mandelbrot set.\smallskip\\ | 
| 367 | (See \url{https://en.wikipedia.org/wiki/Mandelbrot_set} or\\
 | |
| 368 | \phantom{(See }\url{https://www.youtube.com/watch?v=aSg2Db3jF_4}):
 | |
| 184 | 369 | \begin{center}    
 | 
| 370 | \begin{tabular}{c}  
 | |
| 191 | 371 | \includegraphics[scale=0.11]{../pics/mand1.png}\\[-8mm]\mbox{}
 | 
| 184 | 372 | \end{tabular}
 | 
| 187 | 373 | \end{center}
 | 
| 184 | 374 | |
| 187 | 375 | \begin{center}
 | 
| 376 | \begin{tabular}{@{}p{0.45\textwidth}|p{0.45\textwidth}@{}}
 | |
| 191 | 377 | \bf sequential version: & \bf parallel version on 4 cores:\smallskip\\ | 
| 182 | 378 | |
| 191 | 379 |   {\hfill\includegraphics[scale=0.11]{../pics/mand4.png}\hfill} &
 | 
| 380 |   {\hfill\includegraphics[scale=0.11]{../pics/mand3.png}\hfill} \\
 | |
| 187 | 381 | |
| 382 | {\footnotesize\begin{lstlisting}[xleftmargin=-1mm]
 | |
| 186 | 383 | for (y <- (0 until H)) {
 | 
| 384 |   for (x <- (0 until W)) {
 | |
| 385 | ||
| 386 | val c = start + | |
| 387 | (x * d_x + y * d_y * i) | |
| 388 | val iters = iterations(c, max) | |
| 191 | 389 | val colour = | 
| 186 | 390 | if (iters == max) black | 
| 391 | else colours(iters % 16) | |
| 392 | ||
| 191 | 393 | pixel(x, y, colour) | 
| 186 | 394 | } | 
| 395 | viewer.updateUI() | |
| 396 | } | |
| 187 | 397 | \end{lstlisting}}   
 | 
| 398 | & | |
| 399 | {\footnotesize\begin{lstlisting}[xleftmargin=0mm]
 | |
| 400 | 400 | for (y <- (0 until H).par) {
 | 
| 401 |   for (x <- (0 until W).par) {
 | |
| 187 | 402 | |
| 403 | val c = start + | |
| 404 | (x * d_x + y * d_y * i) | |
| 405 | val iters = iterations(c, max) | |
| 191 | 406 | val colour = | 
| 187 | 407 | if (iters == max) black | 
| 408 | else colours(iters % 16) | |
| 409 | ||
| 191 | 410 | pixel(x, y, colour) | 
| 187 | 411 | } | 
| 412 | viewer.updateUI() | |
| 413 | } | |
| 191 | 414 | \end{lstlisting}}\\[-2mm]
 | 
| 187 | 415 | |
| 416 | \centering\includegraphics[scale=0.5]{../pics/cpu2.png} &
 | |
| 188 | 417 | \centering\includegraphics[scale=0.5]{../pics/cpu1.png}
 | 
| 184 | 418 | \end{tabular}
 | 
| 419 | \end{center}
 | |
| 270 
38e13601cb1b
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 420 | \caption{The code of the ``main'' loops in my version of the mandelbrot program.
 | 
| 191 | 421 | The parallel version differs only in \texttt{.par} being added to the
 | 
| 195 | 422 | ``ranges'' of the x and y coordinates. As can be seen from the CPU loads, in | 
| 423 | the sequential version there is a lower peak for an extended period, | |
| 191 | 424 | while in the parallel version there is a short sharp burst for | 
| 425 | essentially the same workload\ldots{}meaning you get more work done 
 | |
| 195 | 426 | in a shorter amount of time. This easy \emph{parallelisation} 
 | 
| 427 | only works reliably with an immutable program. | |
| 188 | 428 | \label{mand}} 
 | 
| 184 | 429 | \end{boxedminipage}
 | 
| 182 | 430 | \end{figure}  
 | 
| 431 | ||
| 275 | 432 | But remember this easy parallelisation of code requires that we have no | 
| 433 | state in our programs\ldots{}that is no counters like \texttt{i} in
 | |
| 434 | \texttt{for}-loops. You might then ask, how do I write loops without
 | |
| 435 | such counters? Well, teaching you that this is possible is one of the | |
| 436 | main points of the Scala-part in PEP. I can assure you it is possible, | |
| 437 | but you have to get your head around it. Once you have mastered this, it | |
| 438 | will be fun to have no state in your programs (a side product is that it | |
| 439 | much easier to debug state-less code and also more often than not easier | |
| 440 | to understand). So have fun with Scala!\footnote{If you are still not
 | |
| 441 | convinced about the function programming ``thing'', there are a few more | |
| 442 | arguments: a lot of research in programming languages happens to take | |
| 443 | place in functional programming languages. This has resulted in | |
| 444 | ultra-useful features such as pattern-matching, strong type-systems, | |
| 445 | laziness, implicits, algebraic datatypes to name a few. Imperative | |
| 446 | languages seem to often lag behind in adopting them: I know, for | |
| 447 | example, that Java will at some point in the future support | |
| 448 | pattern-matching, which has been used for example in SML for at least | |
| 449 | 40(!) years. See | |
| 186 | 450 | \url{http://cr.openjdk.java.net/~briangoetz/amber/pattern-match.html}.
 | 
| 275 | 451 | Automatic garbage collection was included in Java in 1995; the | 
| 452 | functional language LISP had this already in 1958. Generics were added | |
| 453 | to Java 5 in 2004; the functional language SML had it since 1990. | |
| 277 | 454 | Higher-order functions were added to C\# in 2007, to Java 8 in | 
| 275 | 455 | 2014; again LISP had them since 1958. Also Rust, a C-like programming | 
| 456 | language that has been developed since 2010 and is gaining quite some | |
| 457 | interest, borrows many ideas from functional programming from | |
| 277 | 458 | yesteryear.}\medskip | 
| 170 | 459 | |
| 277 | 460 | \noindent | 
| 461 | If you need any after-work distractions, you might have fun reading this | |
| 462 | about FP (functional programming): | |
| 463 | ||
| 464 | \begin{quote}
 | |
| 465 | \url{https://medium.com/better-programming/fp-toy-7f52ea0a947e}
 | |
| 466 | \end{quote}
 | |
| 188 | 467 | |
| 123 | 468 | \subsection*{The Very Basics}
 | 
| 469 | ||
| 470 | One advantage of Scala over Java is that it includes an interpreter (a | |
| 471 | REPL, or | |
| 472 | \underline{R}ead-\underline{E}val-\underline{P}rint-\underline{L}oop)
 | |
| 181 | 473 | with which you can run and test small code snippets without the need | 
| 123 | 474 | of a compiler. This helps a lot with interactively developing | 
| 188 | 475 | programs. It is my preferred way of writing small Scala | 
| 123 | 476 | programs. Once you installed Scala, you can start the interpreter by | 
| 477 | typing on the command line: | |
| 478 | ||
| 479 | \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
 | |
| 480 | $ scala | |
| 400 | 481 | Welcome to Scala 2.13.6 (OpenJDK 64-Bit Server VM, Java 17). | 
| 123 | 482 | Type in expressions for evaluation. Or try :help. | 
| 483 | ||
| 484 | scala> | |
| 485 | \end{lstlisting}%$
 | |
| 486 | ||
| 335 | 487 | |
| 488 | ||
| 123 | 489 | \noindent The precise response may vary depending | 
| 490 | on the version and platform where you installed Scala. At the Scala | |
| 491 | prompt you can type things like \code{2 + 3}\;\keys{Ret} and
 | |
| 492 | the output will be | |
| 493 | ||
| 494 | \begin{lstlisting}[numbers=none]
 | |
| 495 | scala> 2 + 3 | |
| 496 | res0: Int = 5 | |
| 497 | \end{lstlisting}
 | |
| 498 | ||
| 188 | 499 | \noindent The answer means that he result of the addition is of type | 
| 124 | 500 | \code{Int} and the actual result is 5; \code{res0} is a name that
 | 
| 125 | 501 | Scala gives automatically to the result. You can reuse this name later | 
| 188 | 502 | on, for example | 
| 181 | 503 | |
| 504 | \begin{lstlisting}[numbers=none]
 | |
| 505 | scala> res0 + 4 | |
| 506 | res1: Int = 9 | |
| 507 | \end{lstlisting}
 | |
| 508 | ||
| 509 | \noindent | |
| 510 | Another classic example you can try out is | |
| 123 | 511 | |
| 512 | \begin{lstlisting}[numbers=none]
 | |
| 513 | scala> print("hello world")
 | |
| 514 | hello world | |
| 515 | \end{lstlisting}
 | |
| 516 | ||
| 517 | \noindent Note that in this case there is no result. The | |
| 518 | reason is that \code{print} does not actually produce a result
 | |
| 124 | 519 | (there is no \code{resX} and no type), rather it is a
 | 
| 123 | 520 | function that causes the \emph{side-effect} of printing out a
 | 
| 521 | string. Once you are more familiar with the functional | |
| 522 | programming-style, you will know what the difference is | |
| 523 | between a function that returns a result, like addition, and a | |
| 524 | function that causes a side-effect, like \code{print}. We
 | |
| 525 | shall come back to this point later, but if you are curious | |
| 526 | now, the latter kind of functions always has \code{Unit} as
 | |
| 188 | 527 | return type. It is just not printed by Scala. | 
| 123 | 528 | |
| 181 | 529 | You can try more examples with the Scala REPL, but feel free to | 
| 530 | first guess what the result is (not all answers by Scala are obvious): | |
| 123 | 531 | |
| 532 | \begin{lstlisting}[numbers=none]
 | |
| 533 | scala> 2 + 2 | |
| 534 | scala> 1 / 2 | |
| 535 | scala> 1.0 / 2 | |
| 536 | scala> 1 / 2.0 | |
| 537 | scala> 1 / 0 | |
| 538 | scala> 1.0 / 0.0 | |
| 539 | scala> true == false | |
| 540 | scala> true && false | |
| 541 | scala> 1 > 1.0 | |
| 542 | scala> "12345".length | |
| 181 | 543 | scala> List(1,2,1).size | 
| 544 | scala> Set(1,2,1).size | |
| 265 | 545 | scala> List(1) == List(1) | 
| 546 | scala> Array(1) == Array(1) | |
| 547 | scala> Array(1).sameElements(Array(1)) | |
| 335 | 548 | \end{lstlisting}
 | 
| 549 | ||
| 550 | \noindent | |
| 551 | Also observe carefully what Scala responds in the following | |
| 552 | three instances involving the constant \lstinline!1!---can | |
| 553 | you explain the differences? | |
| 554 | ||
| 555 | ||
| 556 | \begin{lstlisting}[numbers=none]
 | |
| 557 | scala> 1 | |
| 558 | scala> 1L | |
| 559 | scala> 1F | |
| 181 | 560 | \end{lstlisting}\smallskip
 | 
| 123 | 561 | |
| 335 | 562 | |
| 563 | ||
| 181 | 564 | \noindent | 
| 565 | Please take the Scala REPL seriously: If you want to take advantage of my | |
| 566 | reference implementation for the assignments, you will need to be | |
| 567 | able to ``play around'' with it! | |
| 568 | ||
| 569 | \subsection*{Standalone Scala Apps}
 | |
| 123 | 570 | |
| 277 | 571 | If you want to write a standalone app in Scala, you can | 
| 197 | 572 | implement an object that is an instance of \code{App}. For example
 | 
| 573 | write | |
| 123 | 574 | |
| 575 | \begin{lstlisting}[numbers=none]
 | |
| 576 | object Hello extends App {
 | |
| 577 |     println("hello world")
 | |
| 578 | } | |
| 579 | \end{lstlisting}
 | |
| 580 | ||
| 197 | 581 | \noindent save it in a file, say {\tt hello-world.scala}, and
 | 
| 188 | 582 | then run the compiler (\texttt{scalac}) and start the runtime
 | 
| 181 | 583 | environment (\texttt{scala}):
 | 
| 123 | 584 | |
| 585 | \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
 | |
| 586 | $ scalac hello-world.scala | |
| 587 | $ scala Hello | |
| 588 | hello world | |
| 589 | \end{lstlisting}
 | |
| 590 | ||
| 124 | 591 | \noindent | 
| 123 | 592 | Like Java, Scala targets the JVM and consequently | 
| 593 | Scala programs can also be executed by the bog-standard Java | |
| 594 | Runtime. This only requires the inclusion of {\tt
 | |
| 595 | scala-library.jar}, which on my computer can be done as | |
| 596 | follows: | |
| 597 | ||
| 598 | \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
 | |
| 599 | $ scalac hello-world.scala | |
| 600 | $ java -cp /usr/local/src/scala/lib/scala-library.jar:. Hello | |
| 601 | hello world | |
| 602 | \end{lstlisting}
 | |
| 603 | ||
| 604 | \noindent You might need to adapt the path to where you have | |
| 605 | installed Scala. | |
| 606 | ||
| 607 | \subsection*{Values}
 | |
| 608 | ||
| 124 | 609 | In the lectures I will try to avoid as much as possible the term | 
| 610 | \emph{variables} familiar from other programming languages. The reason
 | |
| 611 | is that Scala has \emph{values}, which can be seen as abbreviations of
 | |
| 271 | 612 | larger expressions. The keyword for defining values is \code{val}.
 | 
| 613 | For example | |
| 123 | 614 | |
| 615 | \begin{lstlisting}[numbers=none]
 | |
| 616 | scala> val x = 42 | |
| 617 | x: Int = 42 | |
| 618 | ||
| 619 | scala> val y = 3 + 4 | |
| 620 | y: Int = 7 | |
| 621 | ||
| 622 | scala> val z = x / y | |
| 623 | z: Int = 6 | |
| 624 | \end{lstlisting}
 | |
| 625 | ||
| 626 | \noindent | |
| 272 | 627 | As can be seen, we first define \code{x} and {y} with admittedly some silly
 | 
| 271 | 628 | expressions, and then reuse these values in the definition of \code{z}.
 | 
| 272 | 629 | All easy, right? Why the kerfuffle about values? Well, values are | 
| 271 | 630 | \emph{immutable}. You cannot change their value after you defined them.
 | 
| 631 | If you try to reassign \code{z} above, Scala will yell at you:
 | |
| 123 | 632 | |
| 633 | \begin{lstlisting}[numbers=none]
 | |
| 634 | scala> z = 9 | |
| 635 | error: reassignment to val | |
| 636 | z = 9 | |
| 637 | ^ | |
| 638 | \end{lstlisting}
 | |
| 639 | ||
| 640 | \noindent | |
| 641 | So it would be a bit absurd to call values as variables...you cannot | |
| 195 | 642 | change them; they cannot vary. You might think you can reassign them like | 
| 123 | 643 | |
| 644 | \begin{lstlisting}[numbers=none]
 | |
| 645 | scala> val x = 42 | |
| 646 | scala> val z = x / 7 | |
| 647 | scala> val x = 70 | |
| 648 | scala> println(z) | |
| 649 | \end{lstlisting}
 | |
| 650 | ||
| 124 | 651 | \noindent but try to guess what Scala will print out | 
| 123 | 652 | for \code{z}?  Will it be \code{6} or \code{10}? A final word about
 | 
| 653 | values: Try to stick to the convention that names of values should be | |
| 188 | 654 | lower case, like \code{x}, \code{y}, \code{foo41} and so on. Upper-case
 | 
| 271 | 655 | names you should reserve for what is called \emph{constructors}. And 
 | 
| 656 | forgive me when I call values as variables\ldots{}it is just something that
 | |
| 657 | has been in imprinted into my developer-DNA during my early days and | |
| 272 | 658 | is difficult to get rid of.~\texttt{;o)}  
 | 
| 123 | 659 | |
| 660 | ||
| 661 | \subsection*{Function Definitions}
 | |
| 662 | ||
| 181 | 663 | We do functional programming! So defining functions will be our main occupation. | 
| 182 | 664 | As an example, a function named \code{f} taking a single argument of type 
 | 
| 181 | 665 | \code{Int} can be defined in Scala as follows:
 | 
| 123 | 666 | |
| 667 | \begin{lstlisting}[numbers=none]
 | |
| 181 | 668 | def f(x: Int) : String = ...EXPR... | 
| 123 | 669 | \end{lstlisting} 
 | 
| 670 | ||
| 671 | \noindent | |
| 124 | 672 | This function returns the value resulting from evaluating the expression | 
| 271 | 673 | \code{EXPR} (whatever is substituted for this). Since we declared
 | 
| 674 | \code{String}, the result of this function will be of type
 | |
| 675 | \code{String}. It is a good habit to always include this information
 | |
| 272 | 676 | about the return type, while it is only strictly necessary to give this | 
| 677 | type in recursive functions. Simple examples of Scala functions are: | |
| 123 | 678 | |
| 679 | \begin{lstlisting}[numbers=none]
 | |
| 680 | def incr(x: Int) : Int = x + 1 | |
| 681 | def double(x: Int) : Int = x + x | |
| 682 | def square(x: Int) : Int = x * x | |
| 683 | \end{lstlisting}
 | |
| 684 | ||
| 685 | \noindent | |
| 686 | The general scheme for a function is | |
| 687 | ||
| 688 | \begin{lstlisting}[numbers=none]
 | |
| 689 | def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = {
 | |
| 271 | 690 | ...BODY... | 
| 123 | 691 | } | 
| 692 | \end{lstlisting}
 | |
| 693 | ||
| 694 | \noindent | |
| 197 | 695 | where each argument, \texttt{arg1}, \texttt{arg2} and so on, requires 
 | 
| 696 | its type and the result type of the | |
| 697 | function, \code{rty}, should also be given. If the body of the function is
 | |
| 124 | 698 | more complex, then it can be enclosed in braces, like above. If it it | 
| 699 | is just a simple expression, like \code{x + 1}, you can omit the
 | |
| 195 | 700 | braces. Very often functions are recursive (that is call themselves), | 
| 701 | like the venerable factorial function: | |
| 123 | 702 | |
| 703 | \begin{lstlisting}[numbers=none]
 | |
| 271 | 704 | def fact(n: Int) : Int = | 
| 123 | 705 | if (n == 0) 1 else n * fact(n - 1) | 
| 706 | \end{lstlisting}
 | |
| 188 | 707 | |
| 708 | \noindent | |
| 272 | 709 | We could also have written this with braces as | 
| 271 | 710 | |
| 711 | \begin{lstlisting}[numbers=none]
 | |
| 712 | def fact(n: Int) : Int = {
 | |
| 713 | if (n == 0) 1 | |
| 714 | else n * fact(n - 1) | |
| 715 | } | |
| 716 | \end{lstlisting}
 | |
| 717 | ||
| 718 | \noindent | |
| 272 | 719 | but this seems a bit overkill for a small function like \code{fact}.
 | 
| 301 | 720 | Note that Scala does not have a \code{then}-keyword in an
 | 
| 335 | 721 | \code{if}-statement. Also important is that there should be always an
 | 
| 722 | \code{else}-branch. Never write an \code{if} without an \code{else},
 | |
| 723 | unless you know what you are doing! While \code{def} is the main
 | |
| 724 | mechanism for defining functions, there are a few other ways for doing | |
| 725 | this. We will see some of them in the next sections. | |
| 272 | 726 | |
| 727 | Before we go on, let me explain one tricky point in function | |
| 335 | 728 | definitions, especially in larger definitions. What does a Scala | 
| 729 | function return as result? Scala has a \code{return} keyword, but it is
 | |
| 272 | 730 | used for something different than in Java (and C/C++). Therefore please | 
| 731 | make sure no \code{return} slips into your Scala code.
 | |
| 732 | ||
| 733 | So in the absence of \code{return}, what value does a Scala function
 | |
| 734 | actually produce? A rule-of-thumb is whatever is in the last line of the | |
| 735 | function is the value that will be returned. Consider the following | |
| 736 | example:\footnote{We could have written this function in just one line,
 | |
| 737 | but for the sake of argument lets keep the two intermediate values.} | |
| 738 | ||
| 739 | \begin{lstlisting}[numbers=none]
 | |
| 277 | 740 | def average(xs: List[Int]) : Int = {
 | 
| 272 | 741 | val s = xs.sum | 
| 742 | val n = xs.length | |
| 743 | s / n | |
| 744 | } | |
| 745 | \end{lstlisting}
 | |
| 746 | ||
| 747 | \noindent In this example the expression \code{s / n} is in the last
 | |
| 748 | line of the function---so this will be the result the function | |
| 749 | calculates. The two lines before just calculate intermediate values. | |
| 335 | 750 | This principle of the ``last-line'' comes in handy when you need to | 
| 751 | print out values, for example, for debugging purposes. Suppose you want | |
| 272 | 752 | rewrite the function as | 
| 753 | ||
| 754 | \begin{lstlisting}[numbers=none]
 | |
| 277 | 755 | def average(xs: List[Int]) : Int = {
 | 
| 272 | 756 | val s = xs.sum | 
| 757 | val n = xs.length | |
| 758 | val h = xs.head | |
| 759 | println(s"Input $xs with first element $h") | |
| 760 | s / n | |
| 761 | } | |
| 762 | \end{lstlisting}
 | |
| 763 | ||
| 764 | \noindent | |
| 765 | Here the function still only returns the expression in the last line. | |
| 766 | The \code{println} before just prints out some information about the
 | |
| 767 | input of this function, but does not contribute to the result of the | |
| 768 | function. Similarly, the value \code{h} is used in the \code{println}
 | |
| 335 | 769 | but does not contribute to what integer is returned. | 
| 770 | ||
| 771 | A caveat is that the idea with the ``last line'' is only a rough | |
| 772 | rule-of-thumb. A better rule might be: the last expression that is | |
| 773 | evaluated in the function. Consider the following version of | |
| 774 | \code{average}:
 | |
| 272 | 775 | |
| 776 | \begin{lstlisting}[numbers=none]
 | |
| 277 | 777 | def average(xs: List[Int]) : Int = {
 | 
| 272 | 778 | if (xs.length == 0) 0 | 
| 779 | else xs.sum / xs.length | |
| 780 | } | |
| 781 | \end{lstlisting}
 | |
| 782 | ||
| 783 | \noindent | |
| 335 | 784 | What does this function return? Well there are two possibilities: either | 
| 785 | the result of \code{xs.sum / xs.length} in the last line provided the
 | |
| 786 | list \code{xs} is nonempty, \textbf{or} if the list is empty, then it
 | |
| 787 | will return \code{0} from the \code{if}-branch (which is technically not
 | |
| 788 | the last line, but the last expression evaluated by the function in the | |
| 272 | 789 | empty-case). | 
| 790 | ||
| 791 | Summing up, do not use \code{return} in your Scala code! A function
 | |
| 792 | returns what is evaluated by the function as the last expression. There | |
| 793 | is always only one such last expression. Previous expressions might | |
| 277 | 794 | calculate intermediate values, but they are not returned. If your | 
| 795 | function is supposed to return multiple things, then one way in Scala is | |
| 796 | to use tuples. For example returning the minimum, average and maximum | |
| 797 | can be achieved by | |
| 271 | 798 | |
| 277 | 799 | \begin{lstlisting}[numbers=none]
 | 
| 800 | def avr_minmax(xs: List[Int]) : (Int, Int, Int) = {
 | |
| 801 | if (xs.length == 0) (0, 0, 0) | |
| 802 | else (xs.min, xs.sum / xs.length, xs.max) | |
| 803 | } | |
| 804 | \end{lstlisting}
 | |
| 805 | ||
| 806 | \noindent | |
| 807 | which still satisfies the rule-of-thumb. | |
| 808 | ||
| 809 | ||
| 810 | \subsection*{Loops, or Better the Absence Thereof}
 | |
| 123 | 811 | |
| 272 | 812 | Coming from Java or C/C++, you might be surprised that Scala does | 
| 123 | 813 | not really have loops. It has instead, what is in functional | 
| 814 | programming called, \emph{maps}. To illustrate how they work,
 | |
| 815 | let us assume you have a list of numbers from 1 to 8 and want to | |
| 816 | build the list of squares. The list of numbers from 1 to 8 | |
| 817 | can be constructed in Scala as follows: | |
| 818 | ||
| 819 | \begin{lstlisting}[numbers=none]
 | |
| 820 | scala> (1 to 8).toList | |
| 821 | res1: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8) | |
| 822 | \end{lstlisting}
 | |
| 823 | ||
| 197 | 824 | \noindent Generating from this list the list of corresponding | 
| 825 | squares in a programming language such as Java, you would assume | |
| 826 | the list is given as a kind of array. You would then iterate, or loop, | |
| 123 | 827 | an index over this array and replace each entry in the array | 
| 828 | by the square. Right? In Scala, and in other functional | |
| 829 | programming languages, you use maps to achieve the same. | |
| 830 | ||
| 272 | 831 | A map essentially takes a function that describes how each element is | 
| 832 | transformed (in this example the function is $n \rightarrow n * n$) and | |
| 833 | a list over which this function should work. Pictorially you can think | |
| 834 | of the idea behind maps as follows: | |
| 835 | ||
| 836 | \begin{center}
 | |
| 837 | \begin{tikzpicture}
 | |
| 838 | ||
| 839 |   \node (A0) at (1.2,0) {\texttt{List(}};
 | |
| 840 |   \node (A1) at (2.0,0) {\texttt{1\makebox[0mm]{ ,}}};
 | |
| 841 |   \node (A2) at (2.9,0) {\texttt{2\makebox[0mm]{ ,}}};
 | |
| 842 |   \node (A3) at (3.8,0) {\texttt{3\makebox[0mm]{ ,}}};
 | |
| 843 |   \node (A4) at (4.7,0) {\texttt{4\makebox[0mm]{ ,}}};
 | |
| 844 |   \node (A5) at (5.6,0) {\texttt{5\makebox[0mm]{ ,}}};
 | |
| 845 |   \node (A6) at (6.5,0) {\texttt{6\makebox[0mm]{ ,}}};
 | |
| 846 |   \node (A7) at (7.4,0) {\texttt{7\makebox[0mm]{ ,}}};
 | |
| 847 |   \node (A8) at (8.3,0) {\texttt{8)}};
 | |
| 848 | ||
| 849 |   \node (B0) at (1.2,-3) {\texttt{List(}};
 | |
| 850 |   \node (B1) at (2.0,-3) {\texttt{1\makebox[0mm]{ ,}}};
 | |
| 851 |   \node (B2) at (3.0,-3) {\texttt{4\makebox[0mm]{ ,}}};
 | |
| 852 |   \node (B3) at (4.1,-3) {\texttt{9\makebox[0mm]{ ,}}};
 | |
| 853 |   \node (B4) at (5.2,-3) {\texttt{16\makebox[0mm]{ ,}}};
 | |
| 854 |   \node (B5) at (6.3,-3) {\texttt{25\makebox[0mm]{ ,}}};
 | |
| 855 |   \node (B6) at (7.4,-3) {\texttt{36\makebox[0mm]{ ,}}};
 | |
| 856 |   \node (B7) at (8.4,-3) {\texttt{49\makebox[0mm]{ ,}}};
 | |
| 857 |   \node (B8) at (9.4,-3) {\texttt{64\makebox[0mm]{ )}}};
 | |
| 858 | ||
| 859 | \draw [->,line width=1mm] (A1.south) -- (B1.north); | |
| 860 | \draw [->,line width=1mm] (A2.south) -- (B2.north); | |
| 861 | \draw [->,line width=1mm] (A3.south) -- (B3.north); | |
| 862 | \draw [->,line width=1mm] (A4.south) -- (B4.north); | |
| 863 | \draw [->,line width=1mm] (A5.south) -- (B5.north); | |
| 864 | \draw [->,line width=1mm] (A6.south) -- (B6.north); | |
| 865 | \draw [->,line width=1mm] (A7.south) -- (B7.north); | |
| 866 | \draw [->,line width=1mm] (A8.south) -- (B8.north); | |
| 867 | ||
| 277 | 868 |   \node [red] (Q0) at (-0.3,-0.3) {\large\texttt{n}}; 
 | 
| 869 |   \node (Q1) at (-0.3,-0.4) {};
 | |
| 870 |   \node (Q2) at (-0.3,-2.5) {};
 | |
| 871 |   \node [red] (Q3) at (-0.3,-2.65) {\large\texttt{n\,*\,n}};
 | |
| 272 | 872 | \draw [->,red,line width=1mm] (Q1.south) -- (Q2.north); | 
| 873 | ||
| 874 |   \node [red] at (-1.3,-1.5) {\huge{}\it\textbf{map}};
 | |
| 875 |  \end{tikzpicture}
 | |
| 876 | \end{center}
 | |
| 877 | ||
| 878 | \noindent | |
| 879 | On top is the ``input'' list we want to transform; on the left is the | |
| 880 | ``map'' function for how to transform each element in the input list | |
| 881 | (the square function in this case); at the bottom is the result list of | |
| 277 | 882 | the map. This means that a map generates a \emph{new} list, unlike a
 | 
| 273 | 883 | for-loop in Java or C/C++ which would most likely just update the | 
| 277 | 884 | existing list/array. | 
| 272 | 885 | |
| 277 | 886 | Now there are two ways for expressing such maps in Scala. The first way is | 
| 272 | 887 | called a \emph{for-comprehension}. The keywords are \code{for} and
 | 
| 888 | \code{yield}. Squaring the numbers from 1 to 8 with a for-comprehension
 | |
| 123 | 889 | would look as follows: | 
| 890 | ||
| 891 | \begin{lstlisting}[numbers=none]
 | |
| 892 | scala> for (n <- (1 to 8).toList) yield n * n | |
| 893 | res2: List[Int] = List(1, 4, 9, 16, 25, 36, 49, 64) | |
| 894 | \end{lstlisting}
 | |
| 895 | ||
| 272 | 896 | \noindent This for-comprehension states that from the list of numbers | 
| 277 | 897 | we draw some elements. We use the name \code{n} to range over these
 | 
| 898 | elements (whereby the name is arbitrary; we could use something more | |
| 899 | descriptive if we wanted to). Using \code{n} we compute the result of
 | |
| 900 | \code{n * n} after the \code{yield}. This way of writing a map resembles
 | |
| 901 | a bit the for-loops from imperative languages, even though the ideas | |
| 902 | behind for-loops and for-comprehensions are quite different. Also, this | |
| 903 | is a simple example---what comes after \code{yield} can be a complex
 | |
| 904 | expression enclosed in \texttt{\{...\}}. A more complicated example
 | |
| 905 | might be | |
| 272 | 906 | |
| 907 | \begin{lstlisting}[numbers=none]
 | |
| 908 | scala> for (n <- (1 to 8).toList) yield {
 | |
| 909 | val i = n + 1 | |
| 910 | val j = n - 1 | |
| 273 | 911 | i * j + 1 | 
| 272 | 912 | } | 
| 273 | 913 | res3: List[Int] = List(1, 4, 9, 16, 25, 36, 49, 64) | 
| 272 | 914 | \end{lstlisting}
 | 
| 915 | ||
| 916 | As you can see in for-comprehensions above, we specified the list where | |
| 917 | each \code{n} comes from, namely \code{(1 to 8).toList}, and how each
 | |
| 918 | element needs to be transformed. This can also be expressed in a second | |
| 919 | way in Scala by using directly the function \code{map} as follows:
 | |
| 123 | 920 | |
| 921 | \begin{lstlisting}[numbers=none]
 | |
| 922 | scala> (1 to 8).toList.map(n => n * n) | |
| 923 | res3 = List(1, 4, 9, 16, 25, 36, 49, 64) | |
| 924 | \end{lstlisting}
 | |
| 925 | ||
| 272 | 926 | \noindent In this way, the expression \code{n => n * n} stands for the
 | 
| 927 | function that calculates the square (this is how the \code{n}s are
 | |
| 928 | transformed by the map). It might not be obvious, but | |
| 277 | 929 | the for-comprehensions above are just syntactic sugar: when compiling such | 
| 273 | 930 | code, Scala translates for-comprehensions into equivalent maps. This | 
| 931 | even works when for-comprehensions get more complicated (see below). | |
| 123 | 932 | |
| 933 | The very charming feature of Scala is that such maps or | |
| 272 | 934 | for-comprehensions can be written for any kind of data collection, such | 
| 935 | as lists, sets, vectors, options and so on. For example if we instead | |
| 936 | compute the remainders modulo 3 of this list, we can write | |
| 123 | 937 | |
| 938 | \begin{lstlisting}[numbers=none]
 | |
| 939 | scala> (1 to 8).toList.map(n => n % 3) | |
| 940 | res4 = List(1, 2, 0, 1, 2, 0, 1, 2) | |
| 941 | \end{lstlisting}
 | |
| 942 | ||
| 943 | \noindent If we, however, transform the numbers 1 to 8 not | |
| 270 
38e13601cb1b
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 944 | into a list, but into a set, and then compute the remainders | 
| 123 | 945 | modulo 3 we obtain | 
| 946 | ||
| 947 | \begin{lstlisting}[numbers=none]
 | |
| 948 | scala> (1 to 8).toSet[Int].map(n => n % 3) | |
| 949 | res5 = Set(2, 1, 0) | |
| 950 | \end{lstlisting}
 | |
| 951 | ||
| 301 | 952 | \noindent This\footnote{This returns actually \code{HashSet(2, 1, 3)},
 | 
| 953 | but this is just an implementation detail of how sets are implemented in | |
| 954 | Scala.} is the correct result for sets, as there are only three | |
| 955 | equivalence classes of integers modulo 3. Note that in this example we | |
| 956 | need to ``help'' Scala to transform the numbers into a set of integers | |
| 957 | by explicitly annotating the type \code{Int}. Since maps and
 | |
| 958 | for-comprehensions are just syntactic variants of each other, the latter | |
| 959 | can also be written as | |
| 123 | 960 | |
| 961 | \begin{lstlisting}[numbers=none]
 | |
| 962 | scala> for (n <- (1 to 8).toSet[Int]) yield n % 3 | |
| 963 | res5 = Set(2, 1, 0) | |
| 964 | \end{lstlisting}
 | |
| 965 | ||
| 966 | For-comprehensions can also be nested and the selection of | |
| 967 | elements can be guarded. For example if we want to pair up | |
| 968 | the numbers 1 to 4 with the letters a to c, we can write | |
| 969 | ||
| 970 | \begin{lstlisting}[numbers=none]
 | |
| 971 | scala> for (n <- (1 to 4).toList; | |
| 972 |             m <- ('a' to 'c').toList) yield (n, m)
 | |
| 973 | res6 = List((1,a), (1,b), (1,c), (2,a), (2,b), (2,c), | |
| 974 | (3,a), (3,b), (3,c), (4,a), (4,b), (4,c)) | |
| 975 | \end{lstlisting}
 | |
| 976 | ||
| 977 | \noindent | |
| 272 | 978 | In this example the for-comprehension ranges over two lists, and | 
| 277 | 979 | produces a list of pairs as output. Or, if we want to find all pairs of | 
| 272 | 980 | numbers between 1 and 3 where the sum is an even number, we can write | 
| 123 | 981 | |
| 982 | \begin{lstlisting}[numbers=none]
 | |
| 983 | scala> for (n <- (1 to 3).toList; | |
| 984 | m <- (1 to 3).toList; | |
| 985 | if (n + m) % 2 == 0) yield (n, m) | |
| 986 | res7 = List((1,1), (1,3), (2,2), (3,1), (3,3)) | |
| 987 | \end{lstlisting}
 | |
| 988 | ||
| 272 | 989 | \noindent The \code{if}-condition in this for-comprehension filters out
 | 
| 277 | 990 | all pairs where the sum is not even (therefore \code{(1, 2)}, \code{(2,
 | 
| 991 | 1)} and \code{(3, 2)} are not in the result because their sum is odd). 
 | |
| 272 | 992 | |
| 278 | 993 | To summarise, maps (or for-comprehensions) transform one collection into | 
| 273 | 994 | another. For example a list of \code{Int}s into a list of squares, and
 | 
| 995 | so on. There is no need for for-loops in Scala. But please do not be | |
| 996 | tempted to write anything like | |
| 272 | 997 | |
| 998 | \begin{lstlisting}[numbers=none]
 | |
| 999 | scala> val cs = ('a' to 'h').toList
 | |
| 1000 | scala> for (n <- (0 until cs.length).toList) | |
| 1001 | yield cs(n).capitalize | |
| 1002 | res8: List[Char] = List(A, B, C, D, E, F, G, H) | |
| 1003 | \end{lstlisting}
 | |
| 1004 | ||
| 1005 | \noindent | |
| 277 | 1006 | This is accepted Scala-code, but utterly bad style (it is more like | 
| 1007 | Java). It can be written much clearer as: | |
| 272 | 1008 | |
| 1009 | \begin{lstlisting}[numbers=none]
 | |
| 1010 | scala> val cs = ('a' to 'h').toList
 | |
| 1011 | scala> for (c <- cs) yield c.capitalize | |
| 1012 | res9: List[Char] = List(A, B, C, D, E, F, G, H) | |
| 1013 | \end{lstlisting}
 | |
| 123 | 1014 | |
| 271 | 1015 | \subsection*{Results and Side-Effects}
 | 
| 1016 | ||
| 301 | 1017 | While hopefully all this about maps looks reasonable, there is one | 
| 273 | 1018 | complication: In the examples above we always wanted to transform one | 
| 1019 | list into another list (e.g.~list of squares), or one set into another | |
| 1020 | set (set of numbers into set of remainders modulo 3). What happens if we | |
| 1021 | just want to print out a list of integers? In these cases the | |
| 1022 | for-comprehensions need to be modified. The reason is that \code{print},
 | |
| 1023 | you guessed it, does not produce any result, but only produces what is | |
| 1024 | in the functional-programming-lingo called a \emph{side-effect}\ldots it
 | |
| 1025 | prints something out on the screen. Printing out the list of numbers | |
| 1026 | from 1 to 5 would look as follows | |
| 123 | 1027 | |
| 1028 | \begin{lstlisting}[numbers=none]
 | |
| 1029 | scala> for (n <- (1 to 5).toList) print(n) | |
| 1030 | 12345 | |
| 1031 | \end{lstlisting}
 | |
| 1032 | ||
| 1033 | \noindent | |
| 1034 | where you need to omit the keyword \code{yield}. You can
 | |
| 1035 | also do more elaborate calculations such as | |
| 1036 | ||
| 1037 | \begin{lstlisting}[numbers=none]
 | |
| 1038 | scala> for (n <- (1 to 5).toList) {
 | |
| 197 | 1039 | val square = n * n | 
| 1040 | println(s"$n * $n = $square") | |
| 123 | 1041 | } | 
| 1042 | 1 * 1 = 1 | |
| 1043 | 2 * 2 = 4 | |
| 1044 | 3 * 3 = 9 | |
| 1045 | 4 * 4 = 16 | |
| 1046 | 5 * 5 = 25 | |
| 1047 | \end{lstlisting}%$
 | |
| 1048 | ||
| 301 | 1049 | \noindent In this code I use a value assignment (\code{val
 | 
| 197 | 1050 | square = ...} ) and also what is called in Scala a | 
| 123 | 1051 | \emph{string interpolation}, written \code{s"..."}. The latter
 | 
| 1052 | is for printing out an equation. It allows me to refer to the | |
| 270 
38e13601cb1b
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1053 | integer values \code{n} and \code{square} inside a string.
 | 
| 123 | 1054 | This is very convenient for printing out ``things''. | 
| 1055 | ||
| 1056 | The corresponding map construction for functions with | |
| 1057 | side-effects is in Scala called \code{foreach}. So you 
 | |
| 1058 | could also write | |
| 1059 | ||
| 1060 | ||
| 1061 | \begin{lstlisting}[numbers=none]
 | |
| 1062 | scala> (1 to 5).toList.foreach(n => print(n)) | |
| 1063 | 12345 | |
| 1064 | \end{lstlisting}
 | |
| 1065 | ||
| 1066 | ||
| 1067 | \noindent or even just | |
| 1068 | ||
| 1069 | \begin{lstlisting}[numbers=none]
 | |
| 1070 | scala> (1 to 5).toList.foreach(print) | |
| 1071 | 12345 | |
| 1072 | \end{lstlisting}
 | |
| 1073 | ||
| 273 | 1074 | \noindent | 
| 123 | 1075 | If you want to find out more about maps and functions with | 
| 1076 | side-effects, you can ponder about the response Scala gives if | |
| 1077 | you replace \code{foreach} by \code{map} in the expression
 | |
| 1078 | above. Scala will still allow \code{map} with side-effect
 | |
| 1079 | functions, but then reacts with a slightly interesting result. | |
| 1080 | ||
| 273 | 1081 | \subsection*{Aggregates}
 | 
| 1082 | ||
| 1083 | There is one more usage of for-loops in Java, C/C++ and the like: | |
| 1084 | sometimes you want to \emph{aggregate} something about a list, for
 | |
| 278 | 1085 | example summing up all its elements. In this case you cannot use maps, | 
| 273 | 1086 | because maps \emph{transform} one data collection into another data
 | 
| 1087 | collection. They cannot be used to generate a single integer | |
| 278 | 1088 | representing an aggregate. So how is this kind of aggregation done in | 
| 1089 | Scala? Let us suppose you want to sum up all elements from a list. You | |
| 1090 | might be tempted to write something like | |
| 273 | 1091 | |
| 1092 | \begin{lstlisting}[numbers=none]
 | |
| 1093 | var cnt = 0 | |
| 1094 | for (n <- (1 to 8).toList) {
 | |
| 1095 | cnt += n | |
| 1096 | } | |
| 1097 | print(cnt) | |
| 1098 | \end{lstlisting}
 | |
| 1099 | ||
| 1100 | \noindent | |
| 277 | 1101 | and indeed this is accepted Scala code and produces the expected result, | 
| 273 | 1102 | namely \code{36}, \textbf{BUT} this is imperative style and not
 | 
| 301 | 1103 | permitted in PEP. If you submit this kind of code, you get 0 marks. The | 
| 1104 | code uses a \code{var} and therefore violates the immutability property
 | |
| 1105 | I ask for in your code. Sorry! | |
| 273 | 1106 | |
| 1107 | So how to do that same thing without using a \code{var}? Well there are
 | |
| 1108 | several ways. One way is to define the following recursive | |
| 1109 | \code{sum}-function:
 | |
| 1110 | ||
| 1111 | \begin{lstlisting}[numbers=none]
 | |
| 1112 | def sum(xs: List[Int]) : Int = | |
| 1113 | if (xs.isEmpty) 0 else xs.head + sum(xs.tail) | |
| 1114 | \end{lstlisting}  
 | |
| 1115 | ||
| 1116 | \noindent | |
| 1117 | You can then call \code{sum((1 to 8).toList)} and obtain the same result
 | |
| 278 | 1118 | without a mutable variable and without a for-loop. Obviously for simple things like | 
| 277 | 1119 | sum, you could have written \code{xs.sum} in the first place. But not
 | 
| 1120 | all aggregate functions are pre-defined and often you have to write your | |
| 1121 | own recursive function for this. | |
| 273 | 1122 | |
| 352 | 1123 | %\subsection*{Always Produce a Result! No Exceptions!}
 | 
| 329 | 1124 | % | 
| 1125 | %Function should always produce a value. Exception is not thrown. | |
| 1126 | %Whenever there is a possibility of non-value result (exception, void, | |
| 1127 | %undefined, null, etc.), it should be incorporated in the result type. | |
| 1128 | %Such types include but not limited to | |
| 1129 | % | |
| 1130 | %Option[T] | |
| 1131 | ||
| 352 | 1132 | %TBD | 
| 334 | 1133 | |
| 329 | 1134 | |
| 271 | 1135 | \subsection*{Higher-Order Functions}
 | 
| 1136 | ||
| 301 | 1137 | Functions obviously play a central role in functional programming. Two simple | 
| 1138 | examples are | |
| 1139 | ||
| 1140 | \begin{lstlisting}[numbers=none]
 | |
| 1141 | def even(x: Int) : Boolean = x % 2 == 0 | |
| 1142 | def odd(x: Int) : Boolean = x % 2 == 1 | |
| 1143 | \end{lstlisting} 
 | |
| 1144 | ||
| 1145 | \noindent | |
| 1146 | More interestingly, the concept of functions is really pushed to the | |
| 1147 | limit in functional programming. Functions can take other functions as | |
| 1148 | arguments and can return a function as a result. This is actually | |
| 1149 | quite important for making code generic. Assume a list of 10 elements: | |
| 1150 | ||
| 1151 | \begin{lstlisting}[numbers=none]
 | |
| 1152 | val lst = (1 to 10).toList | |
| 1153 | \end{lstlisting} 
 | |
| 1154 | ||
| 1155 | \noindent | |
| 1156 | Say, we want to filter out all even numbers. For this we can use | |
| 1157 | ||
| 1158 | \begin{lstlisting}[numbers=none]
 | |
| 1159 | scala> lst.filter(even) | |
| 1160 | List(2, 4, 6, 8, 10) | |
| 1161 | \end{lstlisting} 
 | |
| 1162 | ||
| 1163 | \noindent | |
| 1164 | where \code{filter} expects a function as argument specifying which
 | |
| 1165 | elements of the list should be kept and which should be left out. By | |
| 1166 | allowing \code{filter} to take a function as argument, we can also
 | |
| 1167 | easily filter out odd numbers as well. | |
| 1168 | ||
| 1169 | \begin{lstlisting}[numbers=none]
 | |
| 1170 | scala> lst.filter(odd) | |
| 1171 | List(1, 3, 5, 7, 9) | |
| 1172 | \end{lstlisting} 
 | |
| 1173 | ||
| 1174 | \noindent | |
| 1175 | Such function arguments are quite frequently used for ``generic'' functions. | |
| 1176 | For example it is easy to count odd elements in a list or find the first | |
| 1177 | even number in a list: | |
| 1178 | ||
| 1179 | \begin{lstlisting}[numbers=none]
 | |
| 1180 | scala> lst.count(odd) | |
| 1181 | 5 | |
| 1182 | scala> lst.find(even) | |
| 1183 | Some(2) | |
| 1184 | \end{lstlisting} 
 | |
| 1185 | ||
| 1186 | \noindent | |
| 1187 | Recall that the return type of \code{even} and \code{odd} are booleans.
 | |
| 1188 | Such function are sometimes called predicates, because they determine | |
| 1189 | what should be true for an element and what false, and then performing | |
| 1190 | some operation according to this boolean. Such predicates are quite useful. | |
| 1191 | Say you want to sort the \code{lst}-list in ascending and descending order. 
 | |
| 1192 | For this you can write | |
| 1193 | ||
| 1194 | \begin{lstlisting}[numbers=none]
 | |
| 1195 | lst.sortWith(_ < _) | |
| 1196 | lst.sortWith(_ > _) | |
| 1197 | \end{lstlisting} 
 | |
| 1198 | ||
| 1199 | \noindent where \code{sortWith} expects a predicate as argument. The
 | |
| 1200 | construction \code{_ < _} stands for a function that takes two arguments
 | |
| 1201 | and returns true when the first one is smaller than the second. You can | |
| 1202 | think of this as elegant shorthand notation for | |
| 1203 | ||
| 1204 | \begin{lstlisting}[numbers=none]
 | |
| 1205 | def smaller(x: Int, y: Int) : Boolean = x < y | |
| 1206 | lst.sortWith(smaller) | |
| 1207 | \end{lstlisting} 
 | |
| 1208 | ||
| 1209 | \noindent | |
| 1210 | Say you want to find in \code{lst} the first odd number greater than 2.
 | |
| 1211 | For this you need to write a function that specifies exactly this | |
| 1212 | condition. To do this you can use a slight variant of the shorthand | |
| 1213 | notation above | |
| 1214 | ||
| 1215 | \begin{lstlisting}[numbers=none]
 | |
| 1216 | scala> lst.find(n => odd(n) && n > 2) | |
| 1217 | Some(3) | |
| 1218 | \end{lstlisting} 
 | |
| 1219 | ||
| 1220 | \noindent | |
| 1221 | Here \code{n => ...} specifies a function that takes \code{n} as
 | |
| 1222 | argument and uses this argument in whatever comes after the double | |
| 1223 | arrow. If you want to use this mechanism for looking for an element that | |
| 1224 | is both even and odd, then of course you out of luck. | |
| 1225 | ||
| 1226 | \begin{lstlisting}[numbers=none]
 | |
| 1227 | scala> lst.find(n => odd(n) && even(n)) | |
| 1228 | None | |
| 1229 | \end{lstlisting} 
 | |
| 1230 | ||
| 1231 | While functions taking functions as arguments seems a rather useful | |
| 1232 | feature, the utility of returning a function might not be so clear. | |
| 1233 | I admit the following example is a bit contrived, but believe me | |
| 1234 | sometims functions produce other functions in a very meaningful way. | |
| 1235 | Say we want to generate functions according to strings, as in | |
| 1236 | ||
| 1237 | \begin{lstlisting}[numbers=none]
 | |
| 1238 | def mkfn(s: String) : (Int => Boolean) = | |
| 1239 | if (s == "even") even else odd | |
| 1240 | \end{lstlisting}
 | |
| 1241 | ||
| 1242 | \noindent | |
| 1243 | With this we can generate the required function for \code{filter}
 | |
| 1244 | according to a string: | |
| 1245 | ||
| 1246 | \begin{lstlisting}[numbers=none]
 | |
| 1247 | scala> lst.filter(mkfn("even"))
 | |
| 1248 | List(2, 4, 6, 8, 10) | |
| 1249 | scala> lst.filter(mkfn("foo"))
 | |
| 1250 | List(1, 3, 5, 7, 9) | |
| 1251 | \end{lstlisting}
 | |
| 1252 | ||
| 1253 | \noindent | |
| 1254 | As said, this is example is a bit contrived---I was not able to think | |
| 1255 | of anything simple, but for example in the Compiler module next year I | |
| 1256 | show a compilation functions that needs to generate functions as | |
| 1257 | intermediate result. Anyway, notice the interesting type we had to | |
| 1258 | annotate to \code{mkfn}. Types of Scala are described next.
 | |
| 1259 | ||
| 274 | 1260 | |
| 123 | 1261 | \subsection*{Types}
 | 
| 1262 | ||
| 1263 | In most functional programming languages, types play an | |
| 1264 | important role. Scala is such a language. You have already | |
| 1265 | seen built-in types, like \code{Int}, \code{Boolean},
 | |
| 1266 | \code{String} and \code{BigInt}, but also user-defined ones,
 | |
| 195 | 1267 | like \code{Rexp} (see coursework). Unfortunately, types can be a thorny
 | 
| 123 | 1268 | subject, especially in Scala. For example, why do we need to | 
| 1269 | give the type to \code{toSet[Int]}, but not to \code{toList}?
 | |
| 1270 | The reason is the power of Scala, which sometimes means it | |
| 1271 | cannot infer all necessary typing information. At the | |
| 195 | 1272 | beginning, while getting familiar with Scala, I recommend a | 
| 123 | 1273 | ``play-it-by-ear-approach'' to types. Fully understanding | 
| 1274 | type-systems, especially complicated ones like in Scala, can | |
| 1275 | take a module on their own.\footnote{Still, such a study can
 | |
| 1276 | be a rewarding training: If you are in the business of | |
| 1277 | designing new programming languages, you will not be able to | |
| 1278 | turn a blind eye to types. They essentially help programmers | |
| 1279 | to avoid common programming errors and help with maintaining | |
| 1280 | code.} | |
| 1281 | ||
| 1282 | In Scala, types are needed whenever you define an inductive | |
| 1283 | datatype and also whenever you define functions (their | |
| 1284 | arguments and their results need a type). Base types are types | |
| 1285 | that do not take any (type)arguments, for example \code{Int}
 | |
| 1286 | and \code{String}. Compound types take one or more arguments,
 | |
| 1287 | which as seen earlier need to be given in angle-brackets, for | |
| 1288 | example \code{List[Int]} or \code{Set[List[String]]} or 
 | |
| 1289 | \code{Map[Int, Int]}.
 | |
| 1290 | ||
| 1291 | There are a few special type-constructors that fall outside | |
| 1292 | this pattern. One is for tuples, where the type is written | |
| 1293 | with parentheses. For example | |
| 1294 | ||
| 1295 | \begin{lstlisting}[ numbers=none]
 | |
| 1296 | (Int, Int, String) | |
| 1297 | \end{lstlisting}
 | |
| 1298 | ||
| 1299 | \noindent is for a triple (a tuple with three components---two | |
| 1300 | integers and a string). Tuples are helpful if you want to | |
| 1301 | define functions with multiple results, say the function | |
| 270 
38e13601cb1b
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1302 | returning the quotient and remainder of two numbers. For this | 
| 123 | 1303 | you might define: | 
| 1304 | ||
| 1305 | ||
| 1306 | \begin{lstlisting}[ numbers=none]
 | |
| 301 | 1307 | def quo_rem(m: Int, n: Int) : (Int, Int) = | 
| 1308 | (m / n, m % n) | |
| 123 | 1309 | \end{lstlisting}
 | 
| 1310 | ||
| 1311 | \noindent Since this function returns a pair of integers, its | |
| 277 | 1312 | \emph{return type} needs to be of type \code{(Int, Int)}. Incidentally,
 | 
| 1313 | this is also the \emph{input type} of this function. For this notice
 | |
| 1314 | \code{quo_rem} takes \emph{two} arguments, namely \code{m} and \code{n},
 | |
| 1315 | both of which are integers. They are ``packaged'' in a pair. | |
| 1316 | Consequently the complete type of \code{quo_rem} is
 | |
| 123 | 1317 | |
| 1318 | \begin{lstlisting}[ numbers=none]
 | |
| 1319 | (Int, Int) => (Int, Int) | |
| 1320 | \end{lstlisting}
 | |
| 1321 | ||
| 301 | 1322 | \noindent | 
| 277 | 1323 | This uses another special type-constructor, written as the arrow | 
| 301 | 1324 | \code{=>}. This is sometimes also called \emph{function arrow}.  For
 | 
| 1325 | example, the type \code{Int => String} is for a function that takes an
 | |
| 1326 | integer as input argument and produces a string as result. A function | |
| 1327 | of this type is for instance | |
| 123 | 1328 | |
| 1329 | \begin{lstlisting}[numbers=none]
 | |
| 1330 | def mk_string(n: Int) : String = n match {
 | |
| 1331 | case 0 => "zero" | |
| 1332 | case 1 => "one" | |
| 1333 | case 2 => "two" | |
| 1334 | case _ => "many" | |
| 1335 | } | |
| 1336 | \end{lstlisting}
 | |
| 1337 | ||
| 1338 | \noindent It takes an integer as input argument and returns a | |
| 301 | 1339 | string. The type of the function generated in \code{mkfn} above, is
 | 
| 1340 | \code{Int => Boolean}.
 | |
| 277 | 1341 | |
| 1342 | Unfortunately, unlike other functional programming languages, there is | |
| 1343 | in Scala no easy way to find out the types of existing functions, except | |
| 1344 | by looking into the documentation | |
| 123 | 1345 | |
| 1346 | \begin{quote}
 | |
| 1347 | \url{http://www.scala-lang.org/api/current/}
 | |
| 1348 | \end{quote}
 | |
| 1349 | ||
| 1350 | The function arrow can also be iterated, as in | |
| 1351 | \code{Int => String => Boolean}. This is the type for a function
 | |
| 1352 | taking an integer as first argument and a string as second, | |
| 1353 | and the result of the function is a boolean. Though silly, a | |
| 1354 | function of this type would be | |
| 1355 | ||
| 1356 | ||
| 1357 | \begin{lstlisting}[numbers=none]
 | |
| 1358 | def chk_string(n: Int)(s: String) : Boolean = | |
| 1359 | mk_string(n) == s | |
| 1360 | \end{lstlisting}
 | |
| 1361 | ||
| 1362 | ||
| 1363 | \noindent which checks whether the integer \code{n}
 | |
| 1364 | corresponds to the name \code{s} given by the function
 | |
| 1365 | \code{mk\_string}. Notice the unusual way of specifying the
 | |
| 1366 | arguments of this function: the arguments are given one after | |
| 1367 | the other, instead of being in a pair (what would be the type | |
| 1368 | of this function then?). This way of specifying the arguments | |
| 1369 | can be useful, for example in situations like this | |
| 1370 | ||
| 1371 | \begin{lstlisting}[numbers=none]
 | |
| 1372 | scala> List("one", "two", "three", "many").map(chk_string(2))
 | |
| 1373 | res4 = List(false, true, false, false) | |
| 1374 | ||
| 1375 | scala> List("one", "two", "three", "many").map(chk_string(3))
 | |
| 1376 | res5 = List(false, false, false, true) | |
| 1377 | \end{lstlisting}
 | |
| 1378 | ||
| 1379 | \noindent In each case we can give to \code{map} a specialised
 | |
| 1380 | version of \code{chk_string}---once specialised to 2 and once
 | |
| 1381 | to 3. This kind of ``specialising'' a function is called | |
| 1382 | \emph{partial application}---we have not yet given to this
 | |
| 1383 | function all arguments it needs, but only some of them. | |
| 1384 | ||
| 1385 | Coming back to the type \code{Int => String => Boolean}. The
 | |
| 1386 | rule about such function types is that the right-most type | |
| 1387 | specifies what the function returns (a boolean in this case). | |
| 1388 | The types before that specify how many arguments the function | |
| 1389 | expects and what their type is (in this case two arguments, | |
| 1390 | one of type \code{Int} and another of type \code{String}).
 | |
| 1391 | Given this rule, what kind of function has type | |
| 1392 | \mbox{\code{(Int => String) => Boolean}}? Well, it returns a
 | |
| 1393 | boolean. More interestingly, though, it only takes a single | |
| 1394 | argument (because of the parentheses). The single argument | |
| 1395 | happens to be another function (taking an integer as input and | |
| 1396 | returning a string). Remember that \code{mk_string} is just 
 | |
| 1397 | such a function. So how can we use it? For this define | |
| 1398 | the somewhat silly function \code{apply_3}:
 | |
| 1399 | ||
| 1400 | \begin{lstlisting}[numbers=none]
 | |
| 1401 | def apply_3(f: Int => String): Bool = f(3) == "many" | |
| 1402 | ||
| 1403 | scala> apply_3(mk_string) | |
| 1404 | res6 = true | |
| 1405 | \end{lstlisting}
 | |
| 1406 | ||
| 1407 | You might ask: Apart from silly functions like above, what is | |
| 1408 | the point of having functions as input arguments to other | |
| 1409 | functions? In Java there is indeed no need of this kind of | |
| 1410 | feature: at least in the past it did not allow such | |
| 197 | 1411 | constructions. I think, the point of Java 8 and successors was to lift this | 
| 123 | 1412 | restriction. But in all functional programming languages, | 
| 1413 | including Scala, it is really essential to allow functions as | |
| 301 | 1414 | input argument. Above you have already seen \code{map} and
 | 
| 1415 | \code{foreach} which need this feature. Consider the functions
 | |
| 123 | 1416 | \code{print} and \code{println}, which both print out strings,
 | 
| 1417 | but the latter adds a line break. You can call \code{foreach}
 | |
| 1418 | with either of them and thus changing how, for example, five | |
| 1419 | numbers are printed. | |
| 1420 | ||
| 1421 | ||
| 1422 | \begin{lstlisting}[numbers=none]
 | |
| 1423 | scala> (1 to 5).toList.foreach(print) | |
| 1424 | 12345 | |
| 1425 | scala> (1 to 5).toList.foreach(println) | |
| 1426 | 1 | |
| 1427 | 2 | |
| 1428 | 3 | |
| 1429 | 4 | |
| 1430 | 5 | |
| 1431 | \end{lstlisting}
 | |
| 1432 | ||
| 1433 | ||
| 1434 | \noindent This is actually one of the main design principles | |
| 1435 | in functional programming. You have generic functions like | |
| 1436 | \code{map} and \code{foreach} that can traverse data containers,
 | |
| 1437 | like lists or sets. They then take a function to specify what | |
| 1438 | should be done with each element during the traversal. This | |
| 1439 | requires that the generic traversal functions can cope with | |
| 1440 | any kind of function (not just functions that, for example, | |
| 1441 | take as input an integer and produce a string like above). | |
| 1442 | This means we cannot fix the type of the generic traversal | |
| 1443 | functions, but have to keep them | |
| 181 | 1444 | \emph{polymorphic}.\footnote{Another interesting topic about
 | 
| 123 | 1445 | types, but we omit it here for the sake of brevity.} | 
| 1446 | ||
| 301 | 1447 | There is one more type constructor that is rather special. It is | 
| 1448 | called \code{Unit}. Recall that \code{Boolean} has two values, namely
 | |
| 1449 | \code{true} and \code{false}. This can be used, for example, to test
 | |
| 1450 | something and decide whether the test succeeds or not. In contrast the | |
| 1451 | type \code{Unit} has only a single value, written \code{()}. This
 | |
| 1452 | seems like a completely useless type and return value for a function, | |
| 1453 | but is actually quite useful. It indicates when the function does not | |
| 1454 | return any result. The purpose of these functions is to cause | |
| 1455 | something being written on the screen or written into a file, for | |
| 1456 | example. This is what is called they cause a \emph{side-effect}, for
 | |
| 1457 | example new content displayed on the screen or some new data in a | |
| 1458 | file. Scala uses the \code{Unit} type to indicate that a function does
 | |
| 1459 | not have a result, but potentially causes a side-effect. Typical | |
| 1460 | examples are the printing functions, like \code{print}.
 | |
| 123 | 1461 | |
| 301 | 1462 | |
| 1463 | %%\subsection*{User-Defined Types}
 | |
| 123 | 1464 | |
| 143 | 1465 | % \subsection*{Cool Stuff}
 | 
| 123 | 1466 | |
| 143 | 1467 | % The first wow-moment I had with Scala was when I came across | 
| 1468 | % the following code-snippet for reading a web-page. | |
| 123 | 1469 | |
| 1470 | ||
| 143 | 1471 | % \begin{lstlisting}[ numbers=none]
 | 
| 1472 | % import io.Source | |
| 1473 | % val url = """http://www.inf.kcl.ac.uk/staff/urbanc/""" | |
| 1474 | % Source.fromURL(url)("ISO-8859-1").take(10000).mkString
 | |
| 1475 | % \end{lstlisting}
 | |
| 123 | 1476 | |
| 1477 | ||
| 143 | 1478 | % \noindent These three lines return a string containing the | 
| 1479 | % HTML-code of my webpage. It actually already does something | |
| 1480 | % more sophisticated, namely only returns the first 10000 | |
| 1481 | % characters of a webpage in case it is too large. Why is that | |
| 1482 | % code-snippet of any interest? Well, try implementing | |
| 1483 | % reading-from-a-webpage in Java. I also like the possibility of | |
| 1484 | % triple-quoting strings, which I have only seen in Scala so | |
| 1485 | % far. The idea behind this is that in such a string all | |
| 1486 | % characters are interpreted literally---there are no escaped | |
| 1487 | % characters, like \verb|\n| for newlines. | |
| 123 | 1488 | |
| 143 | 1489 | % My second wow-moment I had with a feature of Scala that other | 
| 1490 | % functional programming languages do not have. This feature is | |
| 1491 | % about implicit type conversions. If you have regular | |
| 1492 | % expressions and want to use them for language processing you | |
| 1493 | % often want to recognise keywords in a language, for example | |
| 1494 | % \code{for},{} \code{if},{} \code{yield} and so on. But the
 | |
| 1495 | % basic regular expression \code{CHAR} can only recognise a
 | |
| 1496 | % single character. In order to recognise a whole string, like | |
| 1497 | % \code{for}, you have to put many of those together using
 | |
| 1498 | % \code{SEQ}:
 | |
| 123 | 1499 | |
| 1500 | ||
| 143 | 1501 | % \begin{lstlisting}[numbers=none]
 | 
| 1502 | % SEQ(CHAR('f'), SEQ(CHAR('o'), CHAR('r')))
 | |
| 1503 | % \end{lstlisting}
 | |
| 123 | 1504 | |
| 143 | 1505 | % \noindent This gets quickly unreadable when the strings and | 
| 1506 | % regular expressions get more complicated. In other functional | |
| 1507 | % programming languages, you can explicitly write a conversion | |
| 1508 | % function that takes a string, say \dq{\pcode{for}}, and
 | |
| 1509 | % generates the regular expression above. But then your code is | |
| 1510 | % littered with such conversion functions. | |
| 123 | 1511 | |
| 143 | 1512 | % In Scala you can do better by ``hiding'' the conversion | 
| 1513 | % functions. The keyword for doing this is \code{implicit} and
 | |
| 1514 | % it needs a built-in library called | |
| 123 | 1515 | |
| 143 | 1516 | % \begin{lstlisting}[numbers=none]
 | 
| 1517 | % scala.language.implicitConversions | |
| 1518 | % \end{lstlisting}
 | |
| 123 | 1519 | |
| 143 | 1520 | % \noindent | 
| 1521 | % Consider the code | |
| 123 | 1522 | |
| 1523 | ||
| 143 | 1524 | % \begin{lstlisting}[language=Scala]
 | 
| 1525 | % import scala.language.implicitConversions | |
| 123 | 1526 | |
| 143 | 1527 | % def charlist2rexp(s: List[Char]) : Rexp = s match {
 | 
| 1528 | % case Nil => EMPTY | |
| 1529 | % case c::Nil => CHAR(c) | |
| 1530 | % case c::s => SEQ(CHAR(c), charlist2rexp(s)) | |
| 1531 | % } | |
| 123 | 1532 | |
| 143 | 1533 | % implicit def string2rexp(s: String) : Rexp = | 
| 1534 | % charlist2rexp(s.toList) | |
| 1535 | % \end{lstlisting}
 | |
| 123 | 1536 | |
| 1537 | ||
| 143 | 1538 | % \noindent where the first seven lines implement a function | 
| 1539 | % that given a list of characters generates the corresponding | |
| 1540 | % regular expression. In Lines 9 and 10, this function is used | |
| 1541 | % for transforming a string into a regular expression. Since the | |
| 1542 | % \code{string2rexp}-function is declared as \code{implicit},
 | |
| 1543 | % the effect will be that whenever Scala expects a regular | |
| 1544 | % expression, but I only give it a string, it will automatically | |
| 1545 | % insert a call to the \code{string2rexp}-function. I can now
 | |
| 1546 | % write for example | |
| 123 | 1547 | |
| 143 | 1548 | % \begin{lstlisting}[numbers=none]
 | 
| 1549 | % scala> ALT("ab", "ac")
 | |
| 1550 | % res9 = ALT(SEQ(CHAR(a),CHAR(b)),SEQ(CHAR(a),CHAR(c))) | |
| 1551 | % \end{lstlisting}
 | |
| 123 | 1552 | |
| 143 | 1553 | % \noindent Recall that \code{ALT} expects two regular
 | 
| 1554 | % expressions as arguments, but I only supply two strings. The | |
| 1555 | % implicit conversion function will transform the string into a | |
| 1556 | % regular expression. | |
| 123 | 1557 | |
| 143 | 1558 | % Using implicit definitions, Scala allows me to introduce | 
| 1559 | % some further syntactic sugar for regular expressions: | |
| 123 | 1560 | |
| 1561 | ||
| 143 | 1562 | % \begin{lstlisting}[ numbers=none]
 | 
| 1563 | % implicit def RexpOps(r: Rexp) = new {
 | |
| 1564 | % def | (s: Rexp) = ALT(r, s) | |
| 1565 | % def ~ (s: Rexp) = SEQ(r, s) | |
| 1566 | % def % = STAR(r) | |
| 1567 | % } | |
| 123 | 1568 | |
| 143 | 1569 | % implicit def stringOps(s: String) = new {
 | 
| 1570 | % def | (r: Rexp) = ALT(s, r) | |
| 1571 | % def | (r: String) = ALT(s, r) | |
| 1572 | % def ~ (r: Rexp) = SEQ(s, r) | |
| 1573 | % def ~ (r: String) = SEQ(s, r) | |
| 1574 | % def % = STAR(s) | |
| 1575 | % } | |
| 1576 | % \end{lstlisting}
 | |
| 123 | 1577 | |
| 1578 | ||
| 143 | 1579 | % \noindent This might seem a bit overly complicated, but its effect is | 
| 1580 | % that I can now write regular expressions such as $ab + ac$ | |
| 1581 | % simply as | |
| 123 | 1582 | |
| 1583 | ||
| 143 | 1584 | % \begin{lstlisting}[numbers=none]
 | 
| 1585 | % scala> "ab" | "ac" | |
| 1586 | % res10 = ALT(SEQ(CHAR(a),CHAR(b)),SEQ(CHAR(a),CHAR(c))) | |
| 1587 | % \end{lstlisting}
 | |
| 123 | 1588 | |
| 1589 | ||
| 143 | 1590 | % \noindent I leave you to figure out what the other | 
| 1591 | % syntactic sugar in the code above stands for. | |
| 123 | 1592 | |
| 143 | 1593 | % One more useful feature of Scala is the ability to define | 
| 1594 | % functions with varying argument lists. This is a feature that | |
| 1595 | % is already present in old languages, like C, but seems to have | |
| 1596 | % been forgotten in the meantime---Java does not have it. In the | |
| 1597 | % context of regular expressions this feature comes in handy: | |
| 1598 | % Say you are fed up with writing many alternatives as | |
| 123 | 1599 | |
| 1600 | ||
| 143 | 1601 | % \begin{lstlisting}[numbers=none]
 | 
| 1602 | % ALT(..., ALT(..., ALT(..., ...))) | |
| 1603 | % \end{lstlisting}
 | |
| 123 | 1604 | |
| 1605 | ||
| 143 | 1606 | % \noindent To make it difficult, you do not know how deep such | 
| 1607 | % alternatives are nested. So you need something flexible that | |
| 1608 | % can take as many alternatives as needed. In Scala one can | |
| 1609 | % achieve this by adding a \code{*} to the type of an argument.
 | |
| 1610 | % Consider the code | |
| 123 | 1611 | |
| 1612 | ||
| 143 | 1613 | % \begin{lstlisting}[language=Scala]
 | 
| 1614 | % def Alts(rs: List[Rexp]) : Rexp = rs match {
 | |
| 1615 | % case Nil => NULL | |
| 1616 | % case r::Nil => r | |
| 1617 | % case r::rs => ALT(r, Alts(rs)) | |
| 1618 | % } | |
| 123 | 1619 | |
| 143 | 1620 | % def ALTS(rs: Rexp*) = Alts(rs.toList) | 
| 1621 | % \end{lstlisting}
 | |
| 123 | 1622 | |
| 1623 | ||
| 143 | 1624 | % \noindent The function in Lines 1 to 5 takes a list of regular | 
| 1625 | % expressions and converts it into an appropriate alternative | |
| 1626 | % regular expression. In Line 7 there is a wrapper for this | |
| 1627 | % function which uses the feature of varying argument lists. The | |
| 1628 | % effect of this code is that I can write the regular | |
| 1629 | % expression for keywords as | |
| 123 | 1630 | |
| 1631 | ||
| 143 | 1632 | % \begin{lstlisting}[numbers=none]
 | 
| 1633 | % ALTS("for", "def", "yield", "implicit", "if", "match", "case")
 | |
| 1634 | % \end{lstlisting}
 | |
| 123 | 1635 | |
| 1636 | ||
| 143 | 1637 | % \noindent Again I leave it to you to find out how much this | 
| 1638 | % simplifies the regular expression in comparison with if I had | |
| 1639 | % to write this by hand using only the ``plain'' regular | |
| 1640 | % expressions from the inductive datatype. | |
| 1641 | ||
| 197 | 1642 | %\bigskip\noindent | 
| 1643 | %\textit{More TBD.}
 | |
| 123 | 1644 | |
| 197 | 1645 | %\subsection*{Coursework}
 | 
| 181 | 1646 | |
| 395 | 1647 | \begin{figure}[p]
 | 
| 1648 | \begin{boxedminipage}{\textwidth}  
 | |
| 1649 | \textbf{Scala Syntax for Java Developers}\bigskip
 | |
| 195 | 1650 | |
| 395 | 1651 | \noindent | 
| 343 | 1652 | Scala compiles to the JVM, like the Java language. Because of this, | 
| 352 | 1653 | it can re-use many libraries. Here are a few hints how some Java code | 
| 1654 | tranlsates to Scala code:\bigskip | |
| 343 | 1655 | |
| 352 | 1656 | \noindent | 
| 1657 | Variable declaration: | |
| 343 | 1658 | \begin{lstlisting}[language=Java]
 | 
| 352 | 1659 | Drink coke = getCoke();/*!\annotation{Java}!*/
 | 
| 343 | 1660 | \end{lstlisting}
 | 
| 1661 | ||
| 1662 | \begin{lstlisting}[language=Scala]
 | |
| 352 | 1663 | val coke : Drink = getCoke()/*!\annotation{Scala}!*/
 | 
| 343 | 1664 | \end{lstlisting}
 | 
| 1665 | ||
| 352 | 1666 | \noindent | 
| 395 | 1667 | or even | 
| 1668 | ||
| 1669 | \begin{lstlisting}[language=Scala]
 | |
| 1670 | val coke = getCoke()/*!\annotation{Scala}!*/
 | |
| 1671 | \end{lstlisting}\bigskip
 | |
| 1672 | ||
| 1673 | \noindent | |
| 343 | 1674 | Unit means void: | 
| 1675 | ||
| 1676 | \begin{lstlisting}[language=Java]
 | |
| 352 | 1677 | public void output(String s) {/*!\annotation{Java}!*/
 | 
| 343 | 1678 | System.out.println(s); | 
| 1679 | } | |
| 1680 | \end{lstlisting}
 | |
| 1681 | ||
| 1682 | \begin{lstlisting}[language=Scala]
 | |
| 352 | 1683 | def output(s: String): Unit = println(s)/*!\annotation{Scala}!*/
 | 
| 395 | 1684 | \end{lstlisting}\bigskip
 | 
| 343 | 1685 | |
| 352 | 1686 | \noindent | 
| 343 | 1687 | Type for list of Strings: | 
| 1688 | ||
| 1689 | \begin{lstlisting}[language=Java]
 | |
| 352 | 1690 | List<String>/*!\annotation{Java}!*/
 | 
| 343 | 1691 | \end{lstlisting}
 | 
| 1692 | ||
| 1693 | \begin{lstlisting}[language=Scala]
 | |
| 352 | 1694 | List[String]/*!\annotation{Scala}!*/
 | 
| 395 | 1695 | \end{lstlisting}\bigskip
 | 
| 343 | 1696 | |
| 352 | 1697 | \noindent | 
| 343 | 1698 | String interpolations | 
| 1699 | ||
| 1700 | \begin{lstlisting}[language=Java]
 | |
| 352 | 1701 | System.out.println("Hello, "+ first + " "+ last + "!");
 | 
| 1702 | /*!\annotation{Java}!*/
 | |
| 343 | 1703 | \end{lstlisting}
 | 
| 1704 | ||
| 1705 | \begin{lstlisting}[language=Scala]
 | |
| 352 | 1706 | println(s"Hello, $first $last!")/*!\annotation{Scala}!*/
 | 
| 395 | 1707 | \end{lstlisting}\bigskip
 | 
| 343 | 1708 | |
| 352 | 1709 | \noindent | 
| 395 | 1710 | Java provides some syntactic sugar when constructing anonymous functions: | 
| 343 | 1711 | |
| 1712 | \begin{lstlisting}[language=Java]
 | |
| 1713 | list.foreach(item -> System.out.println("* " + item));
 | |
| 352 | 1714 | /*!\annotation{Java}!*/
 | 
| 343 | 1715 | \end{lstlisting}
 | 
| 1716 | ||
| 352 | 1717 | \noindent | 
| 1718 | In Scala, we use the \code{=>} symbol:
 | |
| 343 | 1719 | |
| 1720 | \begin{lstlisting}[language=Scala]
 | |
| 352 | 1721 | list.foreach(item => println(s"* $item"))/*!\annotation{Scala}!*/
 | 
| 1722 | \end{lstlisting}%$
 | |
| 395 | 1723 | \end{boxedminipage}
 | 
| 1724 | \end{figure}
 | |
| 343 | 1725 | |
| 352 | 1726 | %%new / vs case classes | 
| 343 | 1727 | |
| 195 | 1728 | |
| 123 | 1729 | \subsection*{More Info}
 | 
| 1730 | ||
| 1731 | There is much more to Scala than I can possibly describe in | |
| 197 | 1732 | this document and teach in the lectures. Fortunately there are a | 
| 1733 | number of free books | |
| 123 | 1734 | about Scala and of course lots of help online. For example | 
| 1735 | ||
| 1736 | \begin{itemize}
 | |
| 400 | 1737 | %%\item \url{http://www.scala-lang.org/docu/files/ScalaByExample.pdf}
 | 
| 1738 | %%\item \url{http://www.scala-lang.org/docu/files/ScalaTutorial.pdf}
 | |
| 123 | 1739 | \item \url{https://www.youtube.com/user/ShadowofCatron}
 | 
| 1740 | \item \url{http://docs.scala-lang.org/tutorials}
 | |
| 1741 | \item \url{https://www.scala-exercises.org}
 | |
| 188 | 1742 | \item \url{https://twitter.github.io/scala_school}
 | 
| 123 | 1743 | \end{itemize}
 | 
| 188 | 1744 | |
| 197 | 1745 | \noindent There is also an online course at Coursera on Functional | 
| 123 | 1746 | Programming Principles in Scala by Martin Odersky, the main | 
| 1747 | developer of the Scala language. And a document that explains | |
| 1748 | Scala for Java programmers | |
| 1749 | ||
| 1750 | \begin{itemize}
 | |
| 1751 | \item \small\url{http://docs.scala-lang.org/tutorials/scala-for-java-programmers.html}
 | |
| 1752 | \end{itemize}
 | |
| 1753 | ||
| 1754 | While I am quite enthusiastic about Scala, I am also happy to | |
| 1755 | admit that it has more than its fair share of faults. The | |
| 1756 | problem seen earlier of having to give an explicit type to | |
| 1757 | \code{toSet}, but not \code{toList} is one of them. There are
 | |
| 1758 | also many ``deep'' ideas about types in Scala, which even to | |
| 1759 | me as seasoned functional programmer are puzzling. Whilst | |
| 1760 | implicits are great, they can also be a source of great | |
| 1761 | headaches, for example consider the code: | |
| 1762 | ||
| 1763 | \begin{lstlisting}[numbers=none]
 | |
| 1764 | scala> List (1, 2, 3) contains "your mom" | |
| 1765 | res1: Boolean = false | |
| 1766 | \end{lstlisting}
 | |
| 1767 | ||
| 1768 | \noindent Rather than returning \code{false}, this code should
 | |
| 1769 | throw a typing-error. There are also many limitations Scala | |
| 1770 | inherited from the JVM that can be really annoying. For | |
| 1771 | example a fixed stack size. One can work around this | |
| 1772 | particular limitation, but why does one have to? | |
| 1773 | More such `puzzles' can be found at | |
| 1774 | ||
| 1775 | \begin{center}
 | |
| 1776 |   \url{http://scalapuzzlers.com} and
 | |
| 1777 |   \url{http://latkin.org/blog/2017/05/02/when-the-scala-compiler-doesnt-help/}
 | |
| 1778 | \end{center}
 | |
| 191 | 1779 | |
| 1780 | Even if Scala has been a success in several high-profile companies, | |
| 1781 | there is also a company (Yammer) that first used Scala in their | |
| 1782 | production code, but then moved away from it. Allegedly they did not | |
| 1783 | like the steep learning curve of Scala and also that new versions of | |
| 1784 | Scala often introduced incompatibilities in old code. Also the Java | |
| 197 | 1785 | language is lately developing at lightening speed (in comparison to the past) | 
| 1786 | taking on many | |
| 191 | 1787 | features of Scala and other languages, and it seems even it introduces | 
| 1788 | new features on its own. | |
| 123 | 1789 | |
| 333 | 1790 | |
| 1791 | Scala is deep: After many years, I still continue to learn new technique | |
| 1792 | for writing more elegant code. | |
| 1793 | ||
| 152 | 1794 | %So all in all, Scala might not be a great teaching language, | 
| 1795 | %but I hope this is mitigated by the fact that I never require | |
| 1796 | %you to write any Scala code. You only need to be able to read | |
| 1797 | %it. In the coursework you can use any programming language you | |
| 1798 | %like. If you want to use Scala for this, then be my guest; if | |
| 1799 | %you do not want, stick with the language you are most familiar | |
| 1800 | %with. | |
| 123 | 1801 | |
| 1802 | ||
| 191 | 1803 | \subsection*{Conclusion}
 | 
| 1804 | ||
| 198 | 1805 | I hope you liked the short journey through the Scala language---but remember we | 
| 197 | 1806 | like you to take on board the functional programming point of view, | 
| 198 | 1807 | rather than just learning another language. There is an interesting | 
| 1808 | blog article about Scala by a convert: | |
| 1809 | ||
| 1810 | \begin{center}
 | |
| 1811 | \url{https://www.skedulo.com/tech-blog/technology-scala-programming/}
 | |
| 1812 | \end{center}  
 | |
| 1813 | ||
| 1814 | \noindent | |
| 1815 | He makes pretty much the same arguments about functional programming and | |
| 1816 | immutability (one section is teasingly called \textit{``Where Did all
 | |
| 1817 | the Bugs Go?''}). If you happen to moan about all the idiotic features | |
| 1818 | of Scala, well, I guess this is part of the package according to this | |
| 1819 | quote:\bigskip | |
| 197 | 1820 | |
| 1821 | %\begin{itemize}
 | |
| 1822 | %\item no exceptions....there two kinds, one ``global'' exceptions, like | |
| 1823 | %out of memory (not much can be done about this by the ``individual'' | |
| 1824 | %programmer); and ``local one'' open a file that might not exists - in | |
| 1825 | %the latter you do not want to use exceptions, but Options | |
| 1826 | %\end{itemize}
 | |
| 123 | 1827 | |
| 182 | 1828 | \begin{flushright}\it
 | 
| 1829 | There are only two kinds of languages: the ones people complain | |
| 1830 | about\\ and the ones nobody uses.\smallskip\\ | |
| 1831 | \mbox{}\hfill\small{}---Bjarne Stroustrup (the inventor of C++)
 | |
| 1832 | \end{flushright}
 | |
| 1833 | ||
| 123 | 1834 | \end{document}
 | 
| 1835 | ||
| 1836 | %%% Local Variables: | |
| 1837 | %%% mode: latex | |
| 1838 | %%% TeX-master: t | |
| 1839 | %%% End: |