<jupyter_start><jupyter_text>Lecture:<jupyter_code>1 + 1
println("Sup, World?")
res1
res2
// interestingly, results don't apply to print statement outputs.<jupyter_output><empty_output><jupyter_text>Assignment: Scala The Hard Way _**1. Basics**_<jupyter_code>/*
The

basic basics

are 
*/

//...all about how to comment.
println("Sup World!")
println(100)
print("Sup World!")
print(100)
val x = 10
x = 20

// get an error message in the console
var y = 10
y = 20
val z: Int = 10
val a: Double = 1.0
val b: Double = 10
true
false
!true
!false
true == false
10 > 5
1 + 1
2 - 1
5 * 3
6 / 2
6 / 4
6.0 / 4
6 / 4.0

// Math is basically the same thing.
'a'
"a"
"hello world".length
"hello world".substring(2, 6)
"hello world".replace("C", "3")
"hello world".take(5)
"hello world".drop(5)
val n = 45
s"We have $n apples"
val a = Array(11, 9, 6)
s"My second daughter is ${a(0) - a(2)} years old."
s"We have double the amount of ${n / 2.0} in apples."
s"Power of 2: ${math.pow(2, 2)}"
// Exponents are a bit different. 
f"Power of 5: ${math.pow(5, 2)}%1.0f"  
f"Square root of 122: ${math.sqrt(122)}%1.4f"
raw"New line feed: \n. Carriage return: \r."
"They stood outside the \"Rose and Crown\""
val html = """<form id="daform">
                <p>Press belo', Joe</p>
                <input type="submit">
              </form>"""<jupyter_output><empty_output><jupyter_text>_**2. Functions**_<jupyter_code>def sumOfSquares(x: Int, y: Int): Int = {
  val x2 = x * x
  val y2 = y * y
  x2 + y2
}
def sumOfSquaresShort(x: Int, y: Int): Int = x * x + y * y
sumOfSquares(3, 4)
def subtract(x: Int, y: Int): Int = x - y

subtract(10, 3)
subtract(y=10, x=3)
def sq(x: Int) = x * x
def addWithDefault(x: Int, y: Int = 5) = x + y
addWithDefault(1, 2) 
addWithDefault(1)    
(x: Int) => x * x // Anonymous functions
val sq: Int => Int = x => x * x
sq(10)
val addOne: Int => Int = _ + 1
val weirdSum: (Int, Int) => Int = (_ * 2 + _ * 3)

addOne(5)
weirdSum(2,4)
def foo(x: Int): Int = {
  val anonFunc: Int => Int = { z =>
    if (z > 5)
      return z
    else
      z + 2
  }
  anonFunc(x)
}

// Avoid using 'return'. Error-prone in Scala.<jupyter_output><empty_output><jupyter_text>_**3. Flow Control**_<jupyter_code>1 to 5
val r = 1 to 5
r.foreach(println)
r foreach println
(5 to 1 by -1) foreach (println)
var i = 0
while (i < 10) { println("i " + i); i += 1 }

while (i < 10) { println("i " + i); i += 1 }
i
i = 0
do {
  println("i is still less than 10")
  i += 1
} while (i < 10)
def showNumbersInRange(a: Int, b: Int): Unit = {
  print(a)
  if (a < b)
    showNumbersInRange(a + 1, b)
}
showNumbersInRange(1, 14)
val x = 10

if (x == 1) println("yeah")
if (x == 10) println("yeah")
if (x == 11) println("yeah")
if (x == 11) println("yeah") else println("nay")

println(if (x == 10) "yeah" else "nope")
val text = if (x == 10) "yeah" else "nope"<jupyter_output><empty_output><jupyter_text>_**4. Data Structures**_<jupyter_code>val a = Array(1, 2, 3, 5, 8, 13)
a(0)
a(3)     
a(21)    
// error thrown for a(21)
(1, 2)

(4, 3, 2)

(1, 2, "three")

(a, 2, "three")
val divideInts = (x: Int, y: Int) => (x / y, x % y)
divideInts(10, 3)
val d = divideInts(10, 3)
// 1-based index
d._1
d._2
val (div, mod) = divideInts(10, 3)
div
mod<jupyter_output><empty_output><jupyter_text>_**7. Functional Programming**_<jupyter_code>val add10: Int => Int = _ + 10 
List(1, 2, 3) map add10 
List(1, 2, 3) map (x => x + 10)
List(1, 2, 3) map (_ + 10)
List("Dom", "Bob", "Natalia") foreach println
s.map(sq)

val sSquared = s. map(sq)

sSquared.filter(_ < 10)

sSquared.reduce (_+_)
List(1, 2, 3) filter (_ > 2) 
case class Person(name: String, age: Int)
List(
  Person(name = "Dom", age = 23),
  Person(name = "Bob", age = 30)
).filter(_.age > 25) 
val aListOfNumbers = List(1, 2, 3, 4, 10, 20, 100)
aListOfNumbers foreach (x => println(x))
aListOfNumbers foreach println

for { n <- s } yield sq(n)

val nSquared2 = for { n <- s } yield sq(n)

for { n <- nSquared2 if n < 10 } yield n

for { n <- s; nSquared = n * n if nSquared < 10} yield nSquared<jupyter_output><empty_output><jupyter_text>_**9. Misc**_<jupyter_code>import scala.collection.immutable.List
import scala.collection.immutable._
import scala.collection.immutable.{List, Map}
import scala.collection.immutable.{List => ImmutableList}
import scala.collection.immutable.{Map => _, Set => _, _}

object Application {
  def main(args: Array[String]): Unit = {
    // stuff goes here.
  }
}<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17632524707996405, 'fraction_numerical': 0.050314465408805034, 'mean_word_length': 3.5300101729399795, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 52, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '424139', 'license': 'MIT', 'n_tokens_mistral': 2039, 'n_tokens_neox': 1852, 'n_words': 572, 'path': 'module2-scala-for-spark/NB_1.ipynb', 'repo_name': 'axrd/DS-Unit-3-Sprint-3-Big-Data', 'size': '4452'}