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 / 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 / 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 / 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 / lambdaconf-sched2.txt
Last active August 29, 2015 13:59
Tentative LambdaConf schedule revision
SCORE: ????
Track 1
A Lattice of Tightropes: Tradeoffs in Language Design - 90
Monads: Why Should You Care? - 60
Intro to Functional Game Programming - 120
Building Web Services with RedEyes & Scala - 60
Building Web Applications with Clojure - 90
There's no Clusterf*ck without a Cluster - 60
@jdegoes
jdegoes / nchoosek.scala
Created April 13, 2014 23:34
N choose K in Scala
case class Choice[A](chosen: List[A], unchosen: List[A])
def nChoose1[A](n: List[A]): Stream[Choice[A]] = n match {
case Nil => Stream.empty[Choice[A]]
case head :: tail => Choice(head :: Nil, tail) #:: nChoose1(tail).map(choice => choice.copy(unchosen = head :: choice.unchosen))
}
def nChooseK[A](n: List[A], k: Int): Stream[Choice[A]] = {
if (k <= 0) Stream.empty[Choice[A]]
else if (k == 1) nChoose1(n)
@jdegoes
jdegoes / TAB.scala
Created April 10, 2014 22:51
Failed partial type application experiment
trait T[A, B]
object T {
def apply[A]: TA[A] = new TA[A] {}
trait TA[A] {
type Type[B] = T[A, B]
def apply[B]: TAB[B] = new TAB[B] {}
@jdegoes
jdegoes / lambdaconf-2014.md
Last active August 29, 2015 13:58
The LambdaConf 2014 Challenge

Introduction

LambdaConf has multiple tracks, so no attendee can possibly attend all sessions.

Yet, we'd like to schedule the sessions to maximize attendee happiness (for some definition of "maximum" and "happiness").

Sounds like a job for functional programming!

Your mission, should you choose to accept it, is to write a small functional program to produce an "optimal" schedule (your schedule may have any number of tracks you want -- you do not have to stick with 3 or 4).

@jdegoes
jdegoes / DefaultApplicativePlus.scala
Last active August 29, 2015 13:57
A default ApplicativePlus for Applicatives that contain ApplicativePlus's.
implicit def LiftedApplicativePlus[F[_], G[_]]
(implicit F: Applicative[F], G: ApplicativePlus[G]) = new ApplicativePlus[({type f[A]=F[G[A]]})#f] {
def empty[A] = F.point(G.empty)
def point[A](a: => A): F[G[A]] = F.point(G.point(a))
def plus[A](v1: F[G[A]], v2: => F[G[A]]): F[G[A]] = {
F.apply2(v1, v2)(G.plus(_, _))
}
@jdegoes
jdegoes / whatif.scala
Created March 17, 2014 22:07
StateT combinator
def whatif[S, A](f: StateT[M, S, A]): StateT[M, S, A] = {
for {
oldState <- read(identity[S])
rez <- f.imap(Function.const(oldState))
} yield rez
}
@jdegoes
jdegoes / basics.scala
Created February 26, 2014 23:24
Basic patterns
type Pattern[A, B] = A => Option[B]
def some[A, B](p: Pattern[A, B]): Pattern[Option[A], B] = _.flatMap(p)
def none[A, B]: Pattern[A, B] = Function.const(None)
def k[A](v0: A): Pattern[A, A] = v => if (v == v0) Some(v0) else None
def or[A, B](p1: Pattern[A, B], p2: Pattern[A, B]): Pattern[A, B] =
v => p1(v).orElse(p2(v))