Skip to content

Instantly share code, notes, and snippets.

@jrudolph
jrudolph / type-test.scala
Created October 25, 2014 09:36
Getting return types of methods without actually calling them
import scala.reflect.{ classTag, ClassTag }
trait X {
def f: Seq[Int]
}
trait Getter[T] {
def apply[U: ClassTag, C[_]](f: T ⇒ C[U]): ClassTag[U] = implicitly[ClassTag[U]]
}
object Getter extends Getter[Any]
object Tcp {
trait ConnectionDescriptor {
def remoteAddress: InetSocketAddress
def localAddress: InetSocketAddress
}
sealed trait ConnectionFlow extends Flow[ByteString, ByteString] {
def remoteAddress(mMap: MaterializedMap): InetSocketAddress
def localAddress(mMap: MaterializedMap): InetSocketAddress
@jrudolph
jrudolph / trace.log
Created December 19, 2014 13:02
Akka HttpServer trace
> 01 0 0 flow-1-1-map got ExposedPublisher
< 01 0 0 flow-1-1-map got ExposedPublisher
> 02 0 0 flow-1-1-map got SubscribePending$
< 02 0 0 flow-1-1-map got SubscribePending$
> 01 0 0 flow-1-2-1-foreach-stageFactory got ExposedPublisher
< 01 0 0 flow-1-2-1-foreach-stageFactory got ExposedPublisher
> 02 0 0 flow-1-2-1-foreach-stageFactory got SubscribePending$
< 02 0 0 flow-1-2-1-foreach-stageFactory got SubscribePending$
> 03 0 0 flow-1-2-1-foreach-stageFactory got OnSubscribe
< 03 0 0 flow-1-2-1-foreach-stageFactory got OnSubscribe
@jrudolph
jrudolph / WhatScalaGenerates.scala
Created April 29, 2015 06:35
ParameterDirectives
// generated using a simple showTree macro:
parameter("name") // expands to:
parameters(directives.this.ParamDefMagnet.apply[String]("name")(directives.this.ParamDefMagnet2.forString(unmarshalling.this.Deserializer.liftToSourceOption[String, String](unmarshalling.this.Deserializer.fromFunction2Converter[String, String](scala.this.Predef.$conforms[String])))))
parameter("name", "address") // expands to (shapeless 1):
parameters(directives.this.ParamDefMagnet.apply[(String, String)](scala.Tuple2.apply[String, String]("name", "address"))(directives.this.ParamDefMagnet2.forTuple[(String, String), shapeless.::[String,shapeless.::[String,shapeless.HNil]], this.Out](shapeless.this.HListerAux.tupleHLister2[String, String], directives.this.ParamDefMagnet2.forHList[shapeless.::[String,shapeless.::[String,shapeless.HNil]]](shapeless.this.LeftFolder.leftFolder[shapeless.::[String,shapeless.::[String,shapeless.HNil]], spray.routing.Directive0, spray.routing.directives.ParamDefMagnet2.MapReduce.type, this.R](shapeless
@jrudolph
jrudolph / branch-targets.sbt
Last active August 29, 2015 14:25
Setup per-branch target directories in sbt
// Helper snippet to enable per-git-branch target directories
//
// This will move the target directory for a sub-module away
// from <project-root>/<sub-module-name>/target
// to <project-root>/per-branch-targets/<branch-name>/<sub-module-name>
//
// Why?: shorter wait times if you switch branches often enough
// (Example: Akka's project file take 20+ seconds to compile and
// a single line change in AkkaBuild.scala will always
// trigger a complete rebuild.)
import scala.collection.immutable.{TreeMap,SortedMap}
def fromListWith[A,K<%Ordered[K]](seq:Iterable[(K,A)])(f: (A,A)=>A):SortedMap[K,A] =
seq.foldLeft(TreeMap.empty[K,A]){
(map:TreeMap[K,A],entry) =>
val (key,ele) = entry
// update entry by either applying f to existing elements or
// just starting a new entry
map.update(key,map.get(key).map(f(ele,_)).getOrElse(ele))
}
// This code is based on an idea of Rafael de F. Ferreira and was implemented as a response to
// Jim McBeath's blog post http://jim-mcbeath.blogspot.com/2009/09/type-safe-builder-in-scala-part-2.html
// Everyone is free to use, modify, republish, sell or give away this work without prior consent from anybody.
object Scotch {
sealed abstract class Preparation
case object Neat extends Preparation
case object OnTheRocks extends Preparation
case object WithWater extends Preparation
unzip -q $1 -d $1.dir &&
find $1.dir ! -executable -type d -exec chmod +rwx {} \; &&
find $1.dir ! -readable -exec chmod +r {} \; &&
rm $1 &&
mv $1.dir $1 &&
echo $1
final class MyRange(start: Int, limit: Int, step: Int) extends IndexedSeq[Int] {
val length: Int = {
def plen(start: Int, limit: Int, step: Int) =
if (limit <= start) 0 else (limit - start - 1) / step + 1
if (step > 0) plen(start, limit, step)
else plen(limit, start, -step)
}
override def apply(idx: Int) = {
if (idx < 0 || idx >= length) throw new IndexOutOfBoundsException(idx.toString)
start + idx * step
#!/usr/bin/perl -w
# This is derived from the script posted by Thomas Rast in this mailing list post:
# http://n2.nabble.com/Blamming-a-diff-between-two-commits-td2340836.html
sub parse_hunk_header {
my ($line) = @_;
my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
$line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
$o_cnt = 1 unless defined $o_cnt;