Day 5 / Mar 7 (Mon)¶
Opening Questions¶
- Are there any questions about the Scala topics we covered so far?
- 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¶
- Type Constructors
- Known as "Generic Type" in Java
- Scaladoc
Placeholder Syntax¶
Learning Scala book, page 72
- Function literals
- Rocket operator
=>
- Rocket operator
- Demo:
doubler: Int => Int
function- Review doubler.sc
- Demo:
safeStringOp(s: String, f: String => String): String
to return the result of executingf
on the inputs
string whens != null
- Review safeStringOp.sc
-
Exercise: Write a higher-order function (similar to the above) that accepts three input arguments (
x
andy
ints and af
function that accepts two ints to produce an int) that executesf
on the two ints. How to execute it?def combination(x: Int, y: Int, f: (Int, Int) => Int): Int
- Review combination.sc
-
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:
Array
List
Map
Set
- 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¶
- 8:30 - 9:45
- 10:00 - 11:00
- 11:10 - 12:00
- 12:00 - 12:45 lunch break
- 12:45 - 1:30pm
- 1:40pm - 2:45pm
Working Hours¶
Day: Mar 7 (Mon)
- 8:30am - 3pm in the office
- 2 hours for own work