Skip to content

Instantly share code, notes, and snippets.

@squito
squito / .ctags
Last active January 2, 2016 18:09
My computer config
--langdef=Scala
--langmap=Scala:.scala
--regex-Scala=/^[ \t]*class[ \t]*([a-zA-Z0-9_]+)/\1/c,classes/
--regex-Scala=/^[ \t]*object[ \t]*([a-zA-Z0-9_]+)/\1/o,objects/
--regex-Scala=/^[ \t]*trait[ \t]*([a-zA-Z0-9_]+)/\1/t,traits/
--regex-Scala=/^[ \t]*case[ \t]*class[ \t]*([a-zA-Z0-9_]+)/\1/r,cclasses/
--regex-Scala=/^[ \t]*abstract[ \t]*class[ \t]*([a-zA-Z0-9_]+)/\1/a,aclasses/
--regex-Scala=/^[ \t]*def[ \t]*([a-zA-Z0-9_=]+)[ \t]*.*[:=]/\1/m,methods/
--regex-Scala=/[ \t]*val[ \t]*([a-zA-Z0-9_]+)[ \t]*[:=]/\1/V,values/
--regex-Scala=/[ \t]*var[ \t]*([a-zA-Z0-9_]+)[ \t]*[:=]/\1/v,variables/
@squito
squito / implict_wrapper_vs_witness.scala
Last active December 30, 2015 20:39
I first thought of making a union bound using implicit classes, but I realized it would need extra object allocation. The better answer is found here: http://stackoverflow.com/a/3508555/1442961 HOWEVER, that answer doesn't work for varargs
trait Wrapper {
def v: String
}
implicit class StringAsWrapper(s: String) extends Wrapper {
println(s"constructing string wrapper")
def v = s
}
implicit class SymbolAsWrapper(s: Symbol) extends Wrapper {
@squito
squito / annotation_type_parameters.scala
Last active December 30, 2015 15:19
experiments with reflection. goes with a blog post
//the annotation class has type parameter T
class FillDefsWithReflection[T] extends StaticAnnotation {
def macroTransform(annottees: Any*) = macro FillDefsWithReflectionImpl.impl
}
object FillDefsWithReflectionImpl {
def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
import c.universe._
//we need to do some pattern matching to pull out just the type of the trait
val targetTrait = c.prefix.tree match {
@squito
squito / implicits.scala
Last active December 29, 2015 00:39
useful scala implicits. Since IDEs dont' have nice tools for auto-completing implicit imports, this is a handy reference
// for 20.seconds, etc.
import scala.concurrent.duration._
// this is twitters version, only use if you need it to work w/ another API, otherwise prefer the scala version
import com.twitter.conversions.time._
//for 6.megabytes.bytes
import com.twitter.conversions.storage._
//===QF implicits===
@squito
squito / KafkaSimpleConsumerUtils.scala
Created November 20, 2013 15:14
Some scala utils for working w/ kafka SimpleConsumer. realized there was a way for me to use the ConsumerGroup API instead, so putting this here just in case its useful to me later on ...
import kafka.consumer.SimpleConsumer
import kafka.common.TopicAndPartition
import kafka.api._
import scala.annotation.tailrec
import scala.util._
object KafkaSimpleConsumerUtils {
def getLastOffset(consumer: SimpleConsumer, topic: String, partition: Int, whichTime: Long, clientName: String): Try[Long] = {
val tap = new TopicAndPartition(topic, partition)
@squito
squito / findFirst.scala
Last active December 28, 2015 20:18
find first match in scala nested for loops
val data = (1 to 10).toSeq.map{i => (0 to i).map{_.toString}.toSeq}
val iters = for {
sub <- data.iterator
d <- sub.iterator
} yield d
val r = iters.find{x =>
println(s"checking $x")
x == "6"
@squito
squito / badValDefMacro.scala
Last active August 12, 2020 20:57
some basic macros with quasiquotes. Accompanies this blog post <http://imranrashid.com/posts/learning-scala-macros/>. Partially a translation of this example <http://www.warski.org/blog/2012/12/starting-with-scala-macros-a-short-tutorial/> to quasiquotes.
def getValMacro(c: Context)(s: c.Expr[Any]) : c.Expr[Any] = {
import c.universe._
val q"val $name = $value" = s.tree
c.Expr(value)
}
def getVal(s: Any) = macro getValMacro
getVal{val x = 17}
error: exception during macro expansion:
@squito
squito / addedTrait.scala
Last active January 28, 2016 07:41
code samples to go along w/ a blog post on macros imranrashid.com/posts/learning-scala-macros/
val addedTrait = Select(Select(Select(
Select(Ident(newTermName("com")), newTermName("imranrashid")),
newTermName("oleander")),newTermName("macros")),
newTypeName("SimpleTrait"))
@squito
squito / macro compiler error.scala
Last active August 23, 2017 05:45
The horrors of compiler errors when working with macros ...
scala> def classExpandMacroImpl(c: Context)(s: c.Expr[Any]) : c.Expr[Any] = {
| import c.universe._
|
| val cdef = s.tree match {
| case Block(List(x:ClassDef), _) => x
| case _ => c.abort(c.enclosingPosition, "Was expecting a block w/ a ClassDef")
| }
|
| val q"class $name { ..$body }" = cdef
| val newdefs = List[c.universe.Tree](q"def x: Int = z + 7", q"def y: Float = z + 3.2f")
@squito
squito / gist:5921876
Created July 3, 2013 19:20
type variance & toMap
scala> trait A
defined trait A
scala>
scala> class B extends A
defined class B
scala> class C extends A
defined class C