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 / json.scala
Last active July 18, 2016 17:00
micro api for json parsing using the scala standard library
implicit class JsonOps(json: Seq[_]){
def /(label: String): Seq[_] = json flatMap {
case s: Seq[_] => s / label
case m: Map[String@unchecked,_] => m.get(label).toSeq
case other => Nil
}
def as[T] = json.headOption.map(_.asInstanceOf[T])
}
@cvogt
cvogt / type-level-pattern-matching.scala
Created June 6, 2016 21:17
Scala type-level vs. value-level pattern matching
// value-level pattern matching
(1,"test", Person("Chris")) match {
case (x, s: String, t: Person) => doSomething(x)
}
//---------------------
// type-level pattern matching
@cvogt
cvogt / monadic.scala
Last active February 26, 2016 18:09
Monadic Context Nirvana
case class Person(name: String, livesAt: Int, isRich: Boolean )
case class Address(city: String)
// main
println( showPerson(1) )
println( showPerson(2) )
// Blocking
object DAO{
// Sample data coming from Database
@cvogt
cvogt / ->.scala
Last active February 23, 2016 19:39
-> type alias and extractor
type -> [L, R] = ( L, R )
object -> {
def apply[L, R]( l: L, r: R ) = ( l, r )
def unapply[L, R]( t: ( L, R ) ) = Option( t )
}
// usage
def foo(t: Int -> String -> Double) = t match { case i -> s -> d => … }
foo( 1 -> "x" -> 1.1 )
@cvogt
cvogt / showMissingInstance.scala
Last active February 9, 2016 19:30
Show missing type class instance
// error message not helpful
scala> class SomeTypeClass[T]
scala> case class Foo(s: String)
scala> implicit def fooTC(implicit ev: SomeTypeClass[String]) = new SomeTypeClass[Foo]
scala> implicitly[SomeTypeClass[Foo]]
<console>:15: error: could not find implicit value for parameter e: SomeTypeClass[Foo]
implicitly[SomeTypeClass[Foo]]
^
@cvogt
cvogt / ansi.scala
Created February 4, 2016 22:45
ansi color ast for strings.
sealed trait AnnotatedString {
def ++( other: AnnotatedString ) = Concat( this, other )
def mkString( codes: Vector[String] = Vector() ): String = this match {
case Plain( s ) => s
case Concat( left, right ) => left.mkString( codes ) ++ right.mkString( codes )
case Modify( s, f ) => f( s.mkString( codes ) ) // FIXME: this probably messes with the ansi codes
case Colored( code, s ) => code ++ s.mkString( codes :+ code ) ++ Console.RESET ++ codes.mkString
}
def map( f: String => String ) = Modify( this, f )
}
@cvogt
cvogt / BoundLens.scala
Last active June 1, 2016 22:24
Super nice Lens syntax
// usage example
import ai.x.generic.BoundLens.ImplicitBoundLens
case class Outer( inner: Inner, s: String )
case class Inner( i: Int )
val o = Outer(Inner(5), "foo")
o.lens(_.s).set("bar")
.lens(_.inner.i).modify(_+1) // .lens calls can be chained nicely
// implementation
@cvogt
cvogt / shell-out-in-sbt.scala
Created September 22, 2015 11:42
Add to your global.sbt
addCommandAlias("git", "; sh git")
commands ++= Seq(
Command.args("sh", "<shell command>") { (state, args) =>
// using script -q here to maintain ansi colors, see http://stackoverflow.com/a/13587964/467390
val ret = ("script -q /dev/null " ++ args.mkString(" ")) !
state
}
)
@cvogt
cvogt / monadic.scala
Last active September 11, 2015 07:29
how to get for-expression syntax for non-monads, when there is no monad nearby
import scalaz._
import Scalaz._
def monadic[T]( scope: Identity[Unit] => Identity[T] ) =
scope( Monad[Identity].point( () ) ).value
val z = monadic{
for{ _ <- _
x = 5
y = 6
@cvogt
cvogt / A.markdown
Last active August 29, 2015 14:27 — forked from umpirsky/A.markdown
Sublime Text Monokai Sidebar Theme.