Skip to content

Instantly share code, notes, and snippets.

/*
* Copyright 2014–2016 SlamData Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@wemrysi
wemrysi / auditing.scala
Last active December 14, 2015 17:48
Auditing Ideas
type Timestamp = Long
type Nanos = Long
sealed trait Clock[A]
final case class CurrentMillis extends Clock[Timestamp]
final case class CurrentNanos extends Clock[Nanos]
final case class EntryId(val asString: String) extends AnyVal
sealed trait Audit[A]
def bracket[F[_], R, A](acquire: NF[F, R])(use: R => NF[F, Option[A]], release: R => NF[F, Unit]): NF[F, A] = {
def go(cln: NF[F, A], p: NF[F, Option[A]]): NF[F, A] =
await(p) flatMap { step =>
step.head.cata(a => emit(a) ++ go(cln, step.tail), cln ++ go(empty, step.tail))
}
flatMap(acquire) { r =>
val cleanup = drain[F, Unit, A](mask(release(r)))
onError(go(cleanup, use(r))) { e => append(cleanup, fail(e)) }
}
@wemrysi
wemrysi / gist:62b396796868b579bdce
Created June 26, 2014 21:24
MonadLogTask + TreeLogger
import scalaz._, concurrent._
trait MonadLogTask[F[_], L] extends Monad[F] with Catchable[F] {
def log[A](label: => L, task: Task[A]): F[A]
def heading[A](label: => L, fa: F[A]): F[A]
def liftTask[A](task: Task[A]): F[A]
def runFree[S[+_], M[_]](f: S ~> M)(implicit M: Monad[M]): ({type λ[α] = Free[S, α]})#λ ~> M =
new (({type λ[α] = Free[S, α]})#λ ~> M) {
def apply[A](fr: Free[S, A]) = fr.foldMap[M, A](f)
}
def runFreeC[S[+_], M[_]](f: S ~> M)(implicit M: Monad[M]): ({type λ[α] = FreeC[S, α]})#λ ~> M =
new (({type λ[α] = FreeC[S, α]})#λ ~> M) {
def apply[A](fr: FreeC[S, A]) = fr.runM[M, A](coyo => M.map(f(coyo.fi))(coyo.k))
}
object RefactorPuzzle {
case class IntRdr[+A](read: Int => A) {
def map[B](f: A => B): IntRdr[B] =
IntRdr(f compose read)
def flatMap[B](f: A => IntRdr[B]): IntRdr[B] =
IntRdr(n => f(read(n)).read(n))
}
object IntRdr {
@wemrysi
wemrysi / gist:4567725
Created January 18, 2013 19:40
JUnitInterface for spray-testkit
package spray.testkit
import spray.util._
import akka.actor.ActorSystem
trait JUnitInterface extends TestFrameworkInterface {
def failTest(msg: String) = throw new AssertionError(msg)
}
trait JUnitRouteTest extends RouteTest with JUnitInterface {
// Assume the esper clock has been seeded with the vimana system time on start
// and is being updated via the DeviceActor every 100ms when no agent is connected
// Someplace earlier in the class
var lastEvent: Option[Event] = None
for (event <- events) {
lastEvent filter (event.timestamp > _.timestamp) foreach {
esper.advanceClock(event.timestamp - _.timestamp)
}