forked from ankitsindhu/scala-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module 1.scala
231 lines (157 loc) · 3.42 KB
/
Module 1.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Databricks notebook source
// MAGIC %md
// MAGIC <h2>Introduction</h2>
// MAGIC
// MAGIC * Modern programming language, object oriented & functional languages
// MAGIC * Type-safe, extendable, shorter code
// MAGIC
// MAGIC <h3>Object Oriented</h3>
// MAGIC * Pure-objevt-oriented language - everything is an object
// MAGIC
// MAGIC <h3>Functional</h3>
// MAGIC * Every function is a value
// MAGIC * light-weight syntex for defining anyomous function
// MAGIC
// MAGIC <h3>Statically typed</h3>
// MAGIC * generic classes
// MAGIC * upper bounds & lower bounds
// MAGIC
// MAGIC <h3>SCala is extensible</h3>
// MAGIC * Scala provides a unique combination of language mechanisms that make it easy to add new language constructs to form libraries
// COMMAND ----------
//val - immutable
val x = 1 + 1
// COMMAND ----------
println(x)
// COMMAND ----------
println("Hello World")
// COMMAND ----------
//a is immutable
val a = 10
// COMMAND ----------
//var - mutable
//based on value assigned, it identifies it as an integer
var b = 88
// COMMAND ----------
b = 889
// COMMAND ----------
b
// COMMAND ----------
//code inside println
println({
val x = 1 + 1
x + 1 //This is returned
})
// COMMAND ----------
// MAGIC %md
// MAGIC <h3>Functions</h3>
// COMMAND ----------
// func takes int & returns one more than that
// function1 - function taking 1 arg
val f = (x :Int) => x + 1
// COMMAND ----------
f(10)
// COMMAND ----------
//Function defination of multiple lines
val f1 = (x:Int, y:Int) => {
println(x+y)
x+y
}
val x = 18
// COMMAND ----------
f1(2,4)
// COMMAND ----------
val g = (x : Int, y : Int) => x + y
// COMMAND ----------
g(6,7)
// COMMAND ----------
// MAGIC %md
// MAGIC <h3>Methods</h3>
// MAGIC * Methods are defined with def keyword
// COMMAND ----------
//Method takes double arg & returns String
def getSquareString(input: Double): String = {
val sq = input * input
sq.toString
}
// COMMAND ----------
getSquareString(55)
// COMMAND ----------
//Scala Classes
class MyClass {
var info : Int = 0;
//Constructors
def this(value : Int) = {
this();
this.info = value
}
//methods
def getMyInfo() : Int = {
return this.info
}
def addMyInfo(value : Int) {
this.info += value
}
}
// COMMAND ----------
val m = new MyClass(77)
val n = new MyClass(88)
// COMMAND ----------
m.info
// COMMAND ----------
m.getMyInfo()
n.addMyInfo(22)
// COMMAND ----------
// SCala Singleton & Companion Objects
// COMMAND ----------
//Defines a singleton object Main
object Main {
def sayHi() {
println("Hi!")
}
}
// COMMAND ----------
Main.sayHi()
// COMMAND ----------
//Companion Objects
class Main {
def sayHelloWorld() {
println("Hello World")
}
}
object Main {
def sayHi() {
println("Hiii")
}
}
// COMMAND ----------
var a : Main = new Main();
a.sayHelloWorld()
Main.sayHi()
// COMMAND ----------
// Arrays
var myArray : Array[String] = new Array[String](10)
// COMMAND ----------
myArray(0) = "Hello"
// COMMAND ----------
for( i <- 0 until myArray.length ){
println(myArray(i))
}
// COMMAND ----------
for(st <- myArray){
println(st)
}
// COMMAND ----------
//Conditional statements
var myInt : Int = 1;
if(myInt == 0){
println("myInt == 0")
} else {
println("myInt != 0")
}
// COMMAND ----------
var myText : String =
if(myInt == 0) "my-Int == 0"
else "my-Int != 0"
println(myText)
// COMMAND ----------