Skip to content

keramiozsoy/scala-101

Repository files navigation

scala-101

resource

install scala3

clone project on your local

cd $HOME && git clone https://github.com/keramiozsoy/scala-101.git && cd scala-101

check installed scala

scala --version
Scala code runner version 3.3.0 -- lsCopyright 2002-2023, LAMP/EPFL

scala-repl

  • 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

create your first scala program with sbt tool

  • 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-world
sbt clean && sbt run
Hello world!
I was compiled by Scala 3. :)

var type

  • var type is immutable.

  • run below code. value of variable is changed.

  var a = 0;
  
  a = 5;
cd $HOME/scala-101/var-type
sbt clean && sbt run

val type

  • val type is immutable.

  • run below code which wil not work.

  val a = 0

  a = 5; // wrong
cd $HOME/scala-101/val-type/src/main/scala
sbt clean && sbt run

declaring variable

If 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

strings

  • 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
                      CCCC
cd $HOME/scala-101/strings
sbt clean && sbt run

if else condition

  • 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 positive
cd $HOME/scala-101/if-else
sbt clean && sbt run

for loop condition

  • 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-loop
sbt clean && sbt run

for loop guards condition

  • Guards which helps when you need to use multiple if expression on for loop.
cd $HOME/scala-101/for-loop-guards
sbt clean && sbt run

for loop yield condition

  • using yield keyword ( instead of do keyword ) will returns a result after completing of for loop iterations.
cd $HOME/scala-101/for-loop-yield
sbt clean && sbt run

match condition

  • it helps to find result from options
cd $HOME/scala-101/match-condition
sbt clean && sbt run

try-catch-finally

  • It is control structure lets you catch exceptions.
cd $HOME/scala-101/try-catch-finally
sbt clean && sbt run

while

  • It seems like other loop, (for) it stops loop when condition is founded.
cd $HOME/scala-101/while
sbt clean && sbt run

domain modeling ( TODO )

  • 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.

methods

A method means a function takes parameters and return a value.

def methodName(param1: Type1, param2: Type2): ReturnType =
cd $HOME/scala-101/methods
sbt clean && sbt run

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages