Skip to content

Instantly share code, notes, and snippets.

View cvogt's full-sized avatar

Jan Christopher Vogt cvogt

  • Symbiont.io
View GitHub Profile
package slick
/**
* This is some code extracted from TimeOut codebase, demonstrating:
* - Use of tag typed to avoid mixing session of different DB
* - The use of the Reader Monad to compose Actions together and defer the choice of async/sync computation
*
* I remove the part where we can say if our operation are read only or not (to use different connection), in order to
* make things easier.
**/
brew update
brew versions FORMULA
cd `brew --prefix`
git checkout HASH Library/Formula/FORMULA.rb # use output of "brew versions"
brew install FORMULA
brew switch FORMULA VERSION
git checkout -- Library/Formula/FORMULA.rb # reset formula
## Example: Using Subversion 1.6.17
#
@cvogt
cvogt / gist:6c0c9361e6868f66d7c9
Created June 20, 2014 23:10
Format code in osx clipboard via command line
pbpaste | highlight --src-lang=Scala -O rtf --style molokai --font-size 18 --font Consolas | pbcopy
package com.sport195.api
import play.api.libs.json._
import scala.language.experimental.macros
object ImplicitConversions {
import java.sql.Timestamp
import java.sql.Date
implicit val x = scala.language.implicitConversions
@cvogt
cvogt / gist:e9da3ebb8790fe2716ee
Last active August 29, 2015 14:06
ReactiveMongo reader writer handler to serialize nested seqs
implicit def MapReader[V,B <: BSONValue](implicit vr: BSONReader[B,V]) = new BSONDocumentReader[Seq[(String, V)]] {
def read(bson: BSONDocument): Seq[(String, V)] = {
val elements = bson.elements.map { tuple =>
tuple._1 -> tuple._2.seeAsTry(vr).get
}
elements
}
}
implicit def MapWriter[V,B <: BSONValue](implicit vw: BSONWriter[V,B]) = new BSONDocumentWriter[Seq[(String, V)]] {
@cvogt
cvogt / gist:e0d62be47f6ffa1f1407
Last active May 4, 2016 13:01
Slick pre-compiled inSet
/*
As discussed on
https://groups.google.com/d/msgid/scalaquery/b9991788-ec33-4b4d-badf-7edd51de754c%40googlegroups.com
*/
/** Contains pre-compiled inSet queries for various arities */
class PreCompiledInSeq[T,E,C[_],V](
q: Query[T,E,C[_]], c: Query[T,E,C[_]] => Column[V]
){
type C = Column[V]
import scalaz._
import Scalaz._
object MyApp extends App{
type Scott[V] = EitherT[Option,String,V]
type ScottI = Scott[Int]
def myf: Int => ScottI = _.point[Scott]
def lar: ScottI = for{
x <- myf(4)
@cvogt
cvogt / gist:81eeaf0a48c7810442ac
Last active August 29, 2015 14:06
TMap with mutual dependencies
// ideal world, where every library has a tmap enabled monadic interface:
object Dep1Lib{
def doSomeThing = for{
dep1context <- Implicit[Dep1Context]
dep3context <- Implicit[Dep3Context]
} yield /* ... do something ... */
}
object Dep3Lib{
def doSomeThingElse = for{
@cvogt
cvogt / gist:c1303f0dfaa2a87f6377
Created October 22, 2014 10:59
Type-indexed maps for fun and functional dependency injection
Type-indexed maps (TMap) are data structures that support type-safe,
constant time lookups of elements by their type. They are unordered,
allow structural typing and can be used as composable records. Unlike
HList-based implementations as in Shapeless, TMaps are implemented
using subtyping, which leads to pretty readable types and error messages.
TMap types can easily be written by hand.
TMaps are a perfect fit for monadic dependency injection. They enable
the Reader monad to be used for multiple dependencies with zero boiler
plate. The combination can be viewed as a partial implementation of
@cvogt
cvogt / gist:b86aff8ac51f49a574cc
Last active August 29, 2015 14:11
Immutable cycles (as seen in "Case classes a la carte with shapeless, now!" at scalax2014 by @milessabin)
// immutable cycle
class Node[T]( val value: T, _next: => Node[T] ){
lazy val next = _next
}
val cycle: Node[Int] = new Node( 1, new Node( 2, cycle ) )
// prints List(1, 2, 1, 2, 1, 2, 1, 2, 1, 2)
println(cycle.iterator.take(10).map(_.value).toList)
implicit class NodeIterator[T](node: Node[T]){