handouts/pep-ho.tex
changeset 270 b9eaa5cdec4a
parent 269 86a85865e772
child 271 48e12e7aee6e
equal deleted inserted replaced
269:86a85865e772 270:b9eaa5cdec4a
   321 
   321 
   322 \centering\includegraphics[scale=0.5]{../pics/cpu2.png} &
   322 \centering\includegraphics[scale=0.5]{../pics/cpu2.png} &
   323 \centering\includegraphics[scale=0.5]{../pics/cpu1.png}
   323 \centering\includegraphics[scale=0.5]{../pics/cpu1.png}
   324 \end{tabular}
   324 \end{tabular}
   325 \end{center}
   325 \end{center}
   326 \caption{The code of the ``main'' loops in my Mandelbrot program.
   326 \caption{The code of the ``main'' loops in my version of the mandelbrot program.
   327 The parallel version differs only in \texttt{.par} being added to the
   327 The parallel version differs only in \texttt{.par} being added to the
   328 ``ranges'' of the x and y coordinates. As can be seen from the CPU loads, in
   328 ``ranges'' of the x and y coordinates. As can be seen from the CPU loads, in
   329 the sequential version there is a lower peak for an extended period,
   329 the sequential version there is a lower peak for an extended period,
   330 while in the parallel version there is a short sharp burst for
   330 while in the parallel version there is a short sharp burst for
   331 essentially the same workload\ldots{}meaning you get more work done 
   331 essentially the same workload\ldots{}meaning you get more work done 
   640 when for-comprehensions get more complicated (see below).
   640 when for-comprehensions get more complicated (see below).
   641 
   641 
   642 The very charming feature of Scala is that such maps or
   642 The very charming feature of Scala is that such maps or
   643 for-comprehensions can be written for any kind of data
   643 for-comprehensions can be written for any kind of data
   644 collection, such as lists, sets, vectors, options and so on.
   644 collection, such as lists, sets, vectors, options and so on.
   645 For example if we instead compute the reminders modulo 3 of
   645 For example if we instead compute the remainders modulo 3 of
   646 this list, we can write
   646 this list, we can write
   647 
   647 
   648 \begin{lstlisting}[numbers=none]
   648 \begin{lstlisting}[numbers=none]
   649 scala> (1 to 8).toList.map(n => n % 3)
   649 scala> (1 to 8).toList.map(n => n % 3)
   650 res4 = List(1, 2, 0, 1, 2, 0, 1, 2)
   650 res4 = List(1, 2, 0, 1, 2, 0, 1, 2)
   651 \end{lstlisting}
   651 \end{lstlisting}
   652 
   652 
   653 \noindent If we, however, transform the numbers 1 to 8 not
   653 \noindent If we, however, transform the numbers 1 to 8 not
   654 into a list, but into a set, and then compute the reminders
   654 into a list, but into a set, and then compute the remainders
   655 modulo 3 we obtain
   655 modulo 3 we obtain
   656 
   656 
   657 \begin{lstlisting}[numbers=none]
   657 \begin{lstlisting}[numbers=none]
   658 scala> (1 to 8).toSet[Int].map(n => n % 3)
   658 scala> (1 to 8).toSet[Int].map(n => n % 3)
   659 res5 = Set(2, 1, 0)
   659 res5 = Set(2, 1, 0)
   699 
   699 
   700 While hopefully this all looks reasonable, there is one
   700 While hopefully this all looks reasonable, there is one
   701 complication: In the examples above we always wanted to
   701 complication: In the examples above we always wanted to
   702 transform one list into another list (e.g.~list of squares),
   702 transform one list into another list (e.g.~list of squares),
   703 or one set into another set (set of numbers into set of
   703 or one set into another set (set of numbers into set of
   704 reminders modulo 3). What happens if we just want to print out
   704 remainders modulo 3). What happens if we just want to print out
   705 a list of integers? Then actually the for-comprehension
   705 a list of integers? Then actually the for-comprehension
   706 needs to be modified. The reason is that \code{print}, you
   706 needs to be modified. The reason is that \code{print}, you
   707 guessed it, does not produce any result, but only produces
   707 guessed it, does not produce any result, but only produces
   708 what is in the functional-programming-lingo called a
   708 what is in the functional-programming-lingo called a
   709 \emph{side-effect}. Printing out the list of numbers from 1 to 5
   709 \emph{side-effect}. Printing out the list of numbers from 1 to 5
   732 
   732 
   733 \noindent In this code I use a variable assignment (\code{val
   733 \noindent In this code I use a variable assignment (\code{val
   734 square = ...} ) and also what is called in Scala a
   734 square = ...} ) and also what is called in Scala a
   735 \emph{string interpolation}, written \code{s"..."}. The latter
   735 \emph{string interpolation}, written \code{s"..."}. The latter
   736 is for printing out an equation. It allows me to refer to the
   736 is for printing out an equation. It allows me to refer to the
   737 integer values \code{n} and \code{square\_n} inside a string.
   737 integer values \code{n} and \code{square} inside a string.
   738 This is very convenient for printing out ``things''. 
   738 This is very convenient for printing out ``things''. 
   739 
   739 
   740 The corresponding map construction for functions with 
   740 The corresponding map construction for functions with 
   741 side-effects is in Scala called \code{foreach}. So you 
   741 side-effects is in Scala called \code{foreach}. So you 
   742 could also write
   742 could also write
   805 \end{lstlisting}
   805 \end{lstlisting}
   806 
   806 
   807 \noindent is for a triple (a tuple with three components---two
   807 \noindent is for a triple (a tuple with three components---two
   808 integers and a string). Tuples are helpful if you want to
   808 integers and a string). Tuples are helpful if you want to
   809 define functions with multiple results, say the function
   809 define functions with multiple results, say the function
   810 returning the quotient and reminder of two numbers. For this
   810 returning the quotient and remainder of two numbers. For this
   811 you might define:
   811 you might define:
   812 
   812 
   813 
   813 
   814 \begin{lstlisting}[ numbers=none]
   814 \begin{lstlisting}[ numbers=none]
   815 def quo_rem(m: Int, n: Int) : (Int, Int) = (m / n, m % n)
   815 def quo_rem(m: Int, n: Int) : (Int, Int) = (m / n, m % n)