Skip to content

Instantly share code, notes, and snippets.

View EECOLOR's full-sized avatar

EECOLOR EECOLOR

  • Kaliber
  • Utrecht, Netherlands
View GitHub Profile
@EECOLOR
EECOLOR / _1_TypedActor.scala
Last active August 29, 2015 14:06
An alternative version of typed actors. Actors can only receive messages of a certain type, the type parameter determines the return type.
package experiment
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.language.higherKinds
import scala.language.implicitConversions
import scala.reflect.ClassTag
import akka.actor.Actor
import akka.actor.ActorRef
@EECOLOR
EECOLOR / project_FakeHttpServletRequest.scala
Last active August 29, 2015 14:03
Experiment running railo from sbt
import javax.servlet.http.HttpServletRequest
class FakeHttpServletRequest extends HttpServletRequest {
// Members declared in javax.servlet.http.HttpServletRequest
def authenticate(x$1: javax.servlet.http.HttpServletResponse): Boolean = ???
def getAuthType(): String = ???
def getContextPath(): String = "/"
def getCookies(): Array[javax.servlet.http.Cookie] = Array.empty
def getDateHeader(x$1: String): Long = ???
def getHeader(x$1: String): String = ???
@EECOLOR
EECOLOR / 0_Program.scala
Last active April 17, 2017 11:13
Wrapper for Free monads that (at least I thought) makes them more usable. Different languages can be composed at will (without manual lifting). Runners should have all the used types in the composed program, but they do not have to be in the same order.
package test
import scala.language.higherKinds
case class Program[F[_], A](free: Free[F, A]) {
import Coproduct.|
def flatMap[G[_], B](f: A => Program[G, B])(
implicit c: F | G): Program[c.Out, B] =
@EECOLOR
EECOLOR / TypeList.scala
Last active August 29, 2015 14:03
Collecting types in a type list (an HList without actual values). Adding and Finding types in that list on a a type level
package test2
import scala.language.higherKinds
trait T1[A]
trait T2[A]
trait T3[A]
trait TypeList
trait Nil extends TypeList
@EECOLOR
EECOLOR / freemonads.scala
Last active August 29, 2015 14:02 — forked from runarorama/gist:a8fab38e473fafa0921d
Free monads
sealed trait Interact[A]
case class Ask(prompt: String)
extends Interact[String]
case class Tell(msg: String)
extends Interact[Unit]
trait Monad[M[_]] {
def pure[A](a: A): M[A]
@EECOLOR
EECOLOR / hList-validation
Last active August 29, 2015 13:56
HList as a basis for validation and conversion. Using fold on nested HList. Applying method on elements in HList tree
import shapeless._
import shapeless.ops.hlist._
import scala.util.Try
object validation {
trait Violation {
def message: String
}
abstract class SimpleViolation(val message: String) extends Violation
@EECOLOR
EECOLOR / 1. spray-embedded-server
Last active January 1, 2016 09:49
A wrapper for Spray's Http server that performs graceful shutdown. The server can be shutdown from the outside by calling the `stop()` method of from the service by sending a message: `context.parent ! Server.Stop`. These conversations helped me find a solution: [How to stop SimpleRoutingApp without getting an error](https://groups.google.com/d/…
package org.qirx.browserTests
import scala.concurrent.Future
import scala.concurrent.Promise
import scala.concurrent.duration._
import scala.util.Try
import akka.actor.Actor
import akka.actor.ActorRef
import akka.actor.ActorSystem
@EECOLOR
EECOLOR / multipart form data in Play WS
Created November 25, 2013 21:23
Multipart form data with Play Framework 2 WS client
//The following example allows you to post `multipart/form-data`. It is a simple version that only
//works with `String` values, but it could easily be modified to use other types of data.
type NameValuePair = (String, String)
case class MultipartFormData(elements: Seq[NameValuePair], boundary: String)(
implicit codec: Codec) {
private val HTTP_SEPARATOR = "\r\n"
private val actualBoundary = "--" + boundary
private val endBoundary = actualBoundary + "--" + HTTP_SEPARATOR
@EECOLOR
EECOLOR / gist:7171474
Created October 26, 2013 16:26
Prints Json litterals for given JsValue
def print(json: JsValue): String = {
json match {
case JsObject(values) =>
val contents =
values.map {
case (k, v) => s""""$k" -> ${print(v)}"""
}.mkString(", ")
s"Json.obj($contents)"
case JsArray(values) =>
import play.api.libs.concurrent.Akka
import akka.actor.{ Props, Actor }
import concurrent.ExecutionContext
import scala.concurrent.duration._
import ExecutionContext.Implicits.global
import play.api.test._
object AkkaScheduler extends WithApplication with App {
case object Tick