diff -r 0e591f806290 -r 8a34b2ebc8cc handouts/pep-ho.tex --- a/handouts/pep-ho.tex Tue Dec 03 11:07:09 2019 +0000 +++ b/handouts/pep-ho.tex Mon Jan 27 10:18:13 2020 +0000 @@ -42,6 +42,44 @@ % https://www.matthewgerstman.com/map-filter-reduce/ +% Timing +% +% xs.map(x => (x, xs.count(_==x))) +% +% vs xs.groupBy(identity) +% +% first is quadratic, while second is linear. + +% contrast map with a for loop in imperative languages +% +% Let’s use a simple example of calculating sales tax on an array of +% prices. +% +% const prices = [19.99, 4.95, 25, 3.50]; +% let new_prices = []; +% +% for(let i=0; i < prices.length; i++) { +% new_prices.push(prices[i] * 1.06); +% } +% +% We can achieve the same results using .map(): +% +% const prices = [19.99, 4.95, 25, 3.50]; +% let new_prices = prices.map(price => price * 1.06); +% +% The syntax above is condensed so let’s walk through it a bit. The +% .map() method takes a callback, which can be thought of as a function. +% That’s what is between the parentheses. The variable price is the name +% that will be used to identify each value. Since there’s only one +% input, we can omit the usual parentheses around the parameters. + +% potentially a worked example? Tetris in scala.js +% +% https://medium.com/@michael.karen/learning-modern-javascript-with-tetris-92d532bcd057 +% +% Scala videos +% https://www.youtube.com/user/DrMarkCLewis + \begin{document} \fnote{\copyright{} Christian Urban, King's College London, 2017, 2018, 2019} @@ -1009,6 +1047,16 @@ all aggregate functions are pre-defined and often you have to write your own recursive function for this. +\subsection*{Always Produce a Result! No Exceptions!} +% +%Function should always produce a value. Exception is not thrown. +%Whenever there is a possibility of non-value result (exception, void, +%undefined, null, etc.), it should be incorporated in the result type. +%Such types include but not limited to +% +%Option[T] + + \subsection*{Higher-Order Functions} Functions obviously play a central role in functional programming. Two simple