Slide updates
This commit is contained in:
parent
e557a4b024
commit
55155f0e71
|
@ -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"
|
||||
|
|
|
@ -5,6 +5,16 @@ date: Feb 4, 2021
|
|||
---
|
||||
|
||||
<section>
|
||||
<section>
|
||||
<h3>News</h3>
|
||||
<p>Checkpoint 0 will be posted tonight or tomorrow.</p>
|
||||
<p>Class actually starts at 12:45 (but I'll be around from 12:30 to answer questions).</p>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<br/>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h3>My assumptions</h3>
|
||||
|
||||
|
@ -22,10 +32,10 @@ date: Feb 4, 2021
|
|||
<dl>
|
||||
<dt>Package</dt>
|
||||
<dd>An organizational unit clustering related functionality</dd>
|
||||
<dt>Class</dt>
|
||||
<dd>A group of related code that applies to entities of a type (Like Java)</dd>
|
||||
<dt>Object</dt>
|
||||
<dd>A 'singleton' class. (Like Java's <b>static</b>)</dd>
|
||||
<dt>Companion Object</dt>
|
||||
<dd>A 'singleton' class implementing methods related to a class</dd>
|
||||
<dt>Case Class</dt>
|
||||
<dd>A class with bonus features (we'll discuss shortly).</dd>
|
||||
</dl>
|
||||
|
@ -38,14 +48,12 @@ date: Feb 4, 2021
|
|||
|
||||
<pre><code class="scala">
|
||||
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
|
|||
<pre><code class="scala">
|
||||
class MyClass(name: String, age: Int) {
|
||||
</code></pre>
|
||||
Classes are defined much like java, except constructor fields are given directly in the class definitions. e.g., you would instantiate this class as <tt>new MyClass("Bob", "102")</tt>.
|
||||
Classes are defined much like java, except constructor fields are given directly in the class definitions. e.g., you would instantiate this class as <tt>new MyClass("Bob", 102)</tt>.
|
||||
</div>
|
||||
</section>
|
||||
|
||||
|
@ -108,7 +116,7 @@ date: Feb 4, 2021
|
|||
with MyOtherTrait {
|
||||
</code></pre>
|
||||
|
||||
Inheritence is defined with the <tt>extends</tt> keyword. Like Java, Scala only allows single inheritance, but you add define interfaces and mixins through the <tt>with</tt> keyword.
|
||||
Inheritence is defined with the <tt>extends</tt> keyword. Like Java, Scala only allows single inheritance, but you add interfaces and mixins through the <tt>with</tt> keyword.
|
||||
|
||||
<div class="fragment">
|
||||
Objects can also use the <tt>extends</tt> and <tt>with</tt> keywords.
|
||||
|
@ -267,9 +275,9 @@ date: Feb 4, 2021
|
|||
<h3>Tuples</h3>
|
||||
|
||||
<pre><code class="scala">
|
||||
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)
|
||||
</code></pre>
|
||||
|
||||
<p>Scala also has a "Tuple" type (like Python).</p>
|
||||
|
@ -286,12 +294,64 @@ date: Feb 4, 2021
|
|||
|
||||
<section>
|
||||
<ul>
|
||||
<li><tt>.map { v => ... }</tt>: Get a new collection by transforming every element of the collection using the lambda.</li>
|
||||
<li><tt>.filter { v => ... }</tt>: Get a new collection by deleting every element of the collection on which the lambda returns false.</li>
|
||||
<li><tt>.flatten</tt>: Assuming the target is a collection of collections, get a new collection by concatenating all of the nested collections.</li>
|
||||
<li><tt>.toMap</tt>: Convert any collection of 2-tuples to a Map.</li>
|
||||
<li><tt>.toSeq</tt>: Convert any collection into a sequence.</li>
|
||||
<li><tt>.toIndexedSeq</tt>: Convert any collection into an indexed sequence.</li>
|
||||
<li><tt>.toSet</tt>: Convert any collection into a set.</li>
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<p>So how about those immutable collections...</p>
|
||||
|
||||
<p class="fragment">Why does a collection need to be mutable?</p>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h3>Common Patterns... </h3>
|
||||
<pre><code class="java">
|
||||
public int add_one(collection: List<Int>) {
|
||||
ArrayList<Int> result = new ArrayList<Int>();
|
||||
for(element : collection){
|
||||
result.append(element + 1)
|
||||
}
|
||||
return result
|
||||
}
|
||||
</code></pre>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<tt>.map { v => ... }</tt>: Get a new collection by transforming every element of the collection using the lambda.
|
||||
|
||||
<pre><code class="scala">
|
||||
collection.map { x => x+1 }
|
||||
</code></pre>
|
||||
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h3>Common Patterns... </h3>
|
||||
<pre><code class="java">
|
||||
public int only_big(collection: List<Int>) {
|
||||
ArrayList<Int> result = new ArrayList<Int>();
|
||||
for(element : collection){
|
||||
if(element > 100){
|
||||
result.append(element)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
</code></pre>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<tt>.filter { v => ... }</tt>: Get a new collection by deleting every element of the collection on which the lambda returns false.
|
||||
|
||||
<pre><code class="scala">
|
||||
collection.filter { _ > 100 }
|
||||
</code></pre>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<pre><code class="scala">
|
||||
all.filter { x => x._2.equals("Cookie") }
|
||||
|
@ -301,25 +361,49 @@ date: Feb 4, 2021
|
|||
</section>
|
||||
|
||||
<section>
|
||||
<ul>
|
||||
<li><tt>.foldLeft(x) { (accum, v) => ... }</tt>: 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.</li>
|
||||
</ul>
|
||||
<h3>Common Patterns... </h3>
|
||||
<pre><code class="java">
|
||||
public int flatten(collection: List<List<Int>>) {
|
||||
ArrayList<Int> result = new ArrayList<Int>();
|
||||
for(nested : collection){
|
||||
for(element : nested){
|
||||
result.append(element)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
</code></pre>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<tt>.flatten</tt>: Assuming the target is a collection of collections, get a new collection by concatenating all of the nested collections.
|
||||
<pre><code class="scala">
|
||||
all.fold(0) { (accum, v) => accum + v._1 }
|
||||
collection.flatten
|
||||
</code></pre>
|
||||
Returns <tt>3</tt> (the sum of the first tuple field)
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h3>A Shorthand</h3>
|
||||
<pre><code class="scala">
|
||||
all.fold(0) { _ + _._1 }
|
||||
<h3>Common Patterns... </h3>
|
||||
<pre><code class="java">
|
||||
public int sum(collection: List<Int>) {
|
||||
int accum = 0;
|
||||
for(element : collection){
|
||||
accum += element
|
||||
}
|
||||
return accum
|
||||
}
|
||||
</code></pre>
|
||||
</section>
|
||||
|
||||
Underscores can <i>sometimes</i> be used as shorthands for lambda functions when a variable is only used once.
|
||||
<section>
|
||||
<tt>.foldLeft(x) { (accum, v) => ... }</tt>: 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.</li>
|
||||
|
||||
<pre><code class="scala">
|
||||
collection.foldLeft(0) { (accum, element) => accum + element }
|
||||
</code></pre>
|
||||
<pre class="fragment"><code class="scala">
|
||||
collection.foldLeft(0) { _ + _ }
|
||||
</code></pre>
|
||||
</section>
|
||||
|
||||
</section>
|
||||
|
@ -377,7 +461,8 @@ date: Feb 4, 2021
|
|||
val myFoo = Foo("Abe", 1)
|
||||
</code></pre>
|
||||
|
||||
<p>For example, you don't need to use "new" to construct one.</p>
|
||||
<p>For example, you don't need to use "new" to construct one</p>
|
||||
<p>and accessors are defined for all of the constructor variables.</p>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
|
@ -451,7 +536,33 @@ organization := "edu.buffalo.cse.odin",
|
|||
</code></pre>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h3>Testing</h3>
|
||||
<p><a href="http://etorreborre.github.io/specs2/">Specs2</a> is my unit testing-framework of choice.</p>
|
||||
|
||||
<pre><code class="scala">
|
||||
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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
</code></pre>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<p>Many IDEs provide SBT integration. See the Scala <a href="https://docs.scala-lang.org/getting-started/index.html">Getting Started Page</a> and Scala <a href="https://scalameta.org/metals/">Metals Page</a> for more details.</p>
|
||||
|
||||
<p>If you prefer a text editor, check out <a href="https://scalacenter.github.io/bloop/">Bloop</a>.</p>
|
||||
</section>
|
||||
</section>
|
||||
|
|
Loading…
Reference in a new issue