88 |
88 |
89 |
89 |
90 %% https://alvinalexander.com/downloads/HelloScala-FreePreview.pdf |
90 %% https://alvinalexander.com/downloads/HelloScala-FreePreview.pdf |
91 %% |
91 %% |
92 %% Section 10 about strings; interpolations and multiline strings |
92 %% Section 10 about strings; interpolations and multiline strings |
|
93 |
|
94 % Easy installation |
|
95 %https://alexarchambault.github.io/posts/2020-09-21-cs-setup.html |
93 |
96 |
94 |
97 |
95 % Exact colors from NB |
98 % Exact colors from NB |
96 \usepackage[breakable]{tcolorbox} |
99 \usepackage[breakable]{tcolorbox} |
97 \definecolor{incolor}{HTML}{303F9F} |
100 \definecolor{incolor}{HTML}{303F9F} |
1615 %\textit{More TBD.} |
1618 %\textit{More TBD.} |
1616 |
1619 |
1617 %\subsection*{Coursework} |
1620 %\subsection*{Coursework} |
1618 |
1621 |
1619 |
1622 |
|
1623 \subsection*{Scala Syntax for Java Developers} |
|
1624 |
|
1625 Scala compiles to the JVM, like the Java language. Because of this, |
|
1626 it can re-use many libraries. |
|
1627 |
|
1628 \begin{lstlisting}[language=Java] |
|
1629 Drink coke = getCoke(); |
|
1630 \end{lstlisting} |
|
1631 |
|
1632 \begin{lstlisting}[language=Scala] |
|
1633 val coke : Drink = getCoke() |
|
1634 \end{lstlisting} |
|
1635 |
|
1636 Unit means void: |
|
1637 |
|
1638 \begin{lstlisting}[language=Java] |
|
1639 public void output(String s) { |
|
1640 System.out.println(s); |
|
1641 } |
|
1642 \end{lstlisting} |
|
1643 |
|
1644 \begin{lstlisting}[language=Scala] |
|
1645 def output(s: String): Unit = println(s) |
|
1646 \end{lstlisting} |
|
1647 |
|
1648 |
|
1649 Type for list of Strings: |
|
1650 |
|
1651 \begin{lstlisting}[language=Java] |
|
1652 List<String> |
|
1653 \end{lstlisting} |
|
1654 |
|
1655 \begin{lstlisting}[language=Scala] |
|
1656 List[String] |
|
1657 \end{lstlisting} |
|
1658 |
|
1659 String interpolations |
|
1660 |
|
1661 \begin{lstlisting}[language=Java] |
|
1662 System.out.println("Hello, "+ firstName + " "+ lastName + "!"); |
|
1663 \end{lstlisting} |
|
1664 |
|
1665 \begin{lstlisting}[language=Scala] |
|
1666 println(s"Hello, $firstName $lastName!") |
|
1667 \end{lstlisting} |
|
1668 |
|
1669 |
|
1670 Java provides syntactic sugar when constructing lambda functions: |
|
1671 |
|
1672 \begin{lstlisting}[language=Java] |
|
1673 list.foreach(item -> System.out.println("* " + item)); |
|
1674 \end{lstlisting} |
|
1675 |
|
1676 In Scala, we use the => symbol with anonymous functions: |
|
1677 |
|
1678 \begin{lstlisting}[language=Scala] |
|
1679 list.foreach(item => println(s"* $item")) |
|
1680 \end{lstlisting} |
|
1681 |
|
1682 new / vs case classes |
|
1683 |
1620 |
1684 |
1621 \subsection*{More Info} |
1685 \subsection*{More Info} |
1622 |
1686 |
1623 There is much more to Scala than I can possibly describe in |
1687 There is much more to Scala than I can possibly describe in |
1624 this document and teach in the lectures. Fortunately there are a |
1688 this document and teach in the lectures. Fortunately there are a |