Skip to content

Instantly share code, notes, and snippets.

// first, we need to create a way to describe a Path in a generic way
import shapeless._, labelled._, record._, syntax.singleton._
import com.twitter.finagle.httpx.path
// this describes the to and from, giving the compiler a way to side step type erasure
// i'm using path.Path to disambiguate a "Path" which is included in the shapeless imports
// in fact, if we supply this in the package object of the module we are using we will have implicit access to it :)
implicit object pathGeneric extends LabelledGeneric[path.Path] {
type Repr = Record.`'str -> String`.T // create a witness/record for this symbol 'str
def from(r: Repr): path.Path = path.Path(r('str)) // define a way to create a path from this Record
@trane
trane / oneWayToDoDefaults.scala
Last active September 17, 2015 16:56
this is one way to handle partial case classes
import io.circe._, io.circe.generic.auto._, io.circe.jawn._, io.circe.syntax._
// define a class with default param value
case class Foo(s: String, i: Int = 10)
val json = """{"s": "Hello"}"""
val json2 = """{"s": "World", "i": 42}"""
// define some boilerplate decoders, utilizing the two constructors via decoders
val dec1 = Decoder.instance[Foo](c => for (s <- c.downField("s").as[String]; i <- c.downField("i").as[Int]) yield Foo(s, i))
case class Foo(s: String, i: Int = 10)
val json = """{"s": "Hello"}"""
// this obviously works
decode[Int => Foo](json).map(_(10))
// but what about cases where you have many default parameters of the same type?
case class Bar(i1: Int, i2: Int = 2, i3: Int = 3, i4: Int = 4)
val json2 = """{"i1": 1}"""
val a = Some(1)
val b = Some(2)
val c = Some(3)
for {
x <- a
y <- b
z <- c
} yield x + y + z // Some(6)
package com.lookout.borderpatrol.example
import java.net.InetSocketAddress
import java.nio.ByteBuffer
import com.twitter.finagle.stats.{Counter, Gauge, Stat, StatsReceiver}
import java.nio.channels.DatagramChannel
import com.twitter.io.Buf
@trane
trane / ringAroundTheMonad.scala
Last active August 29, 2015 14:27
playing with Coyoneda+Free Monad for algebraic Rings.. and funsies
import algebra._, std.int._
import cats._, free._, Free._
trait Expr[A]
case class Const[A](term: A) extends Expr[A]
case class Add[A](e1: Expr[A], e2: Expr[A])(implicit val r: Ring[A]) extends Expr[A]
case class Sub[A](e1: Expr[A], e2: Expr[A])(implicit val r: Ring[A]) extends Expr[A]
case class Mul[A](e1: Expr[A], e2: Expr[A])(implicit val r: Ring[A]) extends Expr[A]
type Exp[A] = FreeC[Expr, A]
package com.twitter.zipkin.collector
import com.twitter.conversions.time._
import com.twitter.finagle.stats.InMemoryStatsReceiver
import com.twitter.util.{Await, Future, FuturePool}
import java.util.concurrent.CountDownLatch
import org.scalatest._
/**
* Tests the BlockingItemQueue to make sure that it can store and consume elements even when adding
import argonaut._
import Argonaut._
case class Session0(request: Int, data: Int) extends Session[Int, Int]
case class Session1(request: Int, data: String) extends Session[Int, String]
case class Session2(request: Int, data: Session0) extends Session[Int, Session0]
implicit def Session0CodecJson: CodecJson[Session0] =
casecodec2(Session0.apply, Session0.unapply)("request", "data")
implicit def Session1CodecJson: CodecJson[Session1] =
def merge[A](t: (List[A], List[A]))(implicit p: (A, A) => Boolean): List[A] = {
t match {
case (as, Nil) => as
case (Nil, bs) => bs
case (a :: as, b :: bs) => if (p(a, b)) a :: merge((as, t._2))
else b :: merge((t._1, bs))
}
}
def split[A](s: List[A]): (List[A], List[A]) =
# case class Stuff(name: String, value: String)
# Set[Stuff]
[{"name": "a", "value": "1"}, {"name": "123", "value": "something"}, {"name": "$#!@", "value": "other"}]
# What if the key constructors are inferred?
# How do you create a Set[Stuff]?
{"a": "1", "123": "something", "$#!@": "other"}
# this is relatively easy to do via an intermediate representation
# Map[String, String] -> Set[Stuff]