<jupyter_start><jupyter_text>Partially Applied Function* Recall this<jupyter_code>def sumf (f:(Int) => Int, x:Int, y:Int): Int = {
   if (x > y) 0
   else f(x) + sumf(f,x+1,y)
}
sumf(num => num * num * num, 3, 4)<jupyter_output><empty_output><jupyter_text>* Lambda – define the function argument on the function call* How about the sum of squares?  Let's create a sum of squaresWe will provide a value for the first parameter (f), and leave the other ones as underbars "_"<jupyter_code>def sumofsquares = sumf(num => num * num, _:Int , _:Int)
sumofsquares(3,5)<jupyter_output><empty_output><jupyter_text>Unpacking Case Classes<jupyter_code>case class Item(name: String, price: Float, size: Int)
val item1 = Item("Spoon", 4.75F, 12)
item1
val Item(x,y,z) = item1
y<jupyter_output><empty_output><jupyter_text>The case class copy constructorchange the value of one or more fields<jupyter_code>val j = item1.copy(price = item1.price * 1.1F)
j<jupyter_output><empty_output><jupyter_text>Reducingreduce applies a function to each element in the collection to produces a running result. The running result, the element and result ***must be of the same type***.  So if each element is of type T, func( running_result:T, element:T ):TThe function func must commute.  You can't guarantee the running_total will be first and the element will be second.The result of one iteration becomes the running result of the next.<jupyter_code>val nice_numbers = Seq(5,6,234,1,46)
nice_numbers<jupyter_output><empty_output><jupyter_text>sum them<jupyter_code>nice_numbers.reduce( (acc, element) => acc + element)<jupyter_output><empty_output><jupyter_text>How about an average. Let's map each element to a tuple of (value, 1)<jupyter_code>val nice_tuples = nice_numbers.map( x => (x, 1))
nice_tuples<jupyter_output><empty_output><jupyter_text>reduce it by summing the values and summing the 1's<jupyter_code>nice_tuples.reduce( (acc, value) => (acc._1 + value._1, acc._2 + value._2 ))<jupyter_output><empty_output><jupyter_text>How Cool would it be to be able to add two tuples of ints.  Let's define a "+" on them using an *implicit* class. Because it's constructor is one element of type tuple (Int,Int), it adds it's methods to the type (Int,Int)<jupyter_code>implicit class TwoTupInt( twoTupInt: (Int,Int)) {
    def +(newTup:(Int,Int)) = (twoTupInt._1 + newTup._1, twoTupInt._2 + newTup._2)
}
(3,4) + (2,9)
nice_tuples.reduce((x,y) => x + y)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1164969450101833, 'fraction_numerical': 0.01670061099796334, 'mean_word_length': 6.1395348837209305, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '72876', 'license': 'MIT', 'n_tokens_mistral': 909, 'n_tokens_neox': 866, 'n_words': 271, 'path': 'Student-Resources/Labs/DataFrameLabs/Lab1-Scala-Examples.ipynb', 'repo_name': 'nthacker/LearnAnalytics-SparkML', 'size': '2455'}