From 55155f0e71cde9890ad27757d003c8ba0363d156 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 4 Feb 2021 02:34:28 -0500 Subject: [PATCH] Slide updates --- src/teaching/cse-562/2021sp/index.erb | 1 + .../cse-562/2021sp/slide/2021-02-04-Scala.erb | 155 +++++++++++++++--- 2 files changed, 134 insertions(+), 22 deletions(-) diff --git a/src/teaching/cse-562/2021sp/index.erb b/src/teaching/cse-562/2021sp/index.erb index 399edc0a..a42f725a 100644 --- a/src/teaching/cse-562/2021sp/index.erb +++ b/src/teaching/cse-562/2021sp/index.erb @@ -10,6 +10,7 @@ schedule: topic: "Scala Primer" materials: lecture: https://youtu.be/0TndAT2MkPs + slides: slide/2021-02-04-Scala.html - date: "Feb. 9" topic: "Relational Algebra + Spark" - date: "Feb. 11" diff --git a/src/teaching/cse-562/2021sp/slide/2021-02-04-Scala.erb b/src/teaching/cse-562/2021sp/slide/2021-02-04-Scala.erb index ff4e4467..32474677 100644 --- a/src/teaching/cse-562/2021sp/slide/2021-02-04-Scala.erb +++ b/src/teaching/cse-562/2021sp/slide/2021-02-04-Scala.erb @@ -5,6 +5,16 @@ date: Feb 4, 2021 ---
+
+

News

+

Checkpoint 0 will be posted tonight or tomorrow.

+

