Skip to content

Instantly share code, notes, and snippets.

View edofic's full-sized avatar

Andraž Bajt edofic

View GitHub Profile
@edofic
edofic / fields.scala
Last active December 18, 2015 02:48
static field name checking
import language.dynamics
import language.experimental.macros
import reflect.macros.Context
class Fields[A] extends Dynamic {
import Fields._
def selectDynamic(nameE: String): String = macro impl[A]
}
object Fields {
@edofic
edofic / MongoDb.scala
Created June 10, 2013 21:28
reactive mongo without play
package models
import com.typesafe.config.ConfigFactory
import reactivemongo.api.{DefaultDB, DB, MongoDriver}
import akka.event.slf4j.Logger
import reactivemongo.api.collections.default.BSONCollection
import scala.util.Try
object MongoDb {
private val logger = Logger("MongoDB")
@edofic
edofic / Unbox.scala
Created June 25, 2013 20:36
regressing types through type functions. apparently scala type system can do this
trait Unbox {
type Box[A]
def apply[A](boxed: Box[A]): A = ???
}
object Id extends Unbox { type Box[A] = A }
case class C[A](a:A)
object CU extends Unbox { type Box[A] = C[A] }
@edofic
edofic / Awesome.scala
Created June 26, 2013 17:31
lying about return type (scala macros). compiler infers the type of generated code. sweet. as a bonus you can crash the compiler if you mix in some singleton types
import reflect.macros.Context
import language.experimental.macros
def producerImpl[A: c.WeakTypeTag](c: Context): c.Expr[Any] = {
c.universe.reify{
() => (??? : A)
}
}
def producer[A] = macro producerImpl[A]
@edofic
edofic / speed.scala
Created June 28, 2013 16:15
akka speed
import concurrent.Await
import concurrent.duration._
import akka.util.Timeout
import akka.pattern.ask
import concurrent.Future
import concurrent.ExecutionContext.Implicits.global
def time(n:Int)(block: => Unit) = {
var i = 0
val start = System.currentTimeMillis()
@edofic
edofic / compiler.scala
Created July 11, 2013 12:26
scala runtime compilation
val compile = {
import tools.reflect.ToolBox
val c = reflect.runtime.currentMirror
val tb = c.mkToolBox()
(src: String) => tb compile (tb parse (imports+src))
}
@edofic
edofic / build.sbt
Created July 16, 2013 18:25
build using my fork of reactive mongo
scalaVersion := "2.10.2"
resolvers += "edofic snapshots" at "http://repo.edofic.com/snapshots"
libraryDependencies += "org.reactivemongo" %% "reactivemongo-bson-macros" % "0.10-SNAPSHOT"
@edofic
edofic / http.scala
Created July 18, 2013 09:52
slumber like urls in scala
import language.dynamics
class Path(path: String) extends Dynamic {
def selectDynamic(m: String) = new Path(s"$path$m/")
def applyDynamic(m: String)(args: Any*) = s"$m: $path with args $args"
override def toString = s"Path($path)"
}
val root = new Path("/")
@edofic
edofic / unfold.scala
Created July 23, 2013 06:30
lazily generating values(iterator) from a starting point using a partial function
def unfold[A](starting: A)(f: A PartialFunction A): Iterator[A] = {
var state = starting
val tail = new Iterator[A]{
def hasNext = f.isDefinedAt(state)
def next() = {
state = f(state)
state
}
}
@edofic
edofic / extract.sh
Created August 29, 2013 19:16
extracht everyzing
#!/bin/bash
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) rar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xvf $1 ;;
*.tbz2) tar xvjf $1 ;;