diff -r 591b9005157e -r 97bcf8efe4e0 handouts/pep-ho.tex --- a/handouts/pep-ho.tex Wed Nov 04 15:35:31 2020 +0000 +++ b/handouts/pep-ho.tex Fri Nov 06 01:07:26 2020 +0000 @@ -7,8 +7,10 @@ \usepackage{marvosym} \usepackage{boxedminipage} +\lstset{escapeinside={/*!}{!*/}} +\newcommand{\annotation}[1]{\hfill\footnotesize{}#1} - +\usepackage{menukeys} %cheat sheet @@ -1095,7 +1097,7 @@ all aggregate functions are pre-defined and often you have to write your own recursive function for this. -\subsection*{Always Produce a Result! No Exceptions!} +%\subsection*{Always Produce a Result! No Exceptions!} % %Function should always produce a value. Exception is not thrown. %Whenever there is a possibility of non-value result (exception, void, @@ -1104,7 +1106,7 @@ % %Option[T] -TBD +%TBD \subsection*{Higher-Order Functions} @@ -1623,63 +1625,71 @@ \subsection*{Scala Syntax for Java Developers} Scala compiles to the JVM, like the Java language. Because of this, -it can re-use many libraries. +it can re-use many libraries. Here are a few hints how some Java code +tranlsates to Scala code:\bigskip +\noindent +Variable declaration: \begin{lstlisting}[language=Java] -Drink coke = getCoke(); +Drink coke = getCoke();/*!\annotation{Java}!*/ \end{lstlisting} \begin{lstlisting}[language=Scala] -val coke : Drink = getCoke() +val coke : Drink = getCoke()/*!\annotation{Scala}!*/ \end{lstlisting} +\noindent Unit means void: \begin{lstlisting}[language=Java] -public void output(String s) { +public void output(String s) {/*!\annotation{Java}!*/ System.out.println(s); } \end{lstlisting} \begin{lstlisting}[language=Scala] -def output(s: String): Unit = println(s) +def output(s: String): Unit = println(s)/*!\annotation{Scala}!*/ \end{lstlisting} - +\noindent Type for list of Strings: \begin{lstlisting}[language=Java] -List +List/*!\annotation{Java}!*/ \end{lstlisting} \begin{lstlisting}[language=Scala] -List[String] +List[String]/*!\annotation{Scala}!*/ \end{lstlisting} +\noindent String interpolations \begin{lstlisting}[language=Java] -System.out.println("Hello, "+ firstName + " "+ lastName + "!"); +System.out.println("Hello, "+ first + " "+ last + "!"); +/*!\annotation{Java}!*/ \end{lstlisting} \begin{lstlisting}[language=Scala] -println(s"Hello, $firstName $lastName!") +println(s"Hello, $first $last!")/*!\annotation{Scala}!*/ \end{lstlisting} - -Java provides syntactic sugar when constructing lambda functions: +\noindent +Java provides syntactic some sugar when constructing anonymous functions: \begin{lstlisting}[language=Java] list.foreach(item -> System.out.println("* " + item)); +/*!\annotation{Java}!*/ \end{lstlisting} -In Scala, we use the => symbol with anonymous functions: +\noindent +In Scala, we use the \code{=>} symbol: \begin{lstlisting}[language=Scala] -list.foreach(item => println(s"* $item")) -\end{lstlisting} +list.foreach(item => println(s"* $item"))/*!\annotation{Scala}!*/ +\end{lstlisting}%$ -new / vs case classes +%%new / vs case classes \subsection*{More Info}