Class actually starts at 12:45 (but I'll be around from 12:30 to answer questions).

+
+ +
+
+
+

My assumptions

@@ -22,10 +32,10 @@ date: Feb 4, 2021
Package
An organizational unit clustering related functionality
+
Class
+
A group of related code that applies to entities of a type (Like Java)
Object
A 'singleton' class. (Like Java's static)
-
Companion Object
-
A 'singleton' class implementing methods related to a class
Case Class
A class with bonus features (we'll discuss shortly).
@@ -38,14 +48,12 @@ date: Feb 4, 2021

   package edu.buffalo.myapp
-
   import java.io.File
   import scala.io._
 
   object MyApp
   {
     val message: String = "Hello World"
-
     def main(args: Array[String]): Unit = {
       println(message)
       var stream: Stream = Stream.fromFile(new File("Hello.txt"))
@@ -96,7 +104,7 @@ date: Feb 4, 2021
     

   class MyClass(name: String, age: Int) { 
     
- Classes are defined much like java, except constructor fields are given directly in the class definitions. e.g., you would instantiate this class as new MyClass("Bob", "102"). + Classes are defined much like java, except constructor fields are given directly in the class definitions. e.g., you would instantiate this class as new MyClass("Bob", 102).
@@ -108,7 +116,7 @@ date: Feb 4, 2021 with MyOtherTrait { - Inheritence is defined with the extends keyword. Like Java, Scala only allows single inheritance, but you add define interfaces and mixins through the with keyword. + Inheritence is defined with the extends keyword. Like Java, Scala only allows single inheritance, but you add interfaces and mixins through the with keyword.
Objects can also use the extends and with keywords. @@ -267,9 +275,9 @@ date: Feb 4, 2021

Tuples


-  val a = (1, "Cookie", "Alice")
-  val b = (2, "Cake", "Bolesław")
-  val all = Seq(a, b)
+  val a                               = (1, "Cookie", "Alice")
+  val b: (Int, String, String)        = (2, "Cake", "Bolesław")
+  val all: Seq[(Int, String, String)] = Seq(a, b)
     

Scala also has a "Tuple" type (like Python).

@@ -286,12 +294,64 @@ date: Feb 4, 2021
    -
  • .map { v => ... }: Get a new collection by transforming every element of the collection using the lambda.
  • -
  • .filter { v => ... }: Get a new collection by deleting every element of the collection on which the lambda returns false.
  • -
  • .flatten: Assuming the target is a collection of collections, get a new collection by concatenating all of the nested collections.
  • +
  • .toMap: Convert any collection of 2-tuples to a Map.
  • +
  • .toSeq: Convert any collection into a sequence.
  • +
  • .toIndexedSeq: Convert any collection into an indexed sequence.
  • +
  • .toSet: Convert any collection into a set.
+
+

So how about those immutable collections...

+ +

Why does a collection need to be mutable?

+
+ +
+

Common Patterns...

+

+  public int add_one(collection: List<Int>) {
+    ArrayList<Int> result = new ArrayList<Int>();
+    for(element : collection){
+      result.append(element + 1)
+    }
+    return result
+  }
+    
+
+ +
+ .map { v => ... }: Get a new collection by transforming every element of the collection using the lambda. + +

+  collection.map { x => x+1 }
+    
+ +
+ +
+

Common Patterns...

+

+  public int only_big(collection: List<Int>) {
+    ArrayList<Int> result = new ArrayList<Int>();
+    for(element : collection){
+      if(element > 100){
+        result.append(element)
+      }
+    }
+    return result
+  }
+    
+
+ +
+ .filter { v => ... }: Get a new collection by deleting every element of the collection on which the lambda returns false. + +

+  collection.filter { _ > 100 }
+    
+
+

   all.filter { x => x._2.equals("Cookie") }
@@ -301,25 +361,49 @@ date: Feb 4, 2021
   
-
    -
  • .foldLeft(x) { (accum, v) => ... }: Start with x. Apply the lambda to (x, firstElement) to get a new x. Repeat for every element of the target and return the final value of x.
  • -
+

Common Patterns...

+

+  public int flatten(collection: List<List<Int>>) {
+    ArrayList<Int> result = new ArrayList<Int>();
+    for(nested : collection){
+      for(element : nested){
+        result.append(element)
+      }
+    }
+    return result
+  }
+    
+ .flatten: Assuming the target is a collection of collections, get a new collection by concatenating all of the nested collections.

-  all.fold(0) { (accum, v) => accum + v._1 }
+  collection.flatten
     
- Returns 3 (the sum of the first tuple field)
-

A Shorthand

-

-  all.fold(0) { _ + _._1 }
+    

Common Patterns...

+

+  public int sum(collection: List<Int>) {
+    int accum = 0;
+    for(element : collection){
+      accum += element
+    }
+    return accum
+  }
     
+
- Underscores can sometimes be used as shorthands for lambda functions when a variable is only used once. +
+ .foldLeft(x) { (accum, v) => ... }: Start with x. Apply the lambda to (x, firstElement) to get a new x. Repeat for every element of the target and return the accumulator. + +

+  collection.foldLeft(0) { (accum, element) => accum + element }
+    
+

+  collection.foldLeft(0) { _ + _ }
+    
@@ -377,7 +461,8 @@ date: Feb 4, 2021 val myFoo = Foo("Abe", 1) -

For example, you don't need to use "new" to construct one.

+

For example, you don't need to use "new" to construct one

+

and accessors are defined for all of the constructor variables.

@@ -451,7 +536,33 @@ organization := "edu.buffalo.cse.odin",
+
+

Testing

+

Specs2 is my unit testing-framework of choice.

+ +

+class HelloWorldSpec extends Specification {
+  "This is a specification for the 'Hello world' string".txt
+
+  "The 'Hello world' string should" >> {
+    "contain 11 characters" >> {
+      "Hello world" must haveSize(11)
+    }
+    "start with 'Hello'" >> {
+      "Hello world" must startWith("Hello")
+    }
+    "end with 'world'" >> {
+      "Hello world" must endWith("world")
+    }
+  }
+}
+
+    
+
+

Many IDEs provide SBT integration. See the Scala Getting Started Page and Scala Metals Page for more details.

+ +

If you prefer a text editor, check out Bloop.