Skip to content

Instantly share code, notes, and snippets.

package net.tixxit.snippet
import scala.collection.IterableLike
import scala.collection.generic.CanBuildFrom
import scala.collection.mutable.Builder
import scala.annotation.tailrec
import spire.syntax._
def seq[A: c.WeakTypeTag](c: Context)(es: List[c.Expr[A]]): c.Expr[List[A]] = {
import c.universe._
val ident = Ident(weakTypeTag[A].tpe.typeSymbol)
c.Expr(Apply(TypeApply(Select(Select(This(newTypeName("immutable")), newTermName("List")), newTermName("apply")), List(ident)), es map (_.tree)))
}
final case class Epsilon[A](value: A)
object Epsilon {
def apply[A: Field](x: Double): A = Field[A].fromDouble(x)
}
// This can be part of the MetricSpace ops.
implicit class ApproxEq[A](x: A) {
def =~=[B](y: A)(implicit ms: MetricSpace[A, B], e: Epsilon[B], o: Order[B]): Boolean =
MetricSpace.close(x, y)(ms, e, o)
package benchmark
import org.scalameter.api._
import spire.algebra._
import spire.math._
import spire.syntax._
import spire.std.long._
sealed trait WithFallback[+A, +B] {
def fold[C](f: A => C, g: B => C): C
}
case class Preferred[+A](a: A) extends WithFallback[A, Nothing] {
def fold[C](f: A => C, g: B => C): C = f(a)
}
case class Fallback[+B](b: B) extends WithFallback[Nothing, B] {
def fold[C](f: A => C, g: B => C): C = g(b)
}
import scala.reflect.ClassTag
sealed trait Value
case class ValueArray[A](xs: Array[A])(implicit val ct: ClassTag[A]) extends Value
case class Text(value: String) extends Value
def doStuff(lit: Value): Unit = lit match {
case (al: ValueArray[a]) =>
implicit val ct = al.ct
val yay = new Array[a](2)
import shapeless._
import shapeless.ops.traversable._
trait ListApply[A] {
def apply(xs: List[_]): Option[A]
}
object ListApply {
implicit def genericListApply[A, L <: HList](gen: Generic.Aux[A, L], toHList: FromTraversable[L]) =
new ListApply[A] {
import scalaz._
import scala.util.Try
final class X {
val magic: Int = scala.util.Random.nextInt(10000)
def isStable: Boolean = magic == 42
}
object Main extends App {
package net.tixxit.data
import scala.annotation.tailrec
import scala.collection.generic.CanBuildFrom
import scala.math.Ordering
import scala.math.Ordering.Implicits._
/**
* An immutable list that maintains a sorted order. At any time, the entire
* list can be traversed, in sorted order in O(n). A single insert is O(log n),
package net.tixxit.snippets;
import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
public class VolatileVsThreadLocalBenchmark {
@State(Scope.Benchmark)
public static class ThreadLocalState {