Skip to content

Instantly share code, notes, and snippets.

@arsenlosenko
Created November 14, 2017 09:46
Show Gist options
  • Save arsenlosenko/cefafae0bb832a0c3b755b7e2b372582 to your computer and use it in GitHub Desktop.
Save arsenlosenko/cefafae0bb832a0c3b755b7e2b372582 to your computer and use it in GitHub Desktop.
/* Created with info from here:
* https://learnxinyminutes.com/docs/scala/
*/
object SandBox {
def main(args: Array[String]): Unit = {
val str: String = "hello world C"
println("------------------------------------------")
println(">string methods")
println(str.length)
println(str.substring(2, 6))
println(str.replace("C", "E"))
println(str.take(5))
println(str.drop(5))
println(">val immutable, var isn't")
val n = 45
println(">print vars via $var notation")
println(s"We have $n apples")
val a = Array(11, 9, 6)
println(s"My second daughter is ${a(0) - a(2)} years old")
println(s"We have double the amount of ${n / 2.0} in apples")
println(">usage of functions")
var sum: Int = sumOfSquares(2, 3)
println(sum)
def subtract(x: Int, y:Int): Int = x - y
println(subtract(5, 2))
def addWithDefault(x: Int, y: Int = 5) = x + y
println(addWithDefault(5))
println(addWithDefault(1, 11))
println(">anonymous function definition")
val sq: Int => Int = x => x * x
println(sq(10))
val addOne: Int => Int = _ + 1
println(addOne(2))
val weirdSum: (Int, Int) => Int = (_ * 2 + _ * 3)
println(weirdSum(2, 3))
println(">loops and shit")
val rng = 1 to 5
rng.foreach(println)
(5 to 1 by -2) foreach (println)
var i = 0
while(i < 10) {println("i " + i); i+=1}
i = 0
do {
println("i is still less than 10")
i += 1
}while(i < 10)
print(">recursion\n")
showNumbersInRange(1, 14)
val x = 10
print(">conditionals\n")
if (x == 1) println("yeah")
if (x == 10) println("yeah")
if (x == 11) println("yeah") else println("nay")
println(if (x==10) "yeah" else "nay")
val text = if (x == 10) "yeah" else "nope"
println(">data structures")
val arr = Array(1, 2, 3, 4, 5)
val map = Map("fork" -> "tenedor", "spoon" -> "cuchara", "knife" -> "cuchillo")
println(map("fork"))
val set = Set(1, 3, 7)
println(set(1))
val divideInts = (x: Int, y: Int) => (x / y, x % y)
val d = divideInts(10, 3)
println(d._1)
println(d._2)
val (div, mod) = divideInts(10, 3)
println(div, mod)
println(">classes")
val bobik = new Dog("greyhound")
println(bobik.breed)
println(bobik.bark)
val sharik = Dog
println(sharik.allKnownBreeds)
println("> case classes")
val george = Person("George", "1234")
val kate = Person("Kate", "4567")
println(george.phoneNumber)
val zhora = george.copy(phoneNumber = "9876")
println(zhora.phoneNumber)
println(">traits")
// TODO: pattern matching and traits
println("> functional programming")
val add10: Int => Int = _ + 10
println(List(1, 2, 3) map add10)
println(List(1, 2, 3) map (x => x * 2))
List("Dom", "Bob", "Natalia") foreach println
var s = List(4, 5 ,6)
val sSquared = s map (x => x * x)
println(sSquared)
println(sSquared.filter(_ < 20)(0))
println(sSquared.reduce(_ + _))
println(List(1, 2, 3) filter (_ > 2))
// TODO: for comprehensions
println("------------------------------------------")
}
// definition of function
def sumOfSquares(x: Int, y: Int): Int = {
val x2 = x * x
val y2 = y * y
x2 + y2
}
def showNumbersInRange(a: Int, b: Int): Unit = {
print(a +"\n")
if (a < b)
showNumbersInRange(a + 1, b)
}
}
class Dog(br: String){
var breed: String = br
def bark = "Woof, woof!"
private def sleep(hours: Int) =
println(s"I'm sleeping for $hours hours")
}
object Dog{
def allKnownBreeds = List("pitbull", "shepherd", "retriever")
def createDog(breed: String) = new Dog(breed)
}
case class Person(name: String, phoneNumber: String)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment