Skip to content

Instantly share code, notes, and snippets.

View betehess's full-sized avatar

Alexandre Bertails betehess

View GitHub Profile
@viktorklang
viktorklang / CQRS_ES_Actor.scala
Created February 1, 2011 15:05
CQRS and EventSourcing using Akka Actors
import scala.collection.mutable.ListBuffer
import akka.actor.{Actor,ActorRef}
import akka.actor.Actor._
import akka.routing.{ Listeners, Listen }
//Represents a domain event
trait Event
//A builder to create domain entities
trait EntityBuilder[Entity] {
@milessabin
milessabin / conversions.scala
Created June 15, 2011 14:29
Code from my talk on representing polymorphic function values using type classes at the Scala eXchange ... full blog post to follow on http://www.chuusai.com/blog.
object Tuples {
import HLists._
implicit def tuple1ToHList[A](t : Product1[A]) = new { def hlisted : A :: HNil = t._1 :: HNil }
implicit def tuple2ToHList[A, B](t : Product2[A, B]) = new { def hlisted : A :: B :: HNil = t._1 :: t._2 :: HNil }
implicit def tuple3ToHList[A, B, C](t : Product3[A, B, C]) = new { def hlisted : A :: B :: C :: HNil = t._1 :: t._2 :: t._3 :: HNil }
implicit def hListToTuple1[A](h : A :: HNil) = new { def tupled : Tuple1[A] = Tuple1(h.head) }
implicit def hListToTuple2[A, B](h : A :: B :: HNil) = new { def tupled : (A, B) = (h.head, h.tail.head) }
implicit def hListToTuple3[A, B, C](h : A :: B :: C :: HNil) = new { def tupled : (A, B, C) = (h.head, h.tail.head, h.tail.tail.head) }
@viktorklang
viktorklang / ScalaEnum.scala
Created June 30, 2011 23:12
DIY Scala Enums (with optional exhaustiveness checking)
trait Enum { //DIY enum type
import java.util.concurrent.atomic.AtomicReference //Concurrency paranoia
type EnumVal <: Value //This is a type that needs to be found in the implementing class
private val _values = new AtomicReference(Vector[EnumVal]()) //Stores our enum values
//Adds an EnumVal to our storage, uses CCAS to make sure it's thread safe, returns the ordinal
private final def addEnumVal(newVal: EnumVal): Int = { import _values.{get, compareAndSet => CAS}
val oldVec = get
@guillaumebort
guillaumebort / Procfile
Created June 22, 2012 16:04
Configuration for a blocking Play 2.0 application
web: target/start -Dhttp.port=${PORT} -Dconfig.resource=prod.conf ${JAVA_OPTS}
@YoEight
YoEight / Alacarte.scala
Created June 28, 2012 11:09
Scala DataType à la carte
object Alacarte {
trait Functor[F[_]]{
def map[A, B](fa: F[A])(f: A => B): F[B]
}
trait Eval[F[_]] {
def F: Functor[F]
def evalAlgebra(fa: F[Int]): Int
}
@sadache
sadache / gist:3026886
Created July 1, 2012 05:01
Bits'bout Play2 Architecture

Bits'bout Play2 Architecture

Play2 Simple HTTP API

Essential blueprint of Play2 architecture is pretty simple and it should be easy to explain in a fairly short blog post. The framework can be understood progressively at different levels; each time having better exposure to some aspects of its design.

The core of Play2 is really small, surrounded by a fair amount of useful APIs, services and structure to make Web Programming tasks easier.

Basically, Play2 is an API that abstractly have the folllowing type

@viktorklang
viktorklang / linearize.scala
Created August 14, 2012 10:02
A Linearized version of Sequence
import scala.concurrent._
import scala.collection.mutable.Builder
import scala.collection.generic.CanBuildFrom
import language.higherKinds
/**
* Linearize asynchrnously applies a given function in-order to a sequence of values, producing a Future with the result of the function applications.
* Execution of subsequent entries will be aborted if an exception is thrown in the application of the function.
*/
def linearize[T, U, C[T] <: Traversable[T]](s: C[T])(f: T => U)(implicit cbf: CanBuildFrom[C[T], U, C[U]], e: ExecutionContext): Future[C[U]] = {
@jkodumal
jkodumal / RecIso.scala
Created September 22, 2012 16:57
Type-safe transformation from nested case classes to HLists using shapeless
// Based on shapeless 1.2.2
import shapeless._, HList._
trait RecIso[T, R] {
def iso(t: T) : R
}
trait RecId[T] extends RecIso[T, T] {
def iso(t: T) : T = t
}
@etorreborre
etorreborre / gist:3856308
Created October 9, 2012 02:55
Tupling big HLists
import shapeless._
// if there's only one element, return it
trait ToTuple2 extends Poly1 {
implicit def h2[T] = at[T :: HNil] { case t :: HNil => t }
}
// if there are at least 2 elements, tuple them
trait ToTuple1 extends ToTuple2 {
implicit def h1[T1, T2, L <: HList, T <: T1 :: T2 :: L](implicit tupler: Tupler[T1 :: T2 :: L]) = at[T1 :: T2 :: L] {
case l @ (t1 :: t2 :: rest) => l.tupled
@travisbrown
travisbrown / tuple-flatten.scala
Created October 24, 2012 11:18
Flattening deeply nested tuples with Shapeless
import shapeless._
trait Flatten[I, O <: HList] {
def apply(i: I): O
}
trait FlattenLow {
implicit def otherFlatten[I] = new Flatten[I, I :: HNil] {
def apply(i: I) = i :: HNil
}