- Introduction
This is a basic Scala workshop to allow smooth introduction into principal Scala features.
-
install JDK8
-
install
sbt
-
install IDE of your choice which supports Scala (IDEA Community Edition with Scala plugin is preferred)
-
checkout the project
git clone [email protected]:lukaszlenart/scala-basics.git
-
start
sbt
within the project’s folder to download all the dependencies
-
REPL, IntelliJ IDEA Scratch files
-
val
,var
,def
-
if
,for
-
String
,Int
-
generics
-
directory structure
-
configuration files
-
commands:
compile
,clean
,reload
,console
,test
,testOnly
-
create a folder for a new project
-
create
src/main/resources
,src/main/scala
folders -
create
src/test/resources
,src/test/scala
folders -
create
build.sbt
file in the root folder and define project’s basic information likename
,version
,scalaVersion
and a first dependency -
start sbt in the root folder
-
class
,case class
-
please do
task1
andtask2
-
-
performing operations in constructor
-
please do
task3
-
-
object
-
companion object &
apply
-
please do
task4
andtask5
-
-
create a class
SimplePerson
withString
fieldsfirstName
andlastName
-
define a method to calculate
fullName
- concatenatefirstName
andlastName
-
create a unit test to cover the above logic
-
repeat the same from task1 but using
case class
and class namePerson
-
add additional assertion to check if
firstName
andlastName
have proper values
-
create a case class
AutoPerson
with String fieldsfirstName
andlastName
-
define a mutable String field
fullName
-
calculate the
fullName
in constructor -
create a unit test to cover the above logic
-
create an object
PersonSingleton
-
define a mutable field
fullName
-
create two unit tests
-
in the first one assign value to
fullName
and assert its value -
with the second test, assert only the value from the first test
-
default arguments
-
named arguments
-
create a case class
Car
with a fieldmake: String
-
define a method with at least two arguments
-
define the last argument with default value
-
-
combine all the arguments and
make
field as a result -
create a unit test to cover calling the method using default value and passing specific value for the default argument
-
create a case class
Driver
with at least two arguments:-
licenseId
-
age
-
-
define a method
canDrive_? : Boolean
to check if driver’s age is over or equal 18 -
create a unit test to cover the method
-
add additional assertion and use
.copy()
with named parameter to define a new value for the age-
use assertion to check if the new value was properly re-defined
-
-
Seq
,List
,Map
, tuples -
elem :: Nil
,head :: tail
-
please do
task1
andtask2
-
-
.empty
,.filter
,.map
,.find
,.count
,.sortBy
-
.get
,.head
,.headOption
-
please do
task3
andtask4
-
-
create a case class
CarMileage
with a fieldmileage: Seq
-
create a unit test and init
CarMileage
usingSeq(…)
-
assert if
mileage
value is the same as sequence usingelem :: Nil
construction -
assert if a first element of
mileage
value is the same usinghead :: tail
construction
-
create a case class
CarGarage
with fieldcars: Map[String, Car]
-
define a method to find a car by given model
-
define a method to count all cars in the
Garage
matching given model -
create a unit test to cover the above logic
-
extend the case class
Car
and add abroken: Boolean
field with default value set tofalse
-
define additional method in
CarGarage
to list cars which are broken -
create a unit test to cover the above logic
-
Option
&None
&Some
-
please do
task1
andtask2
-
-
.flatMap
,.map
,.flatten
-
simple
for
, for-comprehension-
please do
task3
andtask4
-
-
extend the class
Car
and define an optional fielddriver
of typeDriver
with a default value -
create a unit test to cover creating a
Car
with and without the driver
-
extend
CarGarage
with a methodreadyCars
which looks for cars with thedriver
field defined -
create a unit test to cover the logic
-
extend
CarGarage
with methodreadyDrivers
which looks for cars withdriver
field defined and returns those drivers -
create a unit test to cover the logic
-
trait
,extends
,with
-
case objects (better than enums)
-
define a trait
Professional
with methodprofessionalDriverLicense
-
define a new case class
ProfessionalDriver
which extendsSimplePerson
and withProfessional
trait -
implement missing method
-
you can extend
SimplePerson
and add optional driver license field with default value use this value to implementprofessionalDriverLicense
method -
or
-
define a new field in
ProfessionalDriver
class and use it to implement the method
-
-
create a unit test to cover logic of
professionalDriverLicense
-
change implementation of
professionalDriverLicense
inProfessional
trait and returnmissing
by default -
define a new case class
SimpleDriver
which extendsSimplePerson
and withProfessional
trait -
create a unit test to cover logic of default implementation
-
define
sealed trait DriverType
-
create related companion object
-
inside the companion object add two
case object`s `Normal
andProfessional
implementing the trait -
extend
Driver
case class and add optionaldriverType
field of typeDriverType
-
create a test case covering creating
Driver
objects with both values ofDriverType
-
match
&case
-
unapply
-
add additional type to
DriverType
-Missing
-
extend
Driver
case class and definedriverLicense
method which returns driver’slicenseId
prefixed with given type -
if type isn’t defined return only
licenseId
-
create a test case to cover this logic
-
change logic of
driverLicense
method from task1 and ifdriverType
equalsMissing
or isNone
returnlicenseId
-
add additional test case to cover this logic
-
functions that accept functions `def func(calcFn: Double ⇒ Double): Double
-
functions that produce functions
def builder(input: Double): Double ⇒ Double
https://docs.scala-lang.org/tour/higher-order-functions.html
-
define an object with a function that will produce a function based on a `driver’s type and will accept the driver based on his age:
-
a
Normal
driver can drive if his age is equal or over 18 -
a
Professional
driver can drive if his age is equal or over 21 -
any other driver cannot drive
-
-
create a test case to check the implementation
-
define a sealed trait
CarMake
with two case objectsVW
andFord
(similar toDriverType
from module 5) -
define a case class
LuxuryCar
with aCarMake
field -
define a
passCertification
function which accepts a functionCarMake ⇒ Boolean
and use it -
create a test case to check the implementation
-
if
VW
it should pass the certification -
if
Ford
it shouldn’t pass the certification
-