Last active
November 6, 2021 10:31
-
-
Save amuradyan/b1c711ffa28e86785e9d305d5121d571 to your computer and use it in GitHub Desktop.
Գրադարան Քեմփ - Ստեփանավան
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Purpose: show scala basic features in terms of grammar, readability, shortness, easy translation from human to machine | |
// How many of you know a programming language? | |
// Opening with the fizzbuzz | |
for (i <- Range.inclusive(1, 16)) { | |
println( | |
if (i % 3 == 0 && i % 5 == 0) "FizzBuzz" | |
else if (i % 3 == 0) "Fizz" | |
else if (i % 5 == 0) "Buzz" | |
else i | |
) | |
} | |
// Values | |
"asd" | |
'a' | |
true | |
false | |
12 | |
1.2 | |
1.2F | |
12L | |
12: Byte | |
12: Short | |
-12 | |
1 + 2 | |
3 * 4 | |
(3 + 0) * 9 | |
4 / 2 | |
9 / 3 | |
9 / 2 | |
4 / 3 | |
9 / 2F | |
-4 / 3D | |
// Strings | |
'a' + 'b' | |
"" | |
"aasdsdasdasd" | |
"ov ka stex?" | |
""" | |
- ay pnduk? | |
- mi asa tenc | |
""" | |
"asd" + "a3333333" | |
"a" == "a" | |
"a" == "b" | |
// Local Values and Variables | |
val a = 1 | |
res16 + res17 | |
a + 5 | |
val b = a + 5 | |
val a = 9 | |
var vr = 8 | |
vr = 9 | |
val f = 5 | |
val fl = 5L | |
val fll = 5: Long | |
val flll: Long = 5 | |
val o = s"asd $fll" | |
// Tuples | |
// Allows un/grouping things together fast | |
(1, 2) | |
(true, o) | |
val t = (res12, o, '4') | |
t._1 | |
t._2 | |
t._3 | |
val tt: (Int, Boolean) = (1, false) | |
val (mek, fols) = tt | |
mek | |
fols | |
// you need a way to move over elements | |
// tuples are examples of why programming with values is hard | |
val s = List(1, 2, 3, 4) | |
s(1) | |
s ++ s | |
s -- s | |
val ss = List(s, s) | |
ss(1)(2) | |
s.sum | |
s.head | |
s.tail | |
val tl = List((1, "mek"), (2, "erku")) | |
// A better list of tuples | |
val m = Map(1 -> "mek", 2 -> "erku") | |
// Loops | |
// do a foreach | |
var sum = 0 | |
for (n <- s) sum += n | |
sum | |
var summ = 0 | |
for (n <- Range(0, 101)) summ += n | |
summ | |
val indicies = List(2, 0, 1) | |
val values = List(5, 6, 7) | |
val nested = List(indicies, values) | |
var total_sum_ = 0 | |
for (l <- nested; v <- l) total_sum_ += v | |
total_sum_ | |
var tot_even_sum = 0 | |
for (l <- nested; v <- l; if v % 2 == 0) tot_even_sum += v | |
tot_even_sum | |
val onetoten = for (n <- Range(0, 10)) yield n | |
onetoten.sum | |
{ for {n <- Range(0, 5)} yield n }.sum | |
{5} // expression based | |
val nine = {8;9} | |
for { | |
l <- nested; | |
v <- l; | |
if v % 2 == 0 | |
} tot_even_sum += v | |
tot_even_sum | |
// side effects are one more thing to track | |
// Functions | |
// who heard about functions | |
8.5 * 8.5 | |
8.5 * 8.5 | |
8.5 * 8.5 | |
8.5 * 8.5 | |
8.5 * 8.5 | |
val konkret_tiv = 9 | |
konkret_tiv * konkret_tiv | |
konkret_tiv * konkret_tiv | |
konkret_tiv * konkret_tiv | |
konkret_tiv * konkret_tiv | |
konkret_tiv * konkret_tiv | |
konkret_tiv * konkret_tiv * konkret_tiv | |
konkret_tiv * konkret_tiv * konkret_tiv | |
konkret_tiv * konkret_tiv * konkret_tiv | |
konkret_tiv * konkret_tiv * konkret_tiv | |
konkret_tiv * konkret_tiv * konkret_tiv | |
def hashvark(voreve_tiv: Int) = voreve_tiv * voreve_tiv * voreve_tiv | |
hashvark(konkret_tiv) | |
def printHello = println("Hello") | |
printHello | |
// Round braces are not needed, Scala tried to help you type less | |
def greet(greeting: String): Unit = println(greeting) | |
greet("Hi") | |
def greetNTimes(greeting: String, times: Int): Unit = for (i <- Range(0, times)) println(greeting) | |
greetNTimes("Ahoj", 3) | |
def greetNTimess(greeting: String, times: Int): Unit = for (_ <- Range(0, times)) println(greeting) | |
greetNTimes("Ahoj", 3) | |
def greetNTimesWD(greeting: String, times: Int = 1): Unit = for (i <- Range(0, times)) println(greeting) | |
greetNTimesWD("Ola") | |
def greetNTimesWDD(times: Int = 1, greeting: String): Unit = for (i <- Range(0, times)) println(greeting) | |
greetNTimesWDD(greeting = "Ola") | |
// The return type is optional but good practice | |
def add(a: Int, b: Int): Int = a + b | |
add(1, 2) | |
val addl: (Int, Int) => Int = (a, b) => a + b | |
addl(1, 2) | |
def uhashvark(voreve_tiv: Int)(voreve_ayl_tiv: Int)(ayl_voreve_tiv: Int): Int = voreve_tiv * voreve_ayl_tiv * ayl_voreve_tiv | |
val mas_1 = uhashvark(7) | |
val mas_2 = mas_1(2) | |
mas_2(5) | |
val uhashvarkl: Int => Int => Int => Int = | |
voreve_tiv => voreve_ayl_tiv => ayl_voreve_tiv => voreve_tiv * voreve_ayl_tiv * ayl_voreve_tiv | |
val umas_1 = uhashvarkl(7) | |
val umas_2 = mas_1(2) | |
umas_2(5) | |
// Classes | |
// a better tuple / product type | |
("Vanilla/Strawberry", 5) | |
("Cinnamon/Chocolate", 7) | |
class Marojni(val name: String, val price: Int) | |
val vs = Marojni("Vanilla/Strawberry", 5) | |
vs.name | |
val cc = Marojni("Cinnamon/Chocolate", 7) | |
cc.price | |
// Thank you |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspired by the Hands-on Scala Programming