This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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=== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val addedTrait = Select(Select(Select( | |
Select(Ident(newTermName("com")), newTermName("imranrashid")), | |
newTermName("oleander")),newTermName("macros")), | |
newTypeName("SimpleTrait")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> trait A | |
defined trait A | |
scala> | |
scala> class B extends A | |
defined class B | |
scala> class C extends A | |
defined class C |