Skip to content

Instantly share code, notes, and snippets.

View cvogt's full-sized avatar

Jan Christopher Vogt cvogt

  • Symbiont.io
View GitHub Profile
@cvogt
cvogt / gist:9519186
Created March 12, 2014 23:53
Nested entity mapping with Slick 1.x
case class Part1(i1: Int, i2: String)
case class Part2(i1: String, i2: Int)
case class Whole(p1: Part1, p2: Part2)
class Tuple2Mapper[Entity,Component1,Tuple1,Component2,Tuple2](
entityFactory: (Component1,Component2) => Entity,
entityExtractor: Entity => Option[(Component1,Component2)],
component1factory: Tuple1 => Component1,
component1extractor: Component1 => Option[Tuple1],
component2factory: Tuple2 => Component2,
component2extractor: Component2 => Option[Tuple2]
trait Monoid[M] {
def zero: M
def add(m1: M, m2: M): M
}
trait Foldable[F[_]] {
def foldl[A, B](as: F[A], z: B, f: (B, A) => B): B
}
def mapReduce[F[_], A, B](as: F[A], m: Monoid[A])
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)]] {
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