Created
October 29, 2019 15:57
-
-
Save invkrh/97fa6272b898aa04ca08011b1d29946d to your computer and use it in GitHub Desktop.
Note of Scala training session at Criteo
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
trait Name { | |
def it: String | |
} | |
/** no need for explicit import */ | |
object Name { | |
implicit val criteo = new Name { val it = "asdf" } | |
} | |
object Live extends App { | |
/** | |
* implicit conversion only happens at compile time | |
* implicit class to avoid implicit conversion | |
* value class to avoid allocating object, instead, it creates static functions | |
* types share the same property in one typeclass which is neither a type or a class =) | |
* semi-group (zero value of type T) ??? monoid (add operation) ??? | |
*/ | |
trait Monoid[A] { | |
val zero: A | |
def combine(a: A, b: A): A | |
} | |
object Monoid { | |
implicit val intMonoid = new Monoid[Int] { | |
override val zero: Int = 0 | |
override def combine(a: Int, b: Int): Int = a + b | |
} | |
implicit def optionMonoid[A]( | |
implicit monoid: Monoid[A]): Monoid[Option[A]] = new Monoid[Option[A]] { | |
override val zero: Option[A] = None | |
override def combine(a: Option[A], b: Option[A]): Option[A] = | |
(a, b) match { | |
case (Some(p), None) => Some(p) | |
case (None, Some(q)) => Some(q) | |
case (Some(p), Some(q)) => Some(monoid.combine(p, q)) | |
case (None, None) => None | |
} | |
} | |
} | |
// def combineAll[A](items: List[A])(implicit monoid: Monoid[A]): A = | |
// items.foldLeft(monoid.zero)(monoid.combine) | |
def myImplicitly[A](implicit a: A): A = a | |
def combineAll[A: Monoid](items: List[A]): A = { | |
// val monoid = implicitly[Monoid[A]] | |
val monoid = myImplicitly[Monoid[A]] | |
items.foldLeft(monoid.zero)(monoid.combine) | |
} | |
println { | |
combineAll(List(1, 2, 4, 5)) | |
} | |
println { | |
combineAll(List(Option(1), Option(2), None)) | |
} | |
// def print[A](thing: A): Unit = thing match { | |
// case s: String => println(s) | |
// case i: Int => println(i) | |
// } | |
sealed trait StringOrInt[A] | |
implicit case object IntIsAnInt extends StringOrInt[Int] | |
implicit case object StringIsAnString extends StringOrInt[String] | |
def print[A](thing: A)(implicit evidence: StringOrInt[A]): Unit = | |
thing match { | |
case s: String => println(s) | |
case i: Int => println(i) | |
} | |
print("1") | |
print(1) | |
// print(true) | |
// import cats.Semigroup | |
// implicit val intSemigroup = new Semigroup[Int] { | |
// def combine(a: Int, b: Int) = a + b | |
// } | |
import cats.syntax.semigroup._ | |
import cats.instances.all._ | |
val x = 1 | |
val y = 2 | |
val z = 3 | |
x |+| y |+| z | |
val map1 = Map("a" -> 1, "b" -> 2) | |
val map2 = Map("a" -> 3, "b" -> 4) | |
println(map1 |+| map2) | |
import cats.Monad /** include Monad for Either */ | |
val l = List(1, 2, 3, 4) | |
println { | |
Monad[List].flatMap(l)(x => List(x * 2)) | |
} | |
println { | |
Monad[List].ifM(List(true, false, true))(ifTrue = List(1, 2), | |
ifFalse = List(3, 4)) | |
} | |
val toto = "lol" | |
val tata = "lal" | |
val titi = 12 | |
println { | |
import cats.implicits._ | |
toto =!= tata | |
} | |
println { | |
import cats.kernel.Eq | |
Eq[String].eqv(toto, tata) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment