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 / RedEyesPreview.scala
Created February 4, 2014 17:20
A preview of the RedEyes web framework
/*
* RedEyes generates rich descriptions of the API, as well as valid examples for
* all parameters (in this case, example header, query parameter, and content).
*
* The API can also be used to generate client libraries in a variety of languages.
*
* Service handlers are strongly-typed (e.g. retrieveEmployees expects an integer
* and a ModelDesc). Any errors in using the API automatically generate rich
* descriptions on what was expected.
*/
@jdegoes
jdegoes / build.sbt
Created February 19, 2014 22:15
Kill language._
scalacOptions ++= Seq(
"-feature",
"-language:implicitConversions",
"-language:higherKinds",
"-language:existentials",
"-language:postfixOps"
)
@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))
@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 / 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 / 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 / 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 / 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 / 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 / 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 {