Skip to content

Instantly share code, notes, and snippets.

@glava
Last active February 23, 2016 17:43
Show Gist options
  • Save glava/82dc068829ce6e7cc5ea to your computer and use it in GitHub Desktop.
Save glava/82dc068829ce6e7cc5ea to your computer and use it in GitHub Desktop.
// Fire up that repl!
// You can't mix apple and oranges
var orange: String = "nice sweet orange"
var apple: Int = 10
orange = apple // error: type mismatch
// You should keep info about the type all the time
var oranges: List[String] = List("sweet 1", "sweet 2")
var apple: Int = 10
apple :: oranges // otherwise you and up with Any which is useless in most of the cases
// Defining value
val jasonBourne: String = "Matt Damon"
// Defining variable
var jamesBond: String = "Sean Connery"
// Collections are immutable by default
val list: List[Int] = List(4, 3, 2, 1)
// even if we append
5 :: list
// or do some transfomation
list.map(_ + 1)
// defining a function
val add: (Int, Int) => Int = (x: Int, b: Int) => { x + b }
// defining a method
def add(x: Int, b: Int): Int = { x + b }
// Passing function as an argument
def math(a: Int, b: Int, operation: (Int, Int) => Int): Int = operation(a, b)
def plus(a: Int, b: Int) = a + b
def minus(a: Int, b: Int) = a - b
math(10, 2, plus)
math(10, 2, minus)
// Then we can do stuff like this
List(1, 2, 3, 4).map(x => x * 2)
List(1, 2, 3, 4).reduce((x, y) => x + y)
List(1, 2, 3, 4).reduce(_ + _)
// Classes
class Batman(actor: String) { def showActor = actor }
val protector = new Batman("Christian Bale")
// try to access showActor, actor field and try to assign something to actor
// or with creating property through class constructor
class Batman(val actor: String)
val protector = new Batman("Christian Bale")
// try to access showActor, actor field and try to assign something to actor
// or with creating mutable property through class constructor
class Batman(var actor: String)
val protector = new Batman("Christian Bale")
// try to access showActor, actor field and try to assign something to actor
class Batcar(batman: Batman) {
class Robin(actor: String)
val sidekick: Robin = new Robin("Chris O'Donnell")
private var shield: Boolean = true
def shieldOn = shield = true
def shieldOff = shield = false
def shieldStatus = shield
}
val batCar = new Batcar(protector)
// Case Classes
case class Destination(name: String)
case class UBahn(name: String, start: Destination, end: Destination)
val u8 = UBahn("u8", Destination("Hermannstrasse"), Destination("Osloer"))
u8.copy(end = Destination("Janowitzbrücke"))
// Pattern matching
case class Jedi(name: String, side: String)
def isCool(jedi: Jedi) =
jedi match {
case Jedi(_, "dark") => true
case _ => false
}
isCool(Jedi("Kylo Ren", "dark"))
def printFirst(list: List[Int]): Unit = list match {
case List() => println("List is empty")
case head::tail => println(head)
}
def parseArgument(arg: String) = arg match {
case "-h" | "--help" => println("Help is on the way")
case "-v" | "--version" => println("Your version makes me happy")
case _ => println("Have no clue what you want from me")
}
parseArgument("--help")
// Working with option
val politeColleague: Option[String] = Some("Good morning")
val rudeColleague: Option[String] = None
List(politeColleague, rudeColleague).filter(colleague => colleague.isDefined)
def replay(colleagueSays: Option[String]) = colleagueSays match {
case Some(hello) => s"$hello to you too"
case None => "GOOD MORNING!!!"
}
def replay2(colleagueSays: Option[String]) =
colleagueSays.map(hello => s"$hello to you too").getOrElse("GOOD MORNING!!!")
val list: List[Int] = List(1, 2, 3, 4) // or List()
list.headOption // can return Some(1) or None
list.headOption.filter(_ % 2 == 0) // will definitely return None
// or
val map = Map(1 -> "one")
map.get(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment