Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am eggm0n on github.
  • I am eggm0n (https://keybase.io/eggm0n) on keybase.
  • I have a public key ASBGbG6y1gyou649rBsxEYfgV-4tyWRU7fg789QJRnKfLgo

To claim this, I am signing this object:

@eggm0n
eggm0n / validate.scala
Last active September 25, 2017 12:25
extra validation on play json reads
import play.api.libs.json._
import scala.reflect.runtime.universe._
def checkedReads[T](underlyingReads: Reads[T])(implicit typeTag: TypeTag[T]): Reads[T] = new Reads[T] {
def classFields[U: TypeTag]: Set[String] = typeOf[U].members.collect {
case m: MethodSymbol if m.isCaseAccessor => m.name.decodedName.toString
}.toSet
def reads(json: JsValue): JsResult[T] = {
@eggm0n
eggm0n / notes
Created November 30, 2017 09:21
git notes
> git init
Directory:
A folder used for storing multiple files.
Repository:
A directory where Git has been initialized to start version controlling your files.
> git status
The .git directory
@eggm0n
eggm0n / example.scala
Created November 30, 2017 09:39
covariance and contravariance
class GParent
class Parent extends GParent
class Child extends Parent
class Box[+A]
class Box2[-A]
def foo(x : Box[Parent]) : Box[Parent] = identity(x)
def bar(x : Box2[Parent]) : Box2[Parent] = identity(x)
foo(new Box[Child]) // success
foo(new Box[GParent]) // type error
bar(new Box2[Child]) // type error
@eggm0n
eggm0n / notes.md
Created May 11, 2018 14:26
Higher kinded types notes

F is a higher kinded type/function at the type level/type level constructor. Examples are Future, Option, List, etc.

List is a type constructor and Int is a proper type. If I apply Int to List then I get List[Int]which is a proper type. So far, all we have done is used a generic higher kinded type (F) instead of a concrete one (Future).

Typeclasses are a form of ad-hoc polymorphism. They allow you to an enrich an existing type with new capabilities. For example, let’s say I wanted to add two Orders together. The source code for an Order isn’t in our control but we would like to extend it.

def add[A](x: A, y: A)(implicit a: Addable[A]): A = a.add(x, y)

This is an example of a typeclass method. Notice that in order to use the addmethod, your A’s need to have a typeclass implementation of Addable[A].