Skip to content

Instantly share code, notes, and snippets.

View arosien's full-sized avatar

Adam Rosien arosien

View GitHub Profile
// JsonFormat is from spray-json, reproduced here
trait JsonFormat[A] {
def write(a: A): JsValue
def read(js: JsValue): A
}
implicit class FormatInvariantFunctor[A](format: JsonFormat[A]) {
def xmap[B](f: A => B, g: B => A): JsonFormat[B] =
new JsonFormat[B] {
def write(b: B): JsValue = format.write(g(b))
@arosien
arosien / bloom.scala
Created April 2, 2015 19:24
WIP Bloom collections via Spire Lattices
package net.rosien.lattice
import scalaz._
import scalaz.syntax.order._
/* http://db.cs.berkeley.edu/papers/UCB-lattice-tr.pdf */
sealed trait LValue[A] {
def value: A
}

Real Ultimate Power for Services

For when just having a service that does something isn't enough.

Runtime Power

For answering the question: what the hell is going on?

Request Tracing

shared identifier across requests allows visibility of the entire execution graph

When services serve requests, they often require making further requests to other services in order to produce their response...

import org.scalacheck._
import org.scalacheck.Arbitrary._
import scalaz.scalacheck.ScalazProperties._
object FooSpec extends Properties("Foo") {
class Foo(value: String)
implicit val FooEqual: Equal[Foo] = Equal.equalRef
implicit val FooArb: Arbitrary[Foo] = Arbitrary(Gen.identifier map (new Foo(_)))
checkAll(equal.laws[Foo]) // passes
{
"basePath": "http://localhost:8080",
"position": 0,
"authorizations": [],
"models": {
"Pattern": {
"properties": [
{
"_2": {
"allowableValues": {},
@arosien
arosien / shapelessToJson.scala
Last active December 28, 2015 13:28
Conversion from shapeless records to json4s JValues.
trait JSONRecords {
import scala.annotation.implicitNotFound
import org.json4s._
import shapeless._
import shapeless.record._
import shapeless.ops.hlist._
import shapeless.ops.record._
import shapeless.syntax._
import shapeless.syntax.singleton._
@arosien
arosien / runar-io-free.scala
Last active September 10, 2016 07:17
Translation of Runar's ScalaIO 2013 presentation on IO and Free monads (http://blog.higher-order.com/assets/scalaio.pdf) to scalaz.
import scalaz._
import Scalaz._
import Free._
/** "Pure" interactions with a console. */
sealed trait Console[+A]
case class GetLine[A](k: String => A) extends Console[A]
case class PutLine[A](s: String, a: A) extends Console[A]
object Console {
def twice: Int => Option[Int] = i => Some(i * 2)
def stringit: Int => Option[String] = i => Some(i.toString)
val kleislish: Int => Option[String] =
Kleisli.ask[Option, Int] >=> Kleisli(twice) >=> Kleisli(stringit)
type KInt[M[+_], A] = Kleisli[M, Int, A]
val forcomprehensionish: Int => Option[String] =
for {
@arosien
arosien / Topology.scala
Last active December 24, 2015 00:59 — forked from helena/Topology.scala
import java.lang.System.{ currentTimeMillis ⇒ newTimestamp }
import scala.util.Try
import scala.collection.immutable
import scala.collection.JavaConverters._
import akka.actor._
import akka.japi.Util.immutableSeq
import com.typesafe.config._
import scalaz._
import Scalaz._
@arosien
arosien / shapes.scala
Last active December 14, 2015 03:49
shapes
(A => B) => A => B // function
(A => B) => F[A] => F[B] // Functor.fmap
Z[A => B] => Z[A] => Z[B] // Applicative.apply
(A => M[B]) => M[A] => M[B] // Monad.bind
(A => B) apply A: B // Function1.apply
F[A] fmap (A => B): F[B] // Functor.fmap
Z[A] <*> Z[A => B]: Z[B] // Applicative.apply
M[A] >>= (A => M[B]): M[B] // Monad.bind