This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val covariantType1: CovariantType[String] = new CovariantType[String] // Allowed | |
val covariantType2: CovariantType[Any] = new CovariantType[String] // Allowed | |
val covariantType3: CovariantType[String] = new CovariantType[Any] // Compile error |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val contravariantType1: ContravariantType[String] = new ContravariantType[String] // Allowed | |
val contravariantType2: ContravariantType[Any] = new ContravariantType[String] // Compile error | |
val contravariantType3: ContravariantType[String] = new ContravariantType[Any] // Allowed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ContravariantType[-T] { | |
// some functions and fields | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val n = None //> n : None.type = None | |
val s = Some(1) //> s : Some[Int] = Some(1) | |
val o = Option(3) //> o : Option[Int] = Some(3) | |
o match { | |
case Some(x) => println("Count to :" + x) | |
case None => println("Dont count") | |
} //> Count to :3 | |
val s2 = Some(null) //> s2 : Some[Null] = Some(null) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val o4 = Option(4) //> o4 : Option[Int] = Some(4) | |
val o5: Option[Int] = None //> o5 : Option[Int] = None | |
val m1 = o4 map ("Num " + _ * 2) //> m1 : Option[String] = Some(Num 8) | |
val m2 = o5 map ("Num " + _ * 2) //> m2 : Option[String] = None | |
val f1 = for(i <- o4) yield "Num " + i * 3//> f1 : Option[String] = Some(Num 12) | |
val f2 = for(i <- o5) yield "Num " + i * 3//> f2 : Option[String] = None | |
val fm1 = o4 flatMap(i => Option("Num " + i * 4)) //> fm1 : Option[String] = Some(Num 16) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val ln = n.toList //> ln : List[Nothing] = List() | |
val ls = s.toList //> ls : List[Int] = List(1) | |
val lo = o.toList //> lo : List[Int] = List(3) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val n : Option[Int] = None //> n : Option[Int] = None | |
val ol = List(Option(1), n , Option(3), Option(5), n) //> ol : List[Option[Int]] = List(Some(1), None, Some(3), Some(5), None) | |
ol map ( i => i map (j => j * 2)) //> res3: List[Option[Int]] = List(Some(2), None, Some(6), Some(10), None) | |
// FlatMap with map and same using For comprehension | |
ol flatMap ( i => i map (j => Option(j * 2))) //> res5: List[Option[Int]] = List(Some(2), Some(6), Some(10)) | |
for(o <- ol; i <- o) yield Option(i*2) //> res6: List[Option[Int]] = List(Some(2), Some(6), Some(10)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val st = "String Interpolation" //> st : String = String Interpolation | |
// s | |
val s1 = "Lets try $st" //> s1 : String = Lets try $st | |
val s2 = s"Lets try $st" //> s2 : String = Lets try String Interpolation | |
val s3 = s"Also arbitrary expressions ${List(1,2,3,4,6) sum}" //> s3 : String = Also arbitrary expressions 16 | |
// f | |
val height = 1.9d //> height : Double = 1.9 | |
val name = "James" //> name : String = James |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Test { | |
// Implicit class is also a new feature in scala 2.10 | |
implicit class CounterSC(val sc: StringContext) extends AnyVal { | |
// Define functions that we want to use with string interpolation syntax | |
def partsCount(args: Any*): Int = sc.parts.length | |
def argsCount(args: Any*): Int = args.length | |
def tokenCount(args: Any*): Int = sc.parts.length + args.length | |
def getParts(args: Any*): String = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Simple way to create a Function1 | |
val succ = (x: Int) => x + 1 //> succ : Int => Int = <function1> | |
// Same in full version | |
val anonfun1 = new Function1[Int, Int] { | |
// Only need to specify the apply method | |
def apply(x: Int): Int = x + 1 | |
} //> anonfun1 : Int => Int = <function1> | |
// Create one more function to compose |