14 % https://www.youtube.com/watch?v=QdVFvsCWXrA |
15 % https://www.youtube.com/watch?v=QdVFvsCWXrA |
15 |
16 |
16 % functional programming in Scala |
17 % functional programming in Scala |
17 %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 |
18 %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 |
18 |
19 |
19 % functional programmming in C |
20 % functional programming in C |
20 %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 |
21 %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 |
21 |
22 |
22 %speeding through haskell |
23 %speeding through haskell |
23 %https://openlibra.com/en/book/download/speeding-through-haskell |
24 %https://openlibra.com/en/book/download/speeding-through-haskell |
24 |
25 |
77 |
78 |
78 \subsection*{VS Code and Scala} |
79 \subsection*{VS Code and Scala} |
79 |
80 |
80 I found a convenient IDE for writing Scala programs is Microsoft's |
81 I found a convenient IDE for writing Scala programs is Microsoft's |
81 \textit{Visual Studio Code} (VS Code) which runs under MacOSX, Linux and |
82 \textit{Visual Studio Code} (VS Code) which runs under MacOSX, Linux and |
82 obviously Windows.\footnote{Unlike \emph{Microsoft Visual Studio}---note |
83 obviously Windows.\footnote{unlike \emph{Microsoft Visual Studio}---note |
83 the minuscule difference in the name---which is a heavy-duty, |
84 the minuscule difference in the name---which is a heavy-duty, |
84 Windows-only IDE\ldots{}jeez, with all their money could they not come |
85 Windows-only IDE\ldots{}jeez, with all their money could they not come |
85 up with a completely different name for a complete different project? |
86 up with a completely different name for a complete different project? |
86 For the pedantic, Microsoft Visual Studio is an IDE, whereas Visual |
87 For the pedantic, Microsoft Visual Studio is an IDE, whereas Visual |
87 Studio Code is a source code editor. Anybody knows the what the |
88 Studio Code is considered as a source code editor. Anybody knows the what the |
88 difference is?} It can be downloaded for free from |
89 difference is?} It can be downloaded for free from |
89 |
90 |
90 \begin{quote} |
91 \begin{quote} |
91 \url{https://code.visualstudio.com} |
92 \url{https://code.visualstudio.com} |
92 \end{quote} |
93 \end{quote} |
117 What I like most about VS Code is that it provides easy access to the |
118 What I like most about VS Code is that it provides easy access to the |
118 Scala REPL. But if you prefer another editor for coding, it is also |
119 Scala REPL. But if you prefer another editor for coding, it is also |
119 painless to work with Scala completely on the command line (as you might |
120 painless to work with Scala completely on the command line (as you might |
120 have done with \texttt{g++} in the earlier part of PEP). For the |
121 have done with \texttt{g++} in the earlier part of PEP). For the |
121 lazybones among us, there are even online editors and environments for |
122 lazybones among us, there are even online editors and environments for |
122 developing and running Scala programs called \textit{ScalaFiddle} |
123 developing and running Scala programs: \textit{ScalaFiddle} |
123 and \textit{Scastie}, which |
124 and \textit{Scastie} are two of them. They require zero setup |
124 require zero setup (assuming you have a browser handy). You can access |
125 (assuming you have a browser handy). You can access them at |
125 them from: |
|
126 |
126 |
127 \begin{quote} |
127 \begin{quote} |
128 \url{https://scalafiddle.io}\\ |
128 \url{https://scalafiddle.io}\\ |
129 \url{https://scastie.scala-lang.org}\medskip |
129 \url{https://scastie.scala-lang.org}\medskip |
130 \end{quote} |
130 \end{quote} |
131 |
131 |
132 \noindent |
132 \noindent |
|
133 But you should be careful if you use them for your coursework: they |
|
134 are meant to play around, not really for serious work. |
|
135 |
133 Scala can be used with the heavy-duty IDEs Eclipse and IntelliJ. |
136 Scala can be used with the heavy-duty IDEs Eclipse and IntelliJ. |
134 A ready-made Scala bundle for Eclipse is available from |
137 A ready-made Scala bundle for Eclipse is available from |
135 |
138 |
136 \begin{quote} |
139 \begin{quote} |
137 \url{http://scala-ide.org/download/sdk.html} |
140 \url{http://scala-ide.org/download/sdk.html} |
169 programming seems to go against the core principles of |
172 programming seems to go against the core principles of |
170 \textit{imperative programming} (which is what you do in Java and C++ |
173 \textit{imperative programming} (which is what you do in Java and C++ |
171 for example). The main idea of imperative programming is that you have |
174 for example). The main idea of imperative programming is that you have |
172 some form of \emph{state} in your program and you continuously change this |
175 some form of \emph{state} in your program and you continuously change this |
173 state by issuing some commands---for example for updating a field in an |
176 state by issuing some commands---for example for updating a field in an |
174 array or object, or for adding one to a variable and so on. The classic |
177 array or for adding one to a variable and so on. The classic |
175 example for this style of programming are \texttt{for}-loops, like |
178 example for this style of programming are \texttt{for}-loops in C: |
176 |
179 |
177 \begin{lstlisting}[language=C,numbers=none] |
180 \begin{lstlisting}[language=C,numbers=none] |
178 for (int i = 10; i < 20; i++) { |
181 for (int i = 10; i < 20; i++) { |
179 //...Do something interesting with i... |
182 //...Do something interesting with i... |
180 } |
183 } |
424 able to ``play around'' with it! |
427 able to ``play around'' with it! |
425 |
428 |
426 \subsection*{Standalone Scala Apps} |
429 \subsection*{Standalone Scala Apps} |
427 |
430 |
428 If you want to write a stand-alone app in Scala, you can |
431 If you want to write a stand-alone app in Scala, you can |
429 implement an object that is an instance of \code{App}, say |
432 implement an object that is an instance of \code{App}. For example |
|
433 write |
430 |
434 |
431 \begin{lstlisting}[numbers=none] |
435 \begin{lstlisting}[numbers=none] |
432 object Hello extends App { |
436 object Hello extends App { |
433 println("hello world") |
437 println("hello world") |
434 } |
438 } |
435 \end{lstlisting} |
439 \end{lstlisting} |
436 |
440 |
437 \noindent save it in a file, for example {\tt hello-world.scala}, and |
441 \noindent save it in a file, say {\tt hello-world.scala}, and |
438 then run the compiler (\texttt{scalac}) and start the runtime |
442 then run the compiler (\texttt{scalac}) and start the runtime |
439 environment (\texttt{scala}): |
443 environment (\texttt{scala}): |
440 |
444 |
441 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small] |
445 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small] |
442 $ scalac hello-world.scala |
446 $ scalac hello-world.scala |
519 \end{lstlisting} |
523 \end{lstlisting} |
520 |
524 |
521 \noindent |
525 \noindent |
522 This function returns the value resulting from evaluating the expression |
526 This function returns the value resulting from evaluating the expression |
523 \code{EXPR} (whatever is substituted for this). The result will be |
527 \code{EXPR} (whatever is substituted for this). The result will be |
524 of type \code{String}. It is a good habit to include this information |
528 of type \code{String}. It is a good habit to always include this information |
525 about the return type always. Simple examples of Scala functions are: |
529 about the return type. Simple examples of Scala functions are: |
526 |
530 |
527 \begin{lstlisting}[numbers=none] |
531 \begin{lstlisting}[numbers=none] |
528 def incr(x: Int) : Int = x + 1 |
532 def incr(x: Int) : Int = x + 1 |
529 def double(x: Int) : Int = x + x |
533 def double(x: Int) : Int = x + x |
530 def square(x: Int) : Int = x * x |
534 def square(x: Int) : Int = x * x |
538 BODY |
542 BODY |
539 } |
543 } |
540 \end{lstlisting} |
544 \end{lstlisting} |
541 |
545 |
542 \noindent |
546 \noindent |
543 where each argument requires its type and the result type of the |
547 where each argument, \texttt{arg1}, \texttt{arg2} and so on, requires |
544 function, \code{rty}, should be given. If the body of the function is |
548 its type and the result type of the |
|
549 function, \code{rty}, should also be given. If the body of the function is |
545 more complex, then it can be enclosed in braces, like above. If it it |
550 more complex, then it can be enclosed in braces, like above. If it it |
546 is just a simple expression, like \code{x + 1}, you can omit the |
551 is just a simple expression, like \code{x + 1}, you can omit the |
547 braces. Very often functions are recursive (that is call themselves), |
552 braces. Very often functions are recursive (that is call themselves), |
548 like the venerable factorial function: |
553 like the venerable factorial function: |
549 |
554 |
567 \begin{lstlisting}[numbers=none] |
572 \begin{lstlisting}[numbers=none] |
568 scala> (1 to 8).toList |
573 scala> (1 to 8).toList |
569 res1: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8) |
574 res1: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8) |
570 \end{lstlisting} |
575 \end{lstlisting} |
571 |
576 |
572 \noindent Generating from this list, the list of squares in a |
577 \noindent Generating from this list the list of corresponding |
573 programming language such as Java, you would assume the list |
578 squares in a programming language such as Java, you would assume |
574 is given as a kind of array. You would then iterate, or loop, |
579 the list is given as a kind of array. You would then iterate, or loop, |
575 an index over this array and replace each entry in the array |
580 an index over this array and replace each entry in the array |
576 by the square. Right? In Scala, and in other functional |
581 by the square. Right? In Scala, and in other functional |
577 programming languages, you use maps to achieve the same. |
582 programming languages, you use maps to achieve the same. |
578 |
583 |
579 A map essentially takes a function that describes how each |
584 A map essentially takes a function that describes how each |
588 res2: List[Int] = List(1, 4, 9, 16, 25, 36, 49, 64) |
593 res2: List[Int] = List(1, 4, 9, 16, 25, 36, 49, 64) |
589 \end{lstlisting} |
594 \end{lstlisting} |
590 |
595 |
591 \noindent The important keywords are \code{for} and |
596 \noindent The important keywords are \code{for} and |
592 \code{yield}. This for-comprehension roughly states that from |
597 \code{yield}. This for-comprehension roughly states that from |
593 the list of numbers we draw \code{n}s and compute the result |
598 the list of numbers we draw elements that are given the name |
594 of \code{n * n}. As you can see, we specified the list where |
599 \code{n} and compute the result |
|
600 of \code{n * n}. This is a simple example---what comes after |
|
601 \code{yield} can be a complex expression enclosed in \texttt{\{...\}}. |
|
602 As you can see, we specified the list where |
595 each \code{n} comes from, namely \code{(1 to 8).toList}, and |
603 each \code{n} comes from, namely \code{(1 to 8).toList}, and |
596 how each element needs to be transformed. This can also be |
604 how each element needs to be transformed. This can also be |
597 expressed in a second way in Scala by using directly |
605 expressed in a second way in Scala by using directly |
598 \code{map}s as follows: |
606 \code{map}s as follows: |
599 |
607 |
677 reminders modulo 3). What happens if we just want to print out |
685 reminders modulo 3). What happens if we just want to print out |
678 a list of integers? Then actually the for-comprehension |
686 a list of integers? Then actually the for-comprehension |
679 needs to be modified. The reason is that \code{print}, you |
687 needs to be modified. The reason is that \code{print}, you |
680 guessed it, does not produce any result, but only produces |
688 guessed it, does not produce any result, but only produces |
681 what is in the functional-programming-lingo called a |
689 what is in the functional-programming-lingo called a |
682 side-effect. Printing out the list of numbers from 1 to 5 |
690 \emph{side-effect}. Printing out the list of numbers from 1 to 5 |
683 would look as follows |
691 would look as follows |
684 |
692 |
685 \begin{lstlisting}[numbers=none] |
693 \begin{lstlisting}[numbers=none] |
686 scala> for (n <- (1 to 5).toList) print(n) |
694 scala> for (n <- (1 to 5).toList) print(n) |
687 12345 |
695 12345 |
691 where you need to omit the keyword \code{yield}. You can |
699 where you need to omit the keyword \code{yield}. You can |
692 also do more elaborate calculations such as |
700 also do more elaborate calculations such as |
693 |
701 |
694 \begin{lstlisting}[numbers=none] |
702 \begin{lstlisting}[numbers=none] |
695 scala> for (n <- (1 to 5).toList) { |
703 scala> for (n <- (1 to 5).toList) { |
696 val square_n = n * n |
704 val square = n * n |
697 println(s"$n * $n = $square_n") |
705 println(s"$n * $n = $square") |
698 } |
706 } |
699 1 * 1 = 1 |
707 1 * 1 = 1 |
700 2 * 2 = 4 |
708 2 * 2 = 4 |
701 3 * 3 = 9 |
709 3 * 3 = 9 |
702 4 * 4 = 16 |
710 4 * 4 = 16 |
703 5 * 5 = 25 |
711 5 * 5 = 25 |
704 \end{lstlisting}%$ |
712 \end{lstlisting}%$ |
705 |
713 |
706 \noindent In this code I use a variable assignment (\code{val |
714 \noindent In this code I use a variable assignment (\code{val |
707 square_n = ...} ) and also what is called in Scala a |
715 square = ...} ) and also what is called in Scala a |
708 \emph{string interpolation}, written \code{s"..."}. The latter |
716 \emph{string interpolation}, written \code{s"..."}. The latter |
709 is for printing out an equation. It allows me to refer to the |
717 is for printing out an equation. It allows me to refer to the |
710 integer values \code{n} and \code{square\_n} inside a string. |
718 integer values \code{n} and \code{square\_n} inside a string. |
711 This is very convenient for printing out ``things''. |
719 This is very convenient for printing out ``things''. |
712 |
720 |
884 |
892 |
885 You might ask: Apart from silly functions like above, what is |
893 You might ask: Apart from silly functions like above, what is |
886 the point of having functions as input arguments to other |
894 the point of having functions as input arguments to other |
887 functions? In Java there is indeed no need of this kind of |
895 functions? In Java there is indeed no need of this kind of |
888 feature: at least in the past it did not allow such |
896 feature: at least in the past it did not allow such |
889 constructions. I think, the point of Java 8 is to lift this |
897 constructions. I think, the point of Java 8 and successors was to lift this |
890 restriction. But in all functional programming languages, |
898 restriction. But in all functional programming languages, |
891 including Scala, it is really essential to allow functions as |
899 including Scala, it is really essential to allow functions as |
892 input argument. Above you already seen \code{map} and |
900 input argument. Above you already seen \code{map} and |
893 \code{foreach} which need this. Consider the functions |
901 \code{foreach} which need this. Consider the functions |
894 \code{print} and \code{println}, which both print out strings, |
902 \code{print} and \code{println}, which both print out strings, |
1115 % \noindent Again I leave it to you to find out how much this |
1123 % \noindent Again I leave it to you to find out how much this |
1116 % simplifies the regular expression in comparison with if I had |
1124 % simplifies the regular expression in comparison with if I had |
1117 % to write this by hand using only the ``plain'' regular |
1125 % to write this by hand using only the ``plain'' regular |
1118 % expressions from the inductive datatype. |
1126 % expressions from the inductive datatype. |
1119 |
1127 |
1120 \bigskip\noindent |
1128 %\bigskip\noindent |
1121 \textit{More TBD.} |
1129 %\textit{More TBD.} |
1122 |
1130 |
1123 \subsection*{Coursework} |
1131 %\subsection*{Coursework} |
1124 |
1132 |
1125 |
1133 |
1126 |
1134 |
1127 \subsection*{More Info} |
1135 \subsection*{More Info} |
1128 |
1136 |
1129 There is much more to Scala than I can possibly describe in |
1137 There is much more to Scala than I can possibly describe in |
1130 this document. Fortunately there are a number of free books |
1138 this document and teach in the lectures. Fortunately there are a |
|
1139 number of free books |
1131 about Scala and of course lots of help online. For example |
1140 about Scala and of course lots of help online. For example |
1132 |
1141 |
1133 \begin{itemize} |
1142 \begin{itemize} |
1134 \item \url{http://www.scala-lang.org/docu/files/ScalaByExample.pdf} |
1143 \item \url{http://www.scala-lang.org/docu/files/ScalaByExample.pdf} |
1135 \item \url{http://www.scala-lang.org/docu/files/ScalaTutorial.pdf} |
1144 \item \url{http://www.scala-lang.org/docu/files/ScalaTutorial.pdf} |
1137 \item \url{http://docs.scala-lang.org/tutorials} |
1146 \item \url{http://docs.scala-lang.org/tutorials} |
1138 \item \url{https://www.scala-exercises.org} |
1147 \item \url{https://www.scala-exercises.org} |
1139 \item \url{https://twitter.github.io/scala_school} |
1148 \item \url{https://twitter.github.io/scala_school} |
1140 \end{itemize} |
1149 \end{itemize} |
1141 |
1150 |
1142 \noindent There is also a course at Coursera on Functional |
1151 \noindent There is also an online course at Coursera on Functional |
1143 Programming Principles in Scala by Martin Odersky, the main |
1152 Programming Principles in Scala by Martin Odersky, the main |
1144 developer of the Scala language. And a document that explains |
1153 developer of the Scala language. And a document that explains |
1145 Scala for Java programmers |
1154 Scala for Java programmers |
1146 |
1155 |
1147 \begin{itemize} |
1156 \begin{itemize} |
1177 Even if Scala has been a success in several high-profile companies, |
1186 Even if Scala has been a success in several high-profile companies, |
1178 there is also a company (Yammer) that first used Scala in their |
1187 there is also a company (Yammer) that first used Scala in their |
1179 production code, but then moved away from it. Allegedly they did not |
1188 production code, but then moved away from it. Allegedly they did not |
1180 like the steep learning curve of Scala and also that new versions of |
1189 like the steep learning curve of Scala and also that new versions of |
1181 Scala often introduced incompatibilities in old code. Also the Java |
1190 Scala often introduced incompatibilities in old code. Also the Java |
1182 language is lately developing at lightening speed taking on many |
1191 language is lately developing at lightening speed (in comparison to the past) |
|
1192 taking on many |
1183 features of Scala and other languages, and it seems even it introduces |
1193 features of Scala and other languages, and it seems even it introduces |
1184 new features on its own. |
1194 new features on its own. |
1185 |
1195 |
1186 %So all in all, Scala might not be a great teaching language, |
1196 %So all in all, Scala might not be a great teaching language, |
1187 %but I hope this is mitigated by the fact that I never require |
1197 %but I hope this is mitigated by the fact that I never require |
1192 %with. |
1202 %with. |
1193 |
1203 |
1194 |
1204 |
1195 \subsection*{Conclusion} |
1205 \subsection*{Conclusion} |
1196 |
1206 |
1197 \begin{itemize} |
1207 I hope you like the short journey in the Scala World: remember we |
1198 \item no exceptions....there two kinds, one ``global'' exceptions, like |
1208 like you to take on board the functional programming point of view, |
1199 out of memory (not much can be done about this by the ``individual'' |
1209 rather than just learning another language. If you moan about all the |
1200 programmer); and ``local one'' open a file that might not exists - in |
1210 idiotic features of Scala, well that is part of the packaged according |
1201 the latter you do not want to use exceptions, but Options |
1211 to this quote:\bigskip |
1202 \end{itemize} |
1212 |
|
1213 %\begin{itemize} |
|
1214 %\item no exceptions....there two kinds, one ``global'' exceptions, like |
|
1215 %out of memory (not much can be done about this by the ``individual'' |
|
1216 %programmer); and ``local one'' open a file that might not exists - in |
|
1217 %the latter you do not want to use exceptions, but Options |
|
1218 %\end{itemize} |
1203 |
1219 |
1204 \begin{flushright}\it |
1220 \begin{flushright}\it |
1205 There are only two kinds of languages: the ones people complain |
1221 There are only two kinds of languages: the ones people complain |
1206 about\\ and the ones nobody uses.\smallskip\\ |
1222 about\\ and the ones nobody uses.\smallskip\\ |
1207 \mbox{}\hfill\small{}---Bjarne Stroustrup (the inventor of C++) |
1223 \mbox{}\hfill\small{}---Bjarne Stroustrup (the inventor of C++) |