Skip to content

Day 4 / Mar 4 (Fri)

Opening Questions

  1. Are there any questions about the Scala topics covered?
  2. What was the most important Scala feature that we covered during the last session?

Exercise: User Class

Define User class with greet method that says "Hello from $name" when executed.

Scala Topics

  1. val vs def

    size("hello") // funkcja "hello".size // metoda

  2. class User(name: String) vs class User(val name: String) (note name parameter)

git

  • git installation on Windows 11
  • git commmands
  • init
  • status
  • add
  • commit
  • log
  • diff (head..head~1, using commit shas)

Scala Topics

  • scala.Predef
  • Scaladoc
  • String (it's java.lang.String)
  • scala.collection.StringOps
  • Higher-Order Functions (HOFs)
  • filter
  • sortBy
  • foreach
  • Underscore to mark unused input arguments to a function

    val size: String => Int = _ => 5

  • assert

    assert(size("hello") == "hello".size)

    assert("hello".sortBy(c => c.toInt) == "ehllo") assert("hello".sortBy(_.toInt) == "ehllo")

  • procedure vs method vs function

  • methods = class members
  • functions = standalone named executable block
  • procedure = method that returns nothing (Unit)

Exercise: StringOps Favorities

Pick two methods of scala.collection.StringOps and use Scala Worksheet with sbt console to learn how to use them:

  1. ++, concat
  2. filter
  3. drop
  4. apply
  5. sortBy
  6. tail
  7. foreach

Exercise: Convert function to method

Convert the f function to be a def to be used in filter.

val f: Char => Boolean = c => c != 'e' && c != 'o'

def f_def... = ???
"hello".filter(f_def) == "hll"

Exercise

Write last function that works like StringOps.last and uses the methods of StringOps we talked about today (except last).

val last: String => Char = ???

Hint: Use size and apply

Learning Scala

Working with the Learning Scala book, Chapter 8 "Classes".

Homework

Write nth function that returns the character at n-th position.

nth(s: String, n: Int): Char

Assume n as 0 <= n < s.length.

Breaks

  1. 9:50 - 10:00
  2. 11:00 - 11:10
  3. 12:30 - 13:15 lunch break

Working Hours

Day: Mar 4 (Fri)

8:30am - 3pm remotely

Back to top