Skip to content

Instantly share code, notes, and snippets.

@davidhoyt
davidhoyt / projection-scalatest.scala
Last active April 4, 2016 02:01
Maintain references to a source even when performing operations such as map or flatMap.
import org.scalatest.enablers.{Emptiness, Sequencing}
import scala.collection.GenTraversable
import scalaz.~>
trait ProjectionScalaTest {
object listToStream extends (List ~> Stream) {
override def apply[A](fa: List[A]): Stream[A] = fa.toStream
}
@davidhoyt
davidhoyt / OhcCodec.scala
Last active March 30, 2016 23:48
Automatic type class derivation for ohc
import java.nio.ByteBuffer
import shapeless._
trait OhcCodec[T]
extends OhcEncoder[T]
with OhcDecoder[T]
trait OhcEncoder[-T] {
def ohcEncode(given: T, buffer: ByteBuffer): Unit
@davidhoyt
davidhoyt / semigroup-hlist.scala
Last active January 25, 2016 05:41
Combine 2 shapeless HLists of the same type using cats Semigroup
import shapeless._
import cats.std.all._
trait ShapelessCatsSemigroup {
implicit object HNilSemigroup extends Semigroup[HNil] {
override def combine(h1: HNil, h2: HNil): HNil = HNil
}
implicit def HListSemigroup[H : Semigroup, T <: HList : Semigroup]: Semigroup[H :: T] =
new Semigroup[H :: T] {
@davidhoyt
davidhoyt / slick-rx.scala
Created August 29, 2015 00:17
Slick 3 DatabasePublisher to Rx Observable
import rx.lang.scala.{Subject, Observable}
import slick.backend.DatabasePublisher
import scala.concurrent.ExecutionContext
import scala.util.{Failure, Success}
trait StreamExtensions {
implicit def extendDatabasePublisher[T](publisher: DatabasePublisher[T]): StreamExtensions.DatabasePublisherExtensions[T] =
StreamExtensions.DatabasePublisherExtensions[T](publisher)
}
@davidhoyt
davidhoyt / ringAroundTheMonad.scala
Last active August 29, 2015 14:27 — forked from trane/ringAroundTheMonad.scala
playing with Coyoneda+Free Monad for algebraic Rings.. and funsies
import algebra._, std.int._
import cats._, free._, Free._
import scala.language.implicitConversions
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]
type Exp[A] = FreeC[Expr, A]
@davidhoyt
davidhoyt / slick3-cats-example.scala
Last active August 24, 2017 14:10
Slick 3 and cats
case class UserId(value: String) extends AnyVal with slick.lifted.MappedTo[String]
case class User(id: UserId, userName: String, password: String)
trait UsersTable extends TableDefinition with TableSchema { provider: DriverProvider =>
import driver.api._
override type TableRecordType = User
override val table = TableQuery[TableDefinition]
@davidhoyt
davidhoyt / by-name.scala
Created April 22, 2015 07:05
Wrapper for by-name expressions to make them eligible for implicit resolution and implicit conversion
/**
* Provides a wrapper for by-name expressions with the intent that they can become eligible for
* implicit conversions and implicit resolution.
*
* @param expression A by-name parameter that will yield a result of type `T` when evaluated.
* @tparam T Result type of the evaluated `expression`.
*/
final class ByName[+T](expression: => T) extends (() => T) {
/**
* Evaluates the given `expression` every time <em>without</em> memoizing the result.
@davidhoyt
davidhoyt / spray-multimap.scala
Last active August 29, 2015 14:19
Spray multimap for form fields and parameters
type MultiMap = Map[String, List[String]]
def formFieldMultiMap: Directive1[MultiMap] = {
requestInstance.flatMap[MultiMap :: HNil] { request =>
import spray.httpx.unmarshalling._
val result: Deserialized[MultiMap] =
request.as[HttpForm].right.flatMap {
case FormData(fields) =>
val collected = fields.groupBy { case (k, _) => k }
@davidhoyt
davidhoyt / chunked-hlist-with-shapeless-and-spray
Last active August 29, 2015 14:14
Attempts to take an HList like String :: Int :: HNil and zip it with resolved type class instances to produce (String, Marshaller[String]) :: (Int, Marshaller[Int]) :: HNil and then for each pair emit an http chunk via spray
// http://scastie.org/8089
/***
scalaVersion := "2.11.4"
libraryDependencies ++= Seq(
"io.spray" %% "spray-routing-shapeless2" % "1.3.2"
exclude("com.chuusai", "shapeless"),
"com.chuusai" %% "shapeless" % "2.1.0-RC2",
"com.typesafe.akka" %% "akka-actor" % "2.3.9"
)
@davidhoyt
davidhoyt / random-hystrix-thoughts.scala
Created June 18, 2014 04:22
Random hystrix thoughts
case class HystrixData(group: String, command: String, timeout: Int = 1000)
object Hystrix {
import com.netflix.hystrix._
import rx.{Subscription, Subscriber}
import scala.concurrent.ExecutionContext
import scala.concurrent.{Promise, Future}
//B = return value or fallback
//A = optional value to call the hystrix command with