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 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 = {
...
}
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
@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 / 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===
@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 / 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 / .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/