Skip to content

Instantly share code, notes, and snippets.

View tjheslin1's full-sized avatar

Thomas Heslin tjheslin1

  • London
View GitHub Profile
httpPort: 7000
cacheDuration: 60
probeConnectionWaitInSeconds: 10
databases:
- name: bobsDatabase
user: system
jdbcUrl: jdbc:oracle:thin:@localhost:1522:xe
probes:
- healthCheck
- noLabels
sealed trait Node {
def print: String =
this match {
case Obj(v) => v.toString
case JSONObj(v) => s"{\n${v.print}\n}"
case Element(k, v) => s"""\t"$k": "${v.print}",\n"""
case JSONEnd => ""
case JSONPair(hd, tl) => s"${hd.print} ${tl.print}"
case JSONSeq(hd, tl) => s"[ ${hd.print} ${tl.print} ]"
}

Taken from Essential Scala by Noel Welsh and Dave Gurnell

Invariant: Foo[T]: Foo[A] and Foo[B] are unrelated regardless of the relationship between A and B

Covariant: Foo[+T] -> Foo[A] is a supertype ofFoo[B] if A is a supertype of B.

Contravariant: Foo[-T] -> Foo[A] is a subtype of Foo[B] if A is a supertype of B.

Example:

// Taken from Essential Scala by Noel Welsh and Dave Gurnell
// fold: structural recursion
sealed trait LinkedList[A] {
def fold[B](end: B)(pair: (A, B) => B): B =
this match {
case End() => end
case Pair(hd, tl) => pair(hd, tl.fold(end, pair))
}
}
// Taken from Essential Scala by Noel Welsh and Dave Gurnell
for {
a <- getFirstNumber // getFirstNumber returns Option[Int]
b <- getSecondNumber // getSecondNumber returns Option[Int]
} yield a + b
// The final result is an Option[Int]---the result of
// applying `+` to `a` and `b` if both values are present

Taken from Essential Scala by Noel Welsh and Dave Gurnell

The type class pattern separates the implementation of functionality from the type the functionality is provided for.

In Scala, a type class is just a trait.

trait ExampleTypeClass[A] {
  def doSomething(in: A): Foo
}
passwords:
bobsDatabase: oracle
alicesDatabase: oracle

Taken from scala-with-cats by Noel Welsh and Dave Gurnell

A monoid for a type B is:

  • an operation combine with type (B, B) => B
  • an element empty of type B
trait Monoid[B] {
 def combine(x: B, y: B): B

Taken from scala-with-cats by Noel Welsh and Dave Gurnell

Definition of a Monad

  • pure, of type A => F[A]

pure abstracts over constructors, providing a way to create a new monadic context from a plain value.

  • flatMap, of type (F[A], A => F[B]) => F[B]
trait T
// defined trait T
case class A() extends T
// defined class A
case class B(a: A) extends T
// defined class B
def check[Y <: T](t: T): Boolean = t.isInstanceOf[Y]