% !TEX program = xelatex\documentclass{article}\usepackage{../styles/style}\usepackage{../styles/langs}\usepackage{tikz}\usepackage{pgf}\usepackage{marvosym}\usepackage{boxedminipage}\lstset{escapeinside={/*!}{!*/}}\newcommand{\annotation}[1]{\hfill\footnotesize{}#1}\usepackage{menukeys}% Exact colors from NB\usepackage[breakable]{tcolorbox}\definecolor{incolor}{HTML}{303F9F}\definecolor{outcolor}{HTML}{D84315}\definecolor{cellborder}{HTML}{CFCFCF}\definecolor{cellbackground}{HTML}{F7F7F7}\begin{document}\fnote{\copyright{} Christian Urban, King's College London, 2022}\section*{Scala Worksheet 2}\subsection*{Task 1 (Options)}Get familiar with the return value of functions that can``go wrong'':\begin{lstlisting}[numbers=none]scala> List(7,2,3,4,5,6).find(_ < 4)scala> List(5,6,7,8,9).find(_ < 4)scala> List(5,6,7,8,9).minscala> List(5,6,7,8,9).minOptionscala> List[Int]().minOption\end{lstlisting}\noindentNote that there needs to be a type-annotation for \texttt{List()} otherwiseScala will not know which \texttt{min}-version it should use. \subsection*{Task 2 (Try)}The Scala-Idiom \texttt{Try-getOrElse} allows you to convenientlydeal with failure cases.\begin{lstlisting}[numbers=none]scala> Try(Some(List(5,6,7,8,9).min)).getOrElse(None)scala> Try(Some(List[Int]().min)).getOrElse(None)\end{lstlisting}\noindentNote that \texttt{Try} needs the library \texttt{scala.util.\_} to beimported. \begin{lstlisting}[numbers=none]def safe_div(x: Int, y: Int) : Option[Int] = Try(Some(x / y)).getOrElse(None)\end{lstlisting}\subsection*{Task 3 (URLs / Files)}For simple tasks such as reading webpages and files, Scala providesconvenient functions \texttt{Source.fromURL} and \texttt{Source.fromFile}.To try them out, you need to import \texttt{io.Source}.\begin{lstlisting}[numbers=none]scala> Source.fromURL(my_url)("ISO-8859-1").mkStringscala> Source.fromFile(my_file)("ISO-8859-1").mkString\end{lstlisting}\noindentThese functions return an iterator, which can be transformed into a Stringusing \texttt{mkString}. The second argument fixes the character encodingand should not be omitted. If you are interested in the individual linesin the file, for example, you can use\begin{lstlisting}[numbers=none]Source.fromFile(my_file)("ISO-8859-1") .getLines().toList\end{lstlisting}\noindentIf you are after proper error-handling, then you can use Scala's optionsas follows\begin{lstlisting}[numbers=none]Try(Some(Source.fromFile("test.txt")("ISO-8859-1") .mkString)).getOrElse(None)\end{lstlisting} This can also be written slightly shorter as\begin{lstlisting}[numbers=none]Try(Source.fromFile("test.txt")("ISO-8859-1") .mkString).toOption\end{lstlisting} \noindentIn case of reading files, there can be an issue with closingfiles properly. For this Scala provides \texttt{Using}\begin{lstlisting}[numbers=none] Using(Source.fromFile("test.txt")("ISO-8859-1")) (_.mkString).toOption\end{lstlisting} \noindentThis closes the files automatically after reading, but otherwisebehaves as the code shown above: It gives a \texttt{Some} in thesuccess case and \texttt{None} in the failure case. However,\texttt{Using} requires a function as argument for prescribingof what to do with the file content in the success case.\subsection*{Task 4 (Higher-Order Functions)}Higher-Order functions means that Scala allows functions tohave functions as arguments and also allows functions toreturn functions. Get familiar with the short-hand notationfor simple functions\begin{lstlisting}[numbers=none]scala> List(7,2,3,4,5,6).find(_ < 4)scala> List(7,2,3,4,5,6).count(_ % 2 == 0)scala> List(7,2,3,4,5,6).sortWith(_ > _)scala> List(7,2,3,4,5,6).filter(_ > 4)\end{lstlisting}\noindentBe aware that this short-hand notation only works for ``smallish'' functionsand that sometimes Scala cannot figure out the types involved withoutexplicit type annotations.\subsection*{Task 5 (Maps)}Get familiar with the map-function for lists, sets etc. It is thequintessential higher-order function and frequently used for transforminglists.\begin{lstlisting}[numbers=none]scala> List(7,2,3,4,5,6).map(n => n * n)\end{lstlisting} \noindentMake also sure you see that Scala's \texttt{for}-comprehensionsare just syntactic sugar for \texttt{map}s. What would thisexpression look like as \texttt{for}-comprehension? What arethe advantages of \texttt{for}-comprehensions over \texttt{map}s.\subsection*{Task 6 (Pattern-Matching)}Rewrite the following function using pattern-matching\begin{lstlisting}[numbers=none]def my_map(lst: List[Int], f: Int => Int) : List[Int] = { if (lst == Nil) Nil else f(lst.head) :: my_map(lst.tail, f)}\end{lstlisting}\noindentObserve that the type of the function is from \texttt{Int}s to\texttt{Int}s, which is written in Scala as type \texttt{Int => Int}.\subsection*{Task 7 (Web-Crawler, Hard)}Have a look at the web-crawler at the end of \texttt{lecture2.scala}.Can you modify it such that every page is only visited once? \end{document}%%% Local Variables: %%% mode: latex%%% TeX-master: t%%% End: