Skip to content

Day 5 / Mar 7 (Mon)

Opening Questions

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

Review 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.


Review nth.sc for a solution.

Option

  1. Type Constructors
    • Known as "Generic Type" in Java
  2. Scaladoc

Placeholder Syntax

Learning Scala book, page 72

  1. Function literals
    • Rocket operator =>
  2. Demo: doubler: Int => Int function
  3. Demo: safeStringOp(s: String, f: String => String): String to return the result of executing f on the input s string when s != null
  4. Exercise: Write a higher-order function (similar to the above) that accepts three input arguments (x and y ints and a f function that accepts two ints to produce an int) that executes f on the two ints. How to execute it?

    def combination(x: Int, y: Int, f: (Int, Int) => Int): Int

  5. Very useful for Scala Collections library (coming up next)

Scala Collections Library

Learning Scala book, Chapter 6, page 83

Package: scala.collections

Data structures for collecting one or more values of a given type:

  1. Array
  2. List
  3. Map
  4. Set
  5. others

Building blocks of modern software projects

Iterable - the foundation for iteration and manipulation

Demo: Creating instances of List, Set and Map

Exercise: Review the scaladoc of List. Pick two higher-order methods (methods with a function argument). Write a sample code for each (to demo how they work).

Schedule

  1. 8:30 - 9:45
  2. 10:00 - 11:00
  3. 11:10 - 12:00
  4. 12:00 - 12:45 lunch break
  5. 12:45 - 1:30pm
  6. 1:40pm - 2:45pm

Working Hours

Day: Mar 7 (Mon)

  • 8:30am - 3pm in the office
  • 2 hours for own work
Back to top