Skip to content

Instantly share code, notes, and snippets.

@IsaEs
Created January 8, 2019 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IsaEs/54ba1fddc2a71ea0f88e55445afa0f0f to your computer and use it in GitHub Desktop.
Save IsaEs/54ba1fddc2a71ea0f88e55445afa0f0f to your computer and use it in GitHub Desktop.
Scala Basics falan filan
package ies
import scala.collection.mutable.ArrayBuffer
object Main {
def main(args: Array[String]): Unit = {
println("Hello World");
//Byte
val oneByte: Byte = 121
//Boolean
//Char
//Short
//Int
//Long
//Float
//Double ,15digit percion
//BigInts("VeryVeryBigNumber")
//BigDecimal("")
//There is no ++ or --
var i = 0
while (i <= 10) {
println(i);
i += 1
}
do {
i += 1
println(i);
} while (i <= 20)
for (i <- 1 to 10)
println(i)
val randLetters = "ABADASDASD"
for (i <- 0 until randLetters.length)
println(randLetters(i))
val list = List(1, 2, 3, 4, true)
for (i <- list)
println(i)
var evenList = for { i <- 1 to 20 if (i % 2) == 0 } yield i
for (i <- evenList)
println(i)
for (i <- 1 to 5; j <- 6 to 10) {
println("i : " + i, "j: " + j)
}
var str = "HelloScalaImSveyda"
print(str(0))
//def funcName(param:dataType, param:dataType): returnType={
// funcbody
// return returnVal }
def getSum(num1: Int = 1, num2: Int = 1): Int = {
num1 + num2
}
print(getSum())
def helloMalatya(): Unit = {
println("HelloMalatyaNeVarNeYokOrda ")
}
def getSUM(args: Int*): Int = {
var sum: Int = 0
for (num <- args)
sum += num
sum
}
println("GetSum " + getSUM(1, 2, 3, 4, 5))
// Recursion
def fact(num: BigInt): BigInt = {
if (num <= 1) 1
else num * fact(num - 1)
}
println("Fact 5:" + fact(5))
def fib(num: BigInt): BigInt = {
if (num == 0) {
0
} else if (num == 1) {
1
} else
fib(num - 1) + fib(num - 2)
}
println(fib(8))
val favNums = new Array[Int](20)
val friends = Array("Bob", "Tom")
friends(0) = "Sue"
val friends2 = ArrayBuffer[String]()
friends2.insert(0, "Hello")
friends2 += "Cem"
friends2 ++= Array("asd", "asd")
friends2.insert(1, "Hello", "Listerine", "Loglog")
friends2.remove(1, 2)
//Map
val employees = Map("Manager" -> "BobSmith", "Secretary" -> "Sue Brown")
if (employees.contains("Manager"))
printf("Manager: %s\n", employees("Manager"))
// Mutable Map
val customers = collection.mutable.Map(100 -> "IsaEs", 101 -> "Sveyda")
customers(100) = "Brick"
for ((k, v) <- customers)
printf("%d: %s\n", k, v)
//Tuple
var tupleBiz = (100, "Angelica Morry", 1.41)
//
val rover = new Animal
rover.setName("Rover")
rover.setSound("Woof")
println(rover)
val whiskers = new Animal("Whiskers", "Wooof Woof")
println(whiskers)
val spike = new Dog("Spike", "Wooof", "grrrr")
println(spike)
val fang = new Wolf("Chang")
println(fang.move)
List(1, 2, 3, 4, 6, 5).filter(_ % 2 == 0).foreach(println)
}
class Animal(var name: String, var sound: String) {
// There not static variable like in java
this.setName(name)
this.setSound(sound)
val id = Animal.newIdNum
//protected access by class or inner class
def setName(name: String) {
this.name = name;
}
def setSound(sound: String) {
this.sound = sound
}
def this(name: String) {
this("No Name", "No Sound")
this.setName(name)
}
def this() {
this("No Name", "No Sound")
}
override def toString(): String = {
"%s says %s".format(name, sound)
}
}
object Animal {
private var idNumber = 0
private def newIdNum = { idNumber += 1; idNumber }
}
class Dog(name: String, sound: String, growl: String) extends Animal(name, sound) {
def this(name: String, sound: String) {
this("No Name", sound, "No Growl")
this.setName(name)
}
def this(name: String) {
this("No Name", "No Sound", "No Growl")
this.setName(name)
}
def this() {
this("No Name", "No Sound", "No Growl")
}
override def toString(): String = {
"%d %s says %s or %s".format(id, name, sound, growl)
}
}
abstract class Mammal(val name: String) {
var moveSpeed: Double
def move: String
}
class Wolf(name: String) extends Mammal(name) {
var moveSpeed = 12.3
def move = "The wolf %s runs %f".format(name, moveSpeed)
}
trait Flyable {
def fly: String
}
trait BulletProff {
def hitByBullet: String
def ricochet(startSpeed: Double): String = {
"The bullet riccohets at a speed of %.1f km/s".format(startSpeed * .75)
}
}
class SuperHero(val name: String) extends Flyable with BulletProff {
def fly = "%s fly thourogh the air".format(name)
def hitByBullet = "%s hitby bullet".format(name)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment