Skip to content

Instantly share code, notes, and snippets.

@danclien
danclien / DietCakePattern.scala
Created October 24, 2013 15:06
Sets up the Cake pattern with no "service interfaces" and only a single implementation of the dependency.
object DietCakePattern {
/***** Dependency *****/
trait EngineComponent {
val engine: Engine
class Engine {
def start: Unit = println("engine.start: vroom")
}
}
/***** Dependent implementation *****/
@danclien
danclien / DietCakePatternRequiredLines.scala
Created October 24, 2013 21:24
Bare bones to implement the Cake Pattern down the road.
/***** Dependency *****/
trait EngineComponent {
val engine: Engine
// class Engine {
// def start: Unit = println("engine.start: vroom")
// }
}
/***** Dependent implementation *****/
@danclien
danclien / option_monad.js
Last active December 30, 2015 02:09
Option/Maybe monad in JavaScript
function bind(/* (M a) */ monadicValue, /* (a -> M b) */ operation ) {
//Step 1: Unwrap `monadicValue` so type `(M a)` so it's of type `a`
//Step 2: Shoves the `a` through the `operation`
//How it shoves a through the operation depends on what you want it to do
//Step 3: Returns a monadic value of type `(M b)`
}
/* Maybe/Option monad
* If the value is None/Nothing, stop and return None/Nothing,
@danclien
danclien / option_monad_2.js
Created December 3, 2013 00:52
Option monad in JavaScript without using an Array.
function Some(value) {
this.value = value;
}
Some.prototype.bind = function(f) {
return f(this.value);
}
function None() {
}
function Some(value) {
this.value = value;
}
Some.prototype.flatMap = function(f) {
return f(this.value);
}
Some.prototype.map = function(f) {
return new Some(f(this.value));
@danclien
danclien / scala_start.md
Last active December 30, 2015 05:29
Personal notes on starting a new Scala project
  • Use GitHub's default .gitignore file for Scala
    • Add .DS_Store/ to .gitignore
  • Use ./project/Build.scala over ./Build.sbt
  • Add to ./project/plugins.scala with latest version numbers
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.10.1")

addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.1.2")
@danclien
danclien / spray_routing.scala
Last active December 30, 2015 13:09
Cleanest way I've found so far to divide Spray's routing into separate files.
/* Routing trait for mixing in routes */
trait RoutingService extends Directives { this: HttpServiceActor =>
private val EmptyRoute: Route = { context => Unit }
def routes: Route = EmptyRoute
/* Used to concatenate `Route`s together */
def concatRoutes(superRoutes: Route)(routes: Route): Route = superRoutes match {
case EmptyRoute => routes
case _ => superRoutes ~ routes
}
@danclien
danclien / hkt.scala
Last active January 3, 2016 04:18
An attempt to use higher kinded types to "extend" the List and Option classes.
// Enter :paste mode
trait Length[M[_]] {
def length[A](xs: M[A]): Int
}
object Length {
implicit val LengthList: Length[List] = new Length[List] {
def length[A](xs: List[A]): Int = {
xs.length

Usability Lab Notes

Notes from @shalinpei and @llewrek.

Notes

  • 2 rooms, one lab and one observation
  • Observation room
    • One-way mirror
    • Tables
List(1,2,3,4,5).foldRight(List.empty[Int])(_ :: _)
//res1: List[Int] = List(1, 2, 3, 4, 5)
/*
Passing in the type constructors for an abstract data type to its fold method
is the identity function.
List is made up of `Nil` and `Cons` objects.
*/