- 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
sbtwithin 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/scalafolders -
create
src/test/resources,src/test/scalafolders -
create
build.sbtfile in the root folder and define project’s basic information likename,version,scalaVersionand a first dependency -
start sbt in the root folder
-
class,case class-
please do
task1andtask2
-
-
performing operations in constructor
-
please do
task3
-
-
object -
companion object &
apply-
please do
task4andtask5
-
-
create a class
SimplePersonwithStringfieldsfirstNameandlastName -
define a method to calculate
fullName- concatenatefirstNameandlastName -
create a unit test to cover the above logic
-
repeat the same from task1 but using
case classand class namePerson -
add additional assertion to check if
firstNameandlastNamehave proper values
-
create a case class
AutoPersonwith String fieldsfirstNameandlastName -
define a mutable String field
fullName -
calculate the
fullNamein 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
fullNameand assert its value -
with the second test, assert only the value from the first test
-
default arguments
-
named arguments
-
create a case class
Carwith a fieldmake: String -
define a method with at least two arguments
-
define the last argument with default value
-
-
combine all the arguments and
makefield 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
Driverwith at least two arguments:-
licenseId -
age
-
-
define a method
canDrive_? : Booleanto 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
task1andtask2
-
-
.empty,.filter,.map,.find,.count,.sortBy -
.get,.head,.headOption-
please do
task3andtask4
-
-
create a case class
CarMileagewith a fieldmileage: Seq -
create a unit test and init
CarMileageusingSeq(…) -
assert if
mileagevalue is the same as sequence usingelem :: Nilconstruction -
assert if a first element of
mileagevalue is the same usinghead :: tailconstruction
-
create a case class
CarGaragewith fieldcars: Map[String, Car] -
define a method to find a car by given model
-
define a method to count all cars in the
Garagematching given model -
create a unit test to cover the above logic
-
extend the case class
Carand add abroken: Booleanfield with default value set tofalse -
define additional method in
CarGarageto list cars which are broken -
create a unit test to cover the above logic
-
Option&None&Some-
please do
task1andtask2
-
-
.flatMap,.map,.flatten -
simple
for, for-comprehension-
please do
task3andtask4
-
-
extend the class
Carand define an optional fielddriverof typeDriverwith a default value -
create a unit test to cover creating a
Carwith and without the driver
-
extend
CarGaragewith a methodreadyCarswhich looks for cars with thedriverfield defined -
create a unit test to cover the logic
-
extend
CarGaragewith methodreadyDriverswhich looks for cars withdriverfield defined and returns those drivers -
create a unit test to cover the logic
-
trait,extends,with -
case objects (better than enums)
-
define a trait
Professionalwith methodprofessionalDriverLicense -
define a new case class
ProfessionalDriverwhich extendsSimplePersonand withProfessionaltrait -
implement missing method
-
you can extend
SimplePersonand add optional driver license field with default value use this value to implementprofessionalDriverLicensemethod -
or
-
define a new field in
ProfessionalDriverclass and use it to implement the method
-
-
create a unit test to cover logic of
professionalDriverLicense
-
change implementation of
professionalDriverLicenseinProfessionaltrait and returnmissingby default -
define a new case class
SimpleDriverwhich extendsSimplePersonand withProfessionaltrait -
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 `NormalandProfessionalimplementing the trait -
extend
Drivercase class and add optionaldriverTypefield of typeDriverType -
create a test case covering creating
Driverobjects with both values ofDriverType
-
match&case -
unapply
-
add additional type to
DriverType-Missing -
extend
Drivercase class and definedriverLicensemethod which returns driver’slicenseIdprefixed with given type -
if type isn’t defined return only
licenseId -
create a test case to cover this logic
-
change logic of
driverLicensemethod from task1 and ifdriverTypeequalsMissingor isNonereturnlicenseId -
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 ⇒ Doublehttps://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
Normaldriver can drive if his age is equal or over 18 -
a
Professionaldriver 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
CarMakewith two case objectsVWandFord(similar toDriverTypefrom module 5) -
define a case class
LuxuryCarwith aCarMakefield -
define a
passCertificationfunction which accepts a functionCarMake ⇒ Booleanand use it -
create a test case to check the implementation
-
if
VWit should pass the certification -
if
Fordit shouldn’t pass the certification
-