Skip to content

Instantly share code, notes, and snippets.

@rjsen
rjsen / Extends.scala
Last active July 9, 2018 03:44
Macro annotation to eliminate case class repetition
import scala.annotation.{StaticAnnotation, compileTimeOnly}
import scala.language.experimental.macros
import scala.reflect.macros.whitebox.Context
@compileTimeOnly("use macro paradise")
class Extends[T](defaults: Any*) extends StaticAnnotation {
def macroTransform(annottees: Any*): Any = macro Extends.impl
}
object Extends {
@rjsen
rjsen / CirceSupport.scala
Created December 9, 2015 19:58
Circe (un)marshaller for spray
import java.util.NoSuchElementException
import io.circe._
import spray.httpx.marshalling.Marshaller
import spray.httpx.unmarshalling.Unmarshaller
import spray.http._
// scalastyle:off
// providing return types causes a stack overflow, and the throw is necessary in this case
@rjsen
rjsen / Main.scala
Last active December 9, 2015 00:55
Scala FP exercise
/*
This is a port of Tony Morris' Haskell beginner list exercises from (http://blog.tmorris.net/posts/haskell-beginner-exercises-with-tests/index.html).
To run, compile with `scalac Main.scala` and then run `scala Main`.
*/
import scala.{List => _, :: => _, Nil => _}
sealed trait List[+T] {
def head: T
@rjsen
rjsen / ExampleTest.scala
Created June 4, 2015 07:27
Example Test
import org.apache.log4j.{Level, Logger}
import org.specs2.mutable._
import Predef.{conforms => _, _}
class ExampleTest extends Specification {
Logger.getRootLogger.setLevel(Level.ERROR)
sequential
"the transformStream method" should {
@rjsen
rjsen / TestContext.scala
Last active August 29, 2015 14:22
Test Context
import org.apache.spark.streaming.dstream._
import org.specs2.mutable._
import org.apache.spark.streaming._
import scala.collection.mutable
class spark[T](val seq: Seq[String])(implicit val fun: DStream[String] => DStream[T]) extends After {
lazy val ssc = new StreamingContext("local", "test", Seconds(1))
val rdd = ssc.sparkContext.makeRDD(seq)
val stream = new ConstantInputDStream(ssc, rdd)
@rjsen
rjsen / Example.scala
Created June 4, 2015 07:19
Example Streaming Application
import org.apache.spark.streaming.dstream.DStream
import org.apache.spark.streaming.dstream.DStream._
import org.joda.time.DateTime
import org.json4s.jackson.JsonMethods._
import scala.util.Try
case class Purchase(item_id: String, amount: BigDecimal, time: Long)
case class Key(item_id: String, time: DateTime)
case class Summary(item_id: String, time: DateTime, total: BigDecimal)