Skip to content

Instantly share code, notes, and snippets.

View sofoklis's full-sized avatar

Sofoklis Papasofokli sofoklis

View GitHub Profile
@sofoklis
sofoklis / pforelse.scala
Last active December 11, 2015 20:48
partialfunctionsorelse
val positivePF: PartialFunction[Int, String] = { case i if i > 0 => "Positive"}
//> positivePF : PartialFunction[Int,String] = <function1>
val negativePF: PartialFunction[Int, String] = { case i if i < 0 => "Negative"}
//> negativePF : PartialFunction[Int,String] = <function1>
val zeroPF: PartialFunction[Int, String] = { case 0 => "Zero"}
//> zeroPF : PartialFunction[Int,String] = <function1>
// combine the indivitual partial functions to form one that covers all of them
@sofoklis
sofoklis / partialfunctions.scala
Last active December 11, 2015 20:48
partialfunctions.scala
// Full definition by defining the abstract methods
val pf1 = new PartialFunction[Int, Int]{
def apply(x: Int): Int = x match { case t if isDefinedAt(t) => t + 1 }
def isDefinedAt(x: Int): Boolean = x > 0 && x < 10
} //> pf1 : PartialFunction[Int,Int] = <function1>
pf1.isDefinedAt(20) //> res0: Boolean = false
//pf1(20) //scala.MatchError
//pf1.apply(20) // same as above using aply method
@sofoklis
sofoklis / liftedOptions.scala
Last active December 11, 2015 19:38
lifted options
// A lift function that takes a regular function that cannot handle options
// and uses for to call it only with Some(...) cases
def lift1[A, B](f: Function1[A, B]): Function1[Option[A], Option[B]] = {
(oa: Option[A]) =>
for (a <- oa) yield f(a)
} //> lift1: [A, B](f: A => B)Option[A] => Option[B]
// Same for a 2 argument function, if any of the arguments is None, the function simply returns None
def lift2[A, B, C, D](f: Function2[A, B, C]): Function2[Option[A], Option[B], Option[C]] = {
(oa: Option[A], ob: Option[B]) =>
@sofoklis
sofoklis / firstclassfunctions.scala
Created January 27, 2013 17:46
firstclassfunctions.scala
// Simplest way to create a function
def f1(value: Int) = 4 * value //> f1: (value: Int)Int
// And call
f1(4) //> res0: Int = 16
// and save it in a val
val valuef1 = f1 _ //> valuef1 : Int => Int = <function1>
//and call it from the val
@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
@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 / 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 / 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 / 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 / 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)