-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scala Notebook.scala
172 lines (132 loc) · 3.41 KB
/
Scala Notebook.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Databricks notebook source
// Value are immutable
val hello : String = "Hola"
// COMMAND ----------
// Variable are mutable
var hello : String = "hello"
var hello1 = hello + " World"
// COMMAND ----------
// Data type
val numberone : Int = 1
val truth: Boolean = true
val letterA: Char = 'a'
val pi: Double = 3.1415926
val piSInglePrecision: Float = 3.1415f
val bigNumber: Long = 1232434534
val smallNumber: Byte = 127
// COMMAND ----------
println("Here is the number : "+numberone)
// or
// $ represent var or val %means floating .3f 3 digit after float
println(f"Here is the number : $piSInglePrecision%.3f")
// precision at left
println(f"Here is the number : $numberone%05d")
//
println(s"I can use s prefix to use variable like $numberone $letterA ${1+2}")
// COMMAND ----------
// regex or regular expression
val string: String = "To life, the universe is 42."
val pattern = """.*([\d]+).*""".r
var pattern(answerString) = string
val answer = answerString.toInt
println(answer)
// COMMAND ----------
// boolean
val isGreater = 1>2
// COMMAND ----------
// flow control
if (1>3) println("True") else println("False")
// or
if (1>3){
println("True")
}
else{println("False")}
// COMMAND ----------
// Switch case or matching
val number = 2
number match{
case 1 => println("One")
case _ => println("Default")
}
// COMMAND ----------
// for loop
for(x <- 1 to 4){
val squared = x*x
println(squared)
}
// COMMAND ----------
// while loop
var x = 10
while (x>=0){
println(x)
x=x-1
}
// COMMAND ----------
// do while
x = 0
do {println(x); x+=1}while(x<10)
// COMMAND ----------
// Expression
{val x = 10; x+20}
//or
println({val x = 10; x+20})
// COMMAND ----------
// functions
def squareIt(x : Int) : Int = {
x*x
}
println(squareIt(2))
// Transform
def transformInt(x : Int, f:Int=> Int): Int = {
f(x)
}
val result = transformInt(2,squareIt)
// COMMAND ----------
// Lambda or anonymous function
transformInt(3,x => x*x*x)
// COMMAND ----------
// data structure
// tuples
// list
val tuple = ("Pickard","Enerprise")
println(tuple)
// refer to the individual field with one-based index in tuple
println(tuple._1)
val bunch = ("Priyansu",123)
// list
val list = List("Hello","world")
println(list)
// list has 0-based index
println(list(1))
// COMMAND ----------
println(list.head)
println(list.tail)
// list via loop
for (i <- list){println(i)}
val backwardShips = list.map( (ship: String) => {ship.reverse})
for (ship <- backwardShips) {println(ship)}
// COMMAND ----------
// reduce
val numberList = List(1,2,3,4,5)
val sum = numberList.reduce((x : Int, y: Int) => x+y)
// filter() removes stuff
val iHateFives = numberList.filter( (x: Int) => x != 5)
// without declareing name x! = 3 we just define _ which is placeholder
val iHateThrees = numberList.filter(_ != 3)
// COMMAND ----------
// Concatenate lists
val moreNumbers = List(6,7,8)
val lotsOfNumbers = numberList ++ moreNumbers
val reversed = numberList.reverse
val sorted = reversed.sorted
val lotsOfDuplicates = numberList ++ numberList
val distinctValues = lotsOfDuplicates.distinct
val maxValue = numberList.max
val total = numberList.sum
val hasThree = iHateThrees.contains(3)
// MAPS
val shipMap = Map("Kirk" -> "Enterprise", "Picard" -> "Enterprise-D", "Sisko" -> "Deep Space Nine", "Janeway" -> "Voyager")
println(shipMap("Janeway"))
println(shipMap.contains("Archer"))
val archersShip = util.Try(shipMap("Archer")) getOrElse "Unknown"
println(archersShip)