Mastering Scala: Unlock the Power of Functional and Object-Oriented Programming
cd $HOME && git clone https://github.com/keramiozsoy/scala-101.git && cd scala-101scala --versionOutput
Scala code runner version 3.3.0 -- lsCopyright 2002-2023, LAMP/EPFL-
the scala repl ( READ - EVALUATE - PRINT - LOOP ) is a command-line interpreter that you use a test your scala code on terminal.
-
open terminal to open repl
scala- code
1 + 2- ouput
val res0: Int = 3- code
var a = 2 * 5- output
var a: Int = 10- The sbt help to run our project when we have one or multiple files
cd $HOME- create project
sbt new scala/scala3.g8- enter project name like below
hello-world- go to file that have .sbt extension
cd hello-worldsbt clean && sbt runHello world!
I was compiled by Scala 3. :)-
var type is immutable.
-
run below code. value of variable is changed.
var a = 0;
a = 5;cd $HOME/scala-101/var-typesbt clean && sbt run-
val type is immutable.
-
run below code which wil not work.
val a = 0
a = 5; // wrongcd $HOME/scala-101/val-type/src/main/scalasbt clean && sbt runIf you dont want to give type, compiler will understand, no problem
// explicit
val a:Int = 1
// implicit; the compiler infers the type
val b = 2 cd $HOME/scala-101/declaring-variable sbt clean && sbt run- You can use one character or sentence or multiline to define.
val first = "AAAA" // String
println(first)
val second = 'B' // Char
println(second)
println(s"Result: $first and $second") // combine string and char
println(s"Calculate: ${3 * 5} ") //
val multiline = """AAA
BBBBB
CCCC"""
println(multiline)- output
AAAA
B
Result: AAAA and B
Calculate: 15
AAA
BBBBB
CCCCcd $HOME/scala-101/stringssbt clean && sbt run- The if/else statement executes a block of code if a specified condition is true. If the condition is false, another block of code can be executed.
val x = 10
if x < 0 then
println("x is negative")
else if x == 0 then
println ("x is zero")
else
println("x is positive")- output
x is positivecd $HOME/scala-101/if-elsesbt clean && sbt run-
let's assume that we have got list of numbers and, we will print all values. for condition helps to do it.
-
for -> it starts loop
-
do -> to do something while for loop continue, like body of for loop
cd $HOME/scala-101/for-loopsbt clean && sbt run- Guards which helps when you need to use multiple if expression on for loop.
cd $HOME/scala-101/for-loop-guardssbt clean && sbt run- using yield keyword ( instead of do keyword ) will returns a result after completing of for loop iterations.
cd $HOME/scala-101/for-loop-yieldsbt clean && sbt run- it helps to find result from options
cd $HOME/scala-101/match-conditionsbt clean && sbt run- It is control structure lets you catch exceptions.
cd $HOME/scala-101/try-catch-finallysbt clean && sbt run- It seems like other loop, (for) it stops loop when condition is founded.
cd $HOME/scala-101/whilesbt clean && sbt run- Scala supports two paradigms. The paradigm means a way of looking at something, set of idea.
- First paradigm is object-oriented programming
- Second paradigm is functional programming.
- We will look at these paradigm later on.
A method means a function takes parameters and return a value.
def methodName(param1: Type1, param2: Type2): ReturnType =cd $HOME/scala-101/methodssbt clean && sbt run