Skip to content

Instantly share code, notes, and snippets.

View gist:9349865
import java.util.concurrent.LinkedBlockingDeque
import scala.annotation.tailrec
object `MyOwnFsm;)` extends App{
// val musta = new Musta
// musta ! "to5"
// musta ! "to6"
// musta ! "should crash"
// println
val pc = new PorcheCounter
1 to 3 foreach { _ => pc ! Car("porsche") }
@Arneball
Arneball / gist:9414661
Created Mar 7, 2014
Json Quasiquotes example
View gist:9414661
sealed trait JsValue {
override final def toString = mkString
def mkString: String
}
case class JsArray(value: JsValue*) extends JsValue {
def mkString = value.map{ _.mkString }.mkString("[", ", ", "]")
}
case class JsObj(value: (String, JsValue)*) extends JsValue {
def mkString = value.map{
case (k, v) => s""""$k": ${v.mkString}"""
@Arneball
Arneball / Macro
Last active Aug 29, 2015
"Serialize" an sequence of objects to an sequence of AnyRefs containing the elements of the object
View Macro
case class PackedArray[T](elems: Array[AnyRef])
object Packer {
def unapply[T](pa: PackedArray[T]): Seq[T] = macro unpack_impl[T]
def unpack_impl[T : c.WeakTypeTag](c: Context)(pa: c.Expr[PackedArray[T]]): c.Expr[Seq[T]] = {
import c.universe._
val tTyp = weakTypeOf[T]
val Some(fields) = tTyp.declarations.collectFirst {
case cons: MethodSymbol if cons.isPrimaryConstructor && !cons.isPublic =>
c.error(c.enclosingPosition, "No public primary constructor found")
Nil
View gist:b89ad0ac7a1ae81b3099
/**
* Created by arneball on 2014-05-08.
*/
import java.util.{List => JList}
import retrofit.client.Response
import retrofit.http.{Path, GET}
import retrofit.{RetrofitError, Callback, RestAdapter}
import collection.JavaConversions._
object RetroFit extends App {
@Arneball
Arneball / gist:a222b99a7b689bde9585
Last active Aug 29, 2015
Macro annotation extension method
View gist:a222b99a7b689bde9585
class ext extends StaticAnnotation {
def macroTransform(annottees: Any*) = macro extension.impl
}
object extension {
def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
import c.universe._
annottees.map{ _.tree }.head match {
case q"def $name[..$tp](...$params): $ret = $b" =>
val Seq(Seq(thiz, rest @ _*), rest2 @ _*) = params
View gist:392ea620693697414b24
/**
* Created by arneball on 2014-05-31.
*/
object LazyTest extends App {
val n = new LazyTest
try {
println(n.lazyIntInDaHouse)
} catch { case t: Throwable => }
println(n.lazyIntInDaHouse)
}
@Arneball
Arneball / gist:5b7e2e90d889ef9d30ed
Created Jun 14, 2014
Extractor object generation
View gist:5b7e2e90d889ef9d30ed
/**
* Created by arneball on 2014-06-14.
*/
object ExtractorTest extends App with Integral.ExtraImplicits{
@extract def longString(str: String) = str.length > 1
@extract def cool(int: Int) = Option(int).filter{ 2 < _ }.map{ 2 * }
@extract def evensum[T : Integral](l: List[T]) = {
l.sum % implicitly[Integral[T]].fromInt(2) == 0
View gist:276ec14faa79be2d2b49
trait WkRef[+T >: Null <: AnyRef] extends WeakReference[T @uncheckedVariance] {
def map[U >: Null <: AnyRef](f: T => U): WkRef[U] = if(isDefined) new WeakRef[U](f(get)) else WeakNone
def flatMap[U >: Null <: AnyRef](f: T => WkRef[U]) = if(isDefined) f(get) else WeakNone
def isDefined: Boolean
def foreach[U](f: T => U) = if(isDefined) f(get)
def filter(f: T => Boolean): WkRef[T] = if(isDefined && f(get)) this else WeakNone
}
object WkRef {
def apply[T >: Null <: AnyRef](t: T) = t match {
case null => WeakNone
View gist:b9d47b352ca460590023
Compiled from "MyInterface.java"
public interface batik.MyInterface {
public abstract java.util.List<java.lang.String> strings();
public int totalLength(); // OBSERVERA DEFAULT METHOD I INTERFACE
Code:
0: iconst_0
1: istore_1
2: aload_0
3: invokeinterface #1, 1 // InterfaceMethod strings:()Ljava/util/List;
View gist:0e47ce989aa928afe055
object MySorts extends Ordering.ExtraImplicits {
def mergeSort[T : Ordering](elems: List[T]): List[T] = elems match {
case Nil | _::Nil => elems
case that =>
type L = List[T]
def merge(l1: L, l2: L, acc: L): L = l1 -> l2 match {
case (a::as, b::_) if a < b => merge(as, l2, a::acc)
case (_, b::bs) => merge(l1, bs, b::acc)
case (a::as, _) => merge(as, l2, a::acc)
You can’t perform that action at this time.