39 %https://www.metalevel.at/prolog/optimization |
39 %https://www.metalevel.at/prolog/optimization |
40 |
40 |
41 % nice example for map and reduce using Harry potter characters |
41 % nice example for map and reduce using Harry potter characters |
42 % https://www.matthewgerstman.com/map-filter-reduce/ |
42 % https://www.matthewgerstman.com/map-filter-reduce/ |
43 |
43 |
|
44 |
|
45 % Timing |
|
46 % |
|
47 % xs.map(x => (x, xs.count(_==x))) |
|
48 % |
|
49 % vs xs.groupBy(identity) |
|
50 % |
|
51 % first is quadratic, while second is linear. |
|
52 |
|
53 % contrast map with a for loop in imperative languages |
|
54 % |
|
55 % Let’s use a simple example of calculating sales tax on an array of |
|
56 % prices. |
|
57 % |
|
58 % const prices = [19.99, 4.95, 25, 3.50]; |
|
59 % let new_prices = []; |
|
60 % |
|
61 % for(let i=0; i < prices.length; i++) { |
|
62 % new_prices.push(prices[i] * 1.06); |
|
63 % } |
|
64 % |
|
65 % We can achieve the same results using .map(): |
|
66 % |
|
67 % const prices = [19.99, 4.95, 25, 3.50]; |
|
68 % let new_prices = prices.map(price => price * 1.06); |
|
69 % |
|
70 % The syntax above is condensed so let’s walk through it a bit. The |
|
71 % .map() method takes a callback, which can be thought of as a function. |
|
72 % That’s what is between the parentheses. The variable price is the name |
|
73 % that will be used to identify each value. Since there’s only one |
|
74 % input, we can omit the usual parentheses around the parameters. |
|
75 |
|
76 % potentially a worked example? Tetris in scala.js |
|
77 % |
|
78 % https://medium.com/@michael.karen/learning-modern-javascript-with-tetris-92d532bcd057 |
|
79 % |
|
80 % Scala videos |
|
81 % https://www.youtube.com/user/DrMarkCLewis |
44 |
82 |
45 \begin{document} |
83 \begin{document} |
46 \fnote{\copyright{} Christian Urban, King's College London, 2017, 2018, 2019} |
84 \fnote{\copyright{} Christian Urban, King's College London, 2017, 2018, 2019} |
47 |
85 |
48 \section*{A Crash-Course in Scala} |
86 \section*{A Crash-Course in Scala} |
1007 without a mutable variable and without a for-loop. Obviously for simple things like |
1045 without a mutable variable and without a for-loop. Obviously for simple things like |
1008 sum, you could have written \code{xs.sum} in the first place. But not |
1046 sum, you could have written \code{xs.sum} in the first place. But not |
1009 all aggregate functions are pre-defined and often you have to write your |
1047 all aggregate functions are pre-defined and often you have to write your |
1010 own recursive function for this. |
1048 own recursive function for this. |
1011 |
1049 |
|
1050 \subsection*{Always Produce a Result! No Exceptions!} |
|
1051 % |
|
1052 %Function should always produce a value. Exception is not thrown. |
|
1053 %Whenever there is a possibility of non-value result (exception, void, |
|
1054 %undefined, null, etc.), it should be incorporated in the result type. |
|
1055 %Such types include but not limited to |
|
1056 % |
|
1057 %Option[T] |
|
1058 |
|
1059 |
1012 \subsection*{Higher-Order Functions} |
1060 \subsection*{Higher-Order Functions} |
1013 |
1061 |
1014 Functions obviously play a central role in functional programming. Two simple |
1062 Functions obviously play a central role in functional programming. Two simple |
1015 examples are |
1063 examples are |
1016 |
1064 |