Skip to content

Instantly share code, notes, and snippets.

val n = 1e7.toInt
val (rawArray, rawBB, rawFloatBuf) = initArrays(n)
println("result = " + th.pbench({
var idx = 0
var sum = 0f
while(idx < n) {
sum += rawArray(idx)
idx += 1
}
sum
trait ArrayLike[@specialized T] {
def apply(idx: Int) : T
def update(idx: Int, value: T)
def length: Int
def size: Int = length
}
def sumArrayLike(arr: ArrayLike[Float]): Float = {
var idx = 0
var sum = 0f
trait ArraySummer[-T] {
def sum(t: T): Float
}
/** one TypeClass per concrete implementation */
object SpecificSummers {
implicit object SimpleWrappedFloatArraySummer extends ArraySummer[SimpleWrappedFloatArray] {
def sum(arr: SimpleWrappedFloatArray): Float = {
...
}
@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
@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 / 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 / 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 / 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 / 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 / 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===