Skip to content

Instantly share code, notes, and snippets.

View jdegoes's full-sized avatar

John A. De Goes jdegoes

View GitHub Profile
@jdegoes
jdegoes / MonadImpliesFunctor.scala
Created April 16, 2014 16:51
Scalaz test bed
import scala.language.higherKinds
trait Monad[M[_]] {
implicit def `Monad ~> Functor`[M[_]: Monad]: Functor[M] = new Functor[M]{}
}
trait Functor[F[_]] {
}
object Functor {
@jdegoes
jdegoes / Equiv.scala
Created April 22, 2014 02:27
Definition of Equiv.
/**
* Defines an equivalence between types A and B.
*
* Let ~_A be an equivalence relation on A, and ~_B be an equivalence relation
* on B.
*
* Let [a] denote the equivalence class of a in A ({a' | a ~_A a'}), and [b]
* denote the equivalence class of b in B ({b' | b ~_B b'}).
*
* Let `f: {[a] | a in A} -> {[b] | b in B]`, 'g: {[b] | b in B} -> {[a] | a in A}'
@jdegoes
jdegoes / generic.scala
Created April 27, 2014 16:17
Generic encoding of data in Scala
// ####### If you use a more generic lens you can transform types along the way.
case class Lens[S, A](get: S => A, set: (S, A) => S)
case class Prism[S, A](get: S => Option[A], unget: A => S) extends (A => S) {
def apply(a: A): S = unget(a)
def unapply(s: S): Option[A] = get(s)
}
// ####### Sum type
@jdegoes
jdegoes / errors-hk.txt
Created April 28, 2014 21:28
Lack of higher-kinded type inference???
[error] /Users/John/Documents/github/slamdata/slamengine/src/main/scala/slamdata/engine/physical/mongodb/planner.scala:468: type mismatch;
[error] found : slamdata.engine.analysis.fixplate.PhaseE[slamdata.engine.LogicalPlan2,slamdata.engine.PlannerError,Option[slamdata.engine.physical.mongodb.BsonField],Option[slamdata.engine.physical.mongodb.Selector]]
[error] (which expands to) slamdata.engine.analysis.fixplate.PhaseM[[X]scalaz.\/[slamdata.engine.PlannerError,X],slamdata.engine.LogicalPlan2,Option[slamdata.engine.physical.mongodb.BsonField],Option[slamdata.engine.physical.mongodb.Selector]]
[error] required: slamdata.engine.analysis.fixplate.PhaseM[EitherE,slamdata.engine.LogicalPlan2,Option[slamdata.engine.physical.mongodb.BsonField],?]
[error] val AllPhases = (FieldPhase[Unit]).fork(SelectorPhase, ExprPhase) >>> PipelinePhase
@jdegoes
jdegoes / scala-type-error.scala
Created May 7, 2014 22:13
Scala error message
[error] found : (slamdata.engine.analysis.fixplate.Term[[b]slamdata.engine.analysis.fixplate.Ann[slamdata.engine.LogicalPlan,A(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply),b]], slamdata.engine.analysis.fixplate.Term[[b]slamdata.engine.analysis.fixplate.Ann[slamdata.engine.LogicalPlan,A(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply),b]], slamdata.engine.LogicalPlan.JoinType, slamdata.engine.LogicalPlan.JoinRel, slamdata.engine.analysis.fixplate.Term[[b]slamdata.engine.analysis.fixplate.Ann[slamdata.engine.LogicalPlan,A(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply),b]], slamdata.engine.analysis.fixplate.Term[[b]slamdata.engine.analysis.fixplate.Ann[slamdata.engine.LogicalPlan,A(in method apply)(in method apply)(in method apply)(in method app
@jdegoes
jdegoes / scan.scala
Last active August 29, 2015 14:01
scalaz-stream scan
def scan[F[_], A, B](b: B, p: Process[F, A])(f: (B, A) => B): Process[F, B] = {
import Process._
def scan0(b: B, p: Process[F, A]) = scan(b, p)(f)
p match {
case h @ Halt(e) => h
case await : Await[F, Any, A] =>
Process.await[F, Any, B](await.req)(
@jdegoes
jdegoes / orelse.scala
Created June 4, 2014 20:01
Or else operator for Scalaz
implicit class AnyOps[A](value: A) {
def ?: [B](left: Option[B]): A \/ B = left.map(\/-(_)).getOrElse(-\/(value))
}
@jdegoes
jdegoes / cogroup.scala
Last active August 29, 2015 14:04
Simple, generic co-grouping with Scalaz
import scalaz._
import Scalaz._
object cogroup {
import \&/._
sealed trait Instr[A] {
def emit: List[A]
}
case class ConsumeLeft [A](emit: List[A]) extends Instr[A]
@jdegoes
jdegoes / book-outline.md
Created October 6, 2014 23:24
Enterprise Scala

Enterprise Scala

A guide to writing high-performance, robust, Enterprise-grade Scala applications.

Scala is a new language for the JVM that is rapidly spreading beyond tech companies like Twitter, Netflix, and LinkedIn, and into large Enterprises. Engineering teams in companies of all types and sizes are turning to Scala for its strong type system, its extensive ecosystem (Scalaz, Shapeless, Spire), and its seamless interoperability with legacy Java code bases.

While there are many books geared at introducing developers to the Scala programming language, none specifically address the question of what it means to write idiomatic Scala in today’s modern Enterprise. As a large, complex language with many advanced features, there are many ways to write Scala apps, and not all of them were created equal.

Enterprise Scala is the first book to systematize decades of real world, production experience writing large-scale, Enterprise-grade systems in Scala. The result is a comprehensive overview of what idioma

@jdegoes
jdegoes / die-flags-die.md
Created October 8, 2014 17:20
Death to Boolean Flags

Refactoring Booleans to Functions

Boolean parameters are a plague, responsible for non-composable, monolithic functions that never quite do enough, and countless programming bugs:

  • "Oops, meant to pass that as the 2nd boolean flag, not the 1st!"
  • "Oops, accidentally inverted the meaning of that boolean in the implementation!"
  • "Oops, the function isn't flexible enough for my use case, let's add a 6th boolean flag!"
  • "Oops, got the meaning of that boolean flag wrong, time to dig into the source code!"

All boolean parameters should be refactored into functions that effect the change otherwise encoded in the parameter.