Day 4 / Mar 4 (Fri)¶
Opening Questions¶
- Are there any questions about the Scala topics covered?
- 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¶
-
val
vsdef
size("hello") // funkcja "hello".size // metoda
-
class User(name: String)
vsclass User(val name: String)
(notename
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'sjava.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:
++
,concat
filter
drop
apply
sortBy
tail
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¶
- 9:50 - 10:00
- 11:00 - 11:10
- 12:30 - 13:15 lunch break
Working Hours¶
Day: Mar 4 (Fri)
8:30am - 3pm remotely