handouts/pep-ho.tex
changeset 352 97bcf8efe4e0
parent 343 c8fcc0e0a57f
child 395 017f621f5835
equal deleted inserted replaced
351:591b9005157e 352:97bcf8efe4e0
     5 \usepackage{tikz}
     5 \usepackage{tikz}
     6 \usepackage{pgf}
     6 \usepackage{pgf}
     7 \usepackage{marvosym}
     7 \usepackage{marvosym}
     8 \usepackage{boxedminipage}
     8 \usepackage{boxedminipage}
     9 
     9 
    10 
    10 \lstset{escapeinside={/*!}{!*/}}
    11 
    11 \newcommand{\annotation}[1]{\hfill\footnotesize{}#1}
       
    12 
       
    13 \usepackage{menukeys}
    12 
    14 
    13 
    15 
    14 %cheat sheet
    16 %cheat sheet
    15 %http://worldline.github.io/scala-cheatsheet/
    17 %http://worldline.github.io/scala-cheatsheet/
    16 
    18 
  1093 without a mutable variable and without a for-loop. Obviously for simple things like
  1095 without a mutable variable and without a for-loop. Obviously for simple things like
  1094 sum, you could have written \code{xs.sum} in the first place. But not
  1096 sum, you could have written \code{xs.sum} in the first place. But not
  1095 all aggregate functions are pre-defined and often you have to write your
  1097 all aggregate functions are pre-defined and often you have to write your
  1096 own recursive function for this.
  1098 own recursive function for this.
  1097 
  1099 
  1098 \subsection*{Always Produce a Result! No Exceptions!}
  1100 %\subsection*{Always Produce a Result! No Exceptions!}
  1099 %
  1101 %
  1100 %Function should always produce a value. Exception is not thrown.
  1102 %Function should always produce a value. Exception is not thrown.
  1101 %Whenever there is a possibility of non-value result (exception, void,
  1103 %Whenever there is a possibility of non-value result (exception, void,
  1102 %undefined, null, etc.), it should be incorporated in the result type.
  1104 %undefined, null, etc.), it should be incorporated in the result type.
  1103 %Such types include but not limited to
  1105 %Such types include but not limited to
  1104 %
  1106 %
  1105 %Option[T]
  1107 %Option[T]
  1106 
  1108 
  1107 TBD
  1109 %TBD
  1108 
  1110 
  1109 
  1111 
  1110 \subsection*{Higher-Order Functions}
  1112 \subsection*{Higher-Order Functions}
  1111 
  1113 
  1112 Functions obviously play a central role in functional programming. Two simple
  1114 Functions obviously play a central role in functional programming. Two simple
  1621 
  1623 
  1622 
  1624 
  1623 \subsection*{Scala Syntax for Java Developers}
  1625 \subsection*{Scala Syntax for Java Developers}
  1624 
  1626 
  1625 Scala compiles to the JVM, like the Java language. Because of this,
  1627 Scala compiles to the JVM, like the Java language. Because of this,
  1626 it can re-use many libraries.
  1628 it can re-use many libraries. Here are a few hints how some Java code
  1627 
  1629 tranlsates to Scala code:\bigskip
       
  1630 
       
  1631 \noindent
       
  1632 Variable declaration:
  1628 \begin{lstlisting}[language=Java]
  1633 \begin{lstlisting}[language=Java]
  1629 Drink coke = getCoke();
  1634 Drink coke = getCoke();/*!\annotation{Java}!*/
  1630 \end{lstlisting}
  1635 \end{lstlisting}
  1631 
  1636 
  1632 \begin{lstlisting}[language=Scala]
  1637 \begin{lstlisting}[language=Scala]
  1633 val coke : Drink = getCoke()
  1638 val coke : Drink = getCoke()/*!\annotation{Scala}!*/
  1634 \end{lstlisting}
  1639 \end{lstlisting}
  1635 
  1640 
       
  1641 \noindent
  1636 Unit means void:
  1642 Unit means void:
  1637 
  1643 
  1638 \begin{lstlisting}[language=Java]
  1644 \begin{lstlisting}[language=Java]
  1639 public void output(String s) {
  1645 public void output(String s) {/*!\annotation{Java}!*/
  1640   System.out.println(s);
  1646   System.out.println(s);
  1641 }
  1647 }
  1642 \end{lstlisting}
  1648 \end{lstlisting}
  1643 
  1649 
  1644 \begin{lstlisting}[language=Scala]
  1650 \begin{lstlisting}[language=Scala]
  1645 def output(s: String): Unit = println(s)
  1651 def output(s: String): Unit = println(s)/*!\annotation{Scala}!*/
  1646 \end{lstlisting}
  1652 \end{lstlisting}
  1647 
  1653 
  1648 
  1654 \noindent
  1649 Type for list of Strings:
  1655 Type for list of Strings:
  1650 
  1656 
  1651 \begin{lstlisting}[language=Java]
  1657 \begin{lstlisting}[language=Java]
  1652 List<String>
  1658 List<String>/*!\annotation{Java}!*/
  1653 \end{lstlisting}
  1659 \end{lstlisting}
  1654 
  1660 
  1655 \begin{lstlisting}[language=Scala]
  1661 \begin{lstlisting}[language=Scala]
  1656 List[String]
  1662 List[String]/*!\annotation{Scala}!*/
  1657 \end{lstlisting}
  1663 \end{lstlisting}
  1658 
  1664 
       
  1665 \noindent
  1659 String interpolations
  1666 String interpolations
  1660 
  1667 
  1661 \begin{lstlisting}[language=Java]
  1668 \begin{lstlisting}[language=Java]
  1662 System.out.println("Hello, "+ firstName + " "+ lastName + "!");
  1669 System.out.println("Hello, "+ first + " "+ last + "!");
       
  1670 /*!\annotation{Java}!*/
  1663 \end{lstlisting}
  1671 \end{lstlisting}
  1664 
  1672 
  1665 \begin{lstlisting}[language=Scala]
  1673 \begin{lstlisting}[language=Scala]
  1666 println(s"Hello, $firstName $lastName!")
  1674 println(s"Hello, $first $last!")/*!\annotation{Scala}!*/
  1667 \end{lstlisting}
  1675 \end{lstlisting}
  1668 
  1676 
  1669 
  1677 \noindent
  1670 Java provides syntactic sugar when constructing lambda functions:
  1678 Java provides syntactic some sugar when constructing anonymous functions:
  1671 
  1679 
  1672 \begin{lstlisting}[language=Java]
  1680 \begin{lstlisting}[language=Java]
  1673 list.foreach(item -> System.out.println("* " + item));
  1681 list.foreach(item -> System.out.println("* " + item));
  1674 \end{lstlisting}
  1682 /*!\annotation{Java}!*/
  1675 
  1683 \end{lstlisting}
  1676 In Scala, we use the => symbol with anonymous functions:
  1684 
       
  1685 \noindent
       
  1686 In Scala, we use the \code{=>} symbol:
  1677 
  1687 
  1678 \begin{lstlisting}[language=Scala]
  1688 \begin{lstlisting}[language=Scala]
  1679 list.foreach(item => println(s"* $item"))
  1689 list.foreach(item => println(s"* $item"))/*!\annotation{Scala}!*/
  1680 \end{lstlisting}
  1690 \end{lstlisting}%$
  1681 
  1691 
  1682 new / vs case classes
  1692 %%new / vs case classes
  1683 
  1693 
  1684 
  1694 
  1685 \subsection*{More Info}
  1695 \subsection*{More Info}
  1686 
  1696 
  1687 There is much more to Scala than I can possibly describe in
  1697 There is much more to Scala than I can possibly describe in