author | Christian Urban <urbanc@in.tum.de> |
Tue, 29 Oct 2019 09:54:52 +0000 | |
changeset 278 | 0c2481cd8b1c |
parent 277 | acaf2099406a |
child 279 | 14bf4e478534 |
README | file | annotate | diff | comparison | revisions | |
cws/cw05.tex | file | annotate | diff | comparison | revisions | |
cws/disclaimer.sty | file | annotate | diff | comparison | revisions | |
handouts/pep-ho.pdf | file | annotate | diff | comparison | revisions | |
handouts/pep-ho.tex | file | annotate | diff | comparison | revisions | |
progs/lecture2.scala | file | annotate | diff | comparison | revisions | |
progs/lecture4.scala | file | annotate | diff | comparison | revisions | |
slides/slides01.tex | file | annotate | diff | comparison | revisions | |
templates1/alcohol.scala | file | annotate | diff | comparison | revisions | |
templates1/population.csv | file | annotate | diff | comparison | revisions |
--- a/README Fri Aug 16 08:45:21 2019 +0100 +++ b/README Tue Oct 29 09:54:52 2019 +0000 @@ -17,6 +17,15 @@ +----------------------------- +node +Welcome to Node.js v12.11.1. +Type ".help" for more information. +> "11" + 1 +'111' +> "11" - 1 +10 +>
--- a/cws/cw05.tex Fri Aug 16 08:45:21 2019 +0100 +++ b/cws/cw05.tex Tue Oct 29 09:54:52 2019 +0000 @@ -15,6 +15,12 @@ \section*{Coursework 10 (Scala)} +\mbox{}\hfill\textit{``What one programmer can do in one month,}\\ +\mbox{}\hfill\textit{two programmers can do in two months.''}\smallskip\\ +\mbox{}\hfill\textit{ --- Frederick P.~Brooks (author of The Mythical Man-Month)}\bigskip\medskip + + +\noindent This coursework is worth 10\%. It is about a small programming language called brainf***. The first part is due on 13 December at 11pm; the second, more advanced part, is due on 20 December at
--- a/cws/disclaimer.sty Fri Aug 16 08:45:21 2019 +0100 +++ b/cws/disclaimer.sty Tue Oct 29 09:54:52 2019 +0000 @@ -21,7 +21,8 @@ \texttt{Array}s or \texttt{ListBuffer}s, for example. \item Do not use \texttt{return} in your code! It has a different - meaning in Scala than in Java. + meaning in Scala than in Java. It changes the meaning of your program, + and you should never use it. \item Do not use \texttt{var}! This declares a mutable variable. Only use \texttt{val}!
--- a/handouts/pep-ho.tex Fri Aug 16 08:45:21 2019 +0100 +++ b/handouts/pep-ho.tex Tue Oct 29 09:54:52 2019 +0000 @@ -225,15 +225,15 @@ is that this clock-speed has not much increased over the past decade and no dramatic increases are predicted for any time soon. So you are a bit stuck. This is unlike previous generations of developers who could rely -upon the fact that every 2 years or so their code would run twice as -fast because the clock-speed of their CPUs got twice as fast. +upon the fact that approximately every 2 years their code would run +twice as fast because the clock-speed of their CPUs got twice as fast. Unfortunately this does not happen any more nowadays. To get you out of this dreadful situation, CPU producers pile more and more cores into CPUs in order to make them more powerful and potentially make software faster. The task for you as developer is to take somehow advantage of these cores by running as much of your code as possible in parallel on as many cores you have available (typically 4 in modern laptops and -sometimes much more on high-end machines). In this situation, +sometimes much more on high-end machines). In this situation \textit{mutable} variables like \texttt{i} above are evil, or at least a major nuisance: Because if you want to distribute some of the loop-iterations over the cores that are currently idle in your system, @@ -876,7 +876,7 @@ all pairs where the sum is not even (therefore \code{(1, 2)}, \code{(2, 1)} and \code{(3, 2)} are not in the result because their sum is odd). -To sum up, maps (or for-comprehensions) transform one collection into +To summarise, maps (or for-comprehensions) transform one collection into another. For example a list of \code{Int}s into a list of squares, and so on. There is no need for for-loops in Scala. But please do not be tempted to write anything like @@ -968,12 +968,12 @@ There is one more usage of for-loops in Java, C/C++ and the like: sometimes you want to \emph{aggregate} something about a list, for -example summing up all its elements. In this case you cannot use map, +example summing up all its elements. In this case you cannot use maps, because maps \emph{transform} one data collection into another data collection. They cannot be used to generate a single integer -representing an aggregate. So how is this done in Scala? Let us -suppose you want to sum up all elements from a list. You might -be tempted to write something like +representing an aggregate. So how is this kind of aggregation done in +Scala? Let us suppose you want to sum up all elements from a list. You +might be tempted to write something like \begin{lstlisting}[numbers=none] var cnt = 0 @@ -987,7 +987,7 @@ and indeed this is accepted Scala code and produces the expected result, namely \code{36}, \textbf{BUT} this is imperative style and not permitted in PEP. It uses a \code{var} and therefore violates the -immutability property I ask for in your code. Sorry. +immutability property I ask for in your code. Sorry! So how to do that same thing without using a \code{var}? Well there are several ways. One way is to define the following recursive @@ -1000,7 +1000,7 @@ \noindent You can then call \code{sum((1 to 8).toList)} and obtain the same result -without a mutable variable or for-loop. Obviously for simple things like +without a mutable variable and without a for-loop. Obviously for simple things like sum, you could have written \code{xs.sum} in the first place. But not all aggregate functions are pre-defined and often you have to write your own recursive function for this.
--- a/progs/lecture2.scala Fri Aug 16 08:45:21 2019 +0100 +++ b/progs/lecture2.scala Tue Oct 29 09:54:52 2019 +0000 @@ -513,6 +513,18 @@ println(people.sortWith(superior).mkString("\n")) +// String interpolations as patterns + +val date = "2000-01-01" +val s"$year-$month-$day" = date + +def parse_date(date: String) = date match { + case s"$year-$month-$day" => Some((year.toInt, month.toInt, day.toInt)) + case s"$day/$month/$year" => Some((year.toInt, month.toInt, day.toInt)) + case _ => None +} + + // Tail Recursion //================ @@ -723,3 +735,18 @@ +// if you like verbosity, you can full-specify the literal. +// Don't go telling that to people, though +(1 to 100).filter((x: Int) => x % 2 == 0).sum + +// As x is known to be an Int anyway, you can omit that part +(1 to 100).filter(x => x % 2 == 0).sum + +// As each parameter (only x in this case) is passed only once +// you can use the wizardy placeholder syntax +(1 to 100).filter(_ % 2 == 0).sum + +// But if you want to re-use your literal, you can also put it in a value +// In this case, explicit types are required because there's nothing to infer from +val isEven: (x: Int) => x % 2 == 0 +(1 to 100).filter(isEven).sum
--- a/progs/lecture4.scala Fri Aug 16 08:45:21 2019 +0100 +++ b/progs/lecture4.scala Tue Oct 29 09:54:52 2019 +0000 @@ -427,6 +427,27 @@ "HAL".increment +// Abstract idea: +// In that version implicit conversions were used to solve the +// late extension problem; namely, given a class C and a class T, +// how to have C extend T without touching or recompiling C. +// Conversions add a wrapper when a member of T is requested +// from an instance of C. + +//Another example + +case class Duration(time: Long, unit: TimeUnit) { + def +(o: Duration) = Duration(time + unit.convert(o.time, o.unit), unit) +} + +implicit class Int2Duration(that: Int) { + def seconds = new Duration(that, SECONDS) + def minutes = new Duration(that, MINUTES) +} + +5.seconds + 2.minutes //Duration(125L, SECONDS ) + + // Regular expressions - the power of DSLs in Scala //==================================================
--- a/slides/slides01.tex Fri Aug 16 08:45:21 2019 +0100 +++ b/slides/slides01.tex Tue Oct 29 09:54:52 2019 +0000 @@ -20,6 +20,7 @@ % beamer stuff \renewcommand{\slidecaption}{PEP (Scala) \liningnums{01}, King's College London} +%https://insights.stackoverflow.com/survey/2019#technology-most-loved-dreaded-and-wanted \begin{document}
--- a/templates1/alcohol.scala Fri Aug 16 08:45:21 2019 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,66 +0,0 @@ -// Part 2 about Alcohol-Consumption Worldwide -//============================================ - -object CW6b { - -import io.Source -import scala.util._ - -val url_alcohol = - "https://raw.githubusercontent.com/fivethirtyeight/data/master/alcohol-consumption/drinks.csv" - -val file_population = - "population.csv" - - -//(1) Complete the get_csv_page function below. It takes a URL-string -// as argument and generates a list of strings corresponding to each -// line in the downloaded csv-list. The URL url_alcohol above is one -// possible argument. - -//def get_csv_page(url: String) : List[String] = ... - - -// Complete the get_csv_file function below. It takes a file name -// as argument and reads the content of the given file. Like above, -// it should generate a list of strings corresponding to each -// line in the csv-list. The filename file_population is one possible -// argument. - -//def get_csv_file(file: String) : List[String] = ... - - - -//(2) Complete the functions that process the csv-lists. For -// process_alcs extract the country name (as String) and the -// pure alcohol consumption (as Double). For process_pops -// generate a Map of Strings (country names) to Long numbers -// (population sizes). - -//def process_alcs(lines: List[String]) : List[(String, Double)] = ... - -//def process_pops(lines: List[String]) : Map[String, Long] = ... - - - -//(3) Calculate for each country the overall alcohol_consumption using -// the data from the alcohol list and the population sizes list. You -// should only include countries on the alcohol list that are also -// on the population sizes list with the exact same name. Note that -// the spelling of some names in the alcohol list differs from the -// population sizes list. You can ignore entries where the names differ. -// Sort the resulting list according to the country with the highest alcohol -// consumption to the country with the lowest alcohol consumption. - -//def sorted_country_consumption() : List[(String, Long)] = ... - - -// Calculate the world consumption of pure alcohol of all countries, which -// should be the first element in the tuple below. The second element is -// the overall consumption of the first n countries in the sorted list -// from above; and finally the double should be the percentage of the -// first n countries drinking from the the world consumption of alcohol. - -//def percentage(n: Int) : (Long, Long, Double) = ... - -}
--- a/templates1/population.csv Fri Aug 16 08:45:21 2019 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,216 +0,0 @@ -country,population_size -Afghanistan,32758020 -Albania,2889104 -Algeria,39113313 -American Samoa,55437 -Andorra,79223 -Angola,26920466 -Antigua and Barbuda,98875 -Argentina,42981515 -Armenia,2906220 -Aruba,103795 -Australia,23460694 -Austria,8541575 -Azerbaijan,9535079 -Bahamas,382169 -Bahrain,1336397 -Bangladesh,159405279 -Barbados,283385 -Belarus,9474511 -Belgium,11209057 -Belize,351694 -Benin,10286712 -Bermuda,65139 -Bhutan,776448 -Bolivia,10562159 -Bosnia and Herzegovina,3566002 -Botswana,2168573 -Brazil,204213133 -British Virgin Islands,29588 -Brunei Darussalam,411704 -Bulgaria,7223938 -Burkina Faso,17585977 -Burundi,9891790 -Cabo Verde,526437 -Cambodia,15270790 -Cameroon,22239904 -Canada,35544564 -Cayman Islands,59172 -Central African Republic,4515392 -Chad,13569438 -Channel Islands,162969 -Chile,17613798 -China,1364270000 -Colombia,47791911 -Comoros,759385 -Congo,73722860 -Costa Rica,4757575 -Cote d'Ivoire,22531350 -Croatia,4238389 -Cuba,11439767 -Curacao,155909 -Cyprus,1152309 -Czech Republic,10525347 -Denmark,5643475 -Djibouti,912164 -Dominica,72778 -Dominican Republic,10405844 -Ecuador,15903112 -Egypt,91812566 -El Salvador,6281189 -Equatorial Guinea,1129424 -Estonia,1314545 -Ethiopia,97366774 -Faroe Islands,48842 -Fiji,885806 -Finland,5461512 -France,66331957 -French Polynesia,275484 -Gabon,1875713 -Gambia,1917852 -Georgia,3727000 -Germany,80982500 -Ghana,26962563 -Gibraltar,34038 -Greece,10892413 -Greenland,56295 -Grenada,106360 -Guam,160967 -Guatemala,15923559 -Guinea,11805509 -Guinea-Bissau,1725744 -Guyana,763393 -Haiti,10572466 -Honduras,8809216 -Hong Kong SAR,7241700 -Hungary,9866468 -Iceland,327386 -India,1293859294 -Indonesia,255131116 -Iran,78411092 -Iraq,35006080 -Ireland,4617225 -Isle of Man,82590 -Israel,8215700 -Italy,60789140 -Jamaica,2862087 -Japan,127276000 -Jordan,8809306 -Kazakhstan,17289224 -Kenya,46024250 -Kiribati,110458 -North Korea,25116363 -South Korea,50746659 -Kosovo,1821800 -Kuwait,3782450 -Kyrgyz Republic,5835500 -Lao PDR,6576397 -Latvia,1993782 -Lebanon,5603279 -Lesotho,2145785 -Liberia,4390737 -Libya,6204108 -Liechtenstein,37127 -Lithuania,2932367 -Luxembourg,556319 -Macao SAR,588781 -Macedonia,2077495 -Madagascar,23589801 -Malawi,17068838 -Malaysia,30228017 -Maldives,401000 -Mali,16962846 -Malta,427364 -Marshall Islands,52898 -Mauritania,4063920 -Mauritius,1260934 -Mexico,124221600 -Micronesia,104015 -Moldova,3556397 -Monaco,38132 -Mongolia,2923896 -Montenegro,621810 -Morocco,34318082 -Mozambique,27212382 -Myanmar,51924182 -Namibia,2370992 -Nauru,11853 -Nepal,28323241 -Netherlands,16865008 -New Caledonia,268000 -New Zealand,4509700 -Nicaragua,6013997 -Niger,19148219 -Nigeria,176460502 -Northern Mariana Islands,54468 -Norway,5137232 -Oman,3960925 -Pakistan,185546257 -Palau,21094 -Panama,3903986 -Papua New Guinea,7755785 -Paraguay,6552584 -Peru,30973354 -Philippines,100102249 -Poland,38011735 -Portugal,10401062 -Puerto Rico,3534874 -Qatar,2374419 -Romania,19908979 -Russian Federation,143819666 -Rwanda,11345357 -Samoa,192290 -San Marino,32657 -Sao Tome and Principe,191266 -Saudi Arabia,30776722 -Senegal,14546111 -Serbia,7130576 -Seychelles,91359 -Sierra Leone,7079162 -Singapore,5469724 -Sint Maarten (Dutch part),37685 -Slovak Republic,5418649 -Slovenia,2061980 -Solomon Islands,575504 -Somalia,13513125 -South Africa,54146734 -South Sudan,11530971 -Spain,46480882 -Sri Lanka,20771000 -St. Kitts and Nevis,53739 -St. Lucia,176421 -St. Martin (French part),31530 -St. Vincent and the Grenadines,109357 -Sudan,37737913 -Suriname,547928 -Swaziland,1295097 -Sweden,9696110 -Switzerland,8188649 -Syrian Arab Republic,19203090 -Tajikistan,8362745 -Tanzania,52234869 -Thailand,68416772 -Timor-Leste,1212814 -Togo,7228915 -Tonga,105782 -Trinidad and Tobago,1354493 -Tunisia,11143908 -Turkey,77030628 -Turkmenistan,5466241 -Turks and Caicos Islands,33739 -Tuvalu,10908 -Uganda,38833338 -Ukraine,45271947 -United Arab Emirates,9070867 -United Kingdom,64613160 -United States,318563456 -Uruguay,3419546 -Uzbekistan,30757700 -Vanuatu,258850 -Venezuela,30738378 -Vietnam,90728900 -Virgin Islands (U.S.),104170 -West Bank and Gaza,4294682 -Yemen,26246327 -Zambia,15620974 -Zimbabwe,15411675