Skip to content

Instantly share code, notes, and snippets.

View sofoklis's full-sized avatar

Sofoklis Papasofokli sofoklis

View GitHub Profile
@sofoklis
sofoklis / covariantExample.scala
Created January 16, 2013 20:17
Covariant example
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
@sofoklis
sofoklis / contravariantExample.scala
Created January 16, 2013 20:55
contravarian example
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
@sofoklis
sofoklis / contravariantType.scala
Created January 16, 2013 20:56
contravariant definition
class ContravariantType[-T] {
// some functions and fields
}
@sofoklis
sofoklis / simple option.scala
Created January 17, 2013 14:19
simple option
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)
@sofoklis
sofoklis / flatmapoption.scala
Last active December 11, 2015 05:59
flatmap option
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)
@sofoklis
sofoklis / optiontolist.scala
Created January 18, 2013 12:05
option to list
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)
@sofoklis
sofoklis / flatmapmap.scala
Created January 20, 2013 11:34
flatmap map
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))
@sofoklis
sofoklis / stringinterpolation.scala
Created January 23, 2013 18:13
string interpolation
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
@sofoklis
sofoklis / customsi.scala
Last active January 10, 2018 10:01
Custom string interpolation
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 =
@sofoklis
sofoklis / function1.scala
Last active December 11, 2015 17:58
Function1 examples
// 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