Skip to content

Instantly share code, notes, and snippets.

View Blaisorblade's full-sized avatar

Paolo G. Giarrusso Blaisorblade

View GitHub Profile
@Blaisorblade
Blaisorblade / ExtraSettings.tex
Created February 2, 2012 11:13
LaTeX listings: add support for Scala
% Other listings-related settings from
% http://lampsvn.epfl.ch/trac/scala/export/26099/scala-tool-support/trunk/src/latex/scaladoc.sty
% activate the language and predefine settings
\lstset{
language=Scala,%
xleftmargin=4mm,%
aboveskip=3mm,%
belowskip=3mm,%
fontadjust=true,%
columns=[c]fixed,%
struct vec2;
struct vec3;
template<int I, int J> struct MagicVec2
{
friend struct vec2;
inline MagicVec2(vec3* _this);
inline vec2 operator=(const vec2& that);
inline vec2 operator=(const MagicVec2<I, J> &that);
@Blaisorblade
Blaisorblade / Base.scala
Created March 21, 2012 10:42
Showcase bug SI-3346 and applications to deep embeddings
//Paste this code on a fresh Scala console.
trait Exp[+T]
case class Const[T](t: T) extends Exp[T]
implicit def toExp[T](t: T): Exp[T] = Const(t)
case class Plus(x: Exp[Int], y: Exp[Int]) extends Exp[Int]
class IntOps(x: Exp[Int]) {
def +(y: Exp[Int]) = Plus(x, y)
}
@Blaisorblade
Blaisorblade / BetterPartialOrdering.scala
Created July 2, 2012 01:18
Improve PartialOrdering as defined in the Scala standard library (2.9, 2.10)
//Much of the following code derives from scala.math.Ordering,
//and is released under the Scala LICENSE (http://www.scala-lang.org/node/146)
object BetterPartialOrdering {
trait PartialOrderingFromLteq[T] extends PartialOrdering[T] {
override def tryCompare(x: T, y: T): Option[Int] =
(lteq(x, y), lteq(y, x)) match {
case (true, false) => Some(-1)
case (false, true) => Some(1)
case (true, true) => Some(0)
case _ => None
import ivm._
import expressiontree._
import Lifting._
import collection.generic.CanBuildFrom
import collection.TraversableLike
object OptimizationV1 {
val mergeMaps: PartialFunction[Exp[_], Exp[_]] = {
case MapNode(m@MapNode(coll, f), g) =>
@Blaisorblade
Blaisorblade / ExprSplice.scala
Created July 30, 2012 16:07
Examples of macros: inside reify, only .splice, not .value can be called on Expr
import scala.reflect.makro.Context
import language.experimental.macros
def macroId[A](c: Context)(v: c.Expr[A]): c.Expr[A] = c.reify(v.splice)
def id2[A](v: A) = macro macroId[A]
id2(1)
@Blaisorblade
Blaisorblade / TestPatMat.out
Created August 8, 2012 22:22
Testing pattern matcher virtualization
$ ~/opt/scala-2.10.0-M6/bin/scalac -Xexperimental ./test-src/epfl/test4-functions/TestPatMat.scala
./test-src/epfl/test4-functions/TestPatMat.scala:63: error: type mismatch;
found : Prog.this.Rep[Prog.this.Rep[Int]] => Prog.this.M[String]
required: Prog.this.Rep[Int] => Prog.this.M[?]
def test = repInt(7) match { case 5 => "foo" case _ => "bar" }
^
one error found
//Paste this in the REPL
import language.implicitConversions
trait Exp[+T]
trait Def[+T]
case class Eq[T](a: Exp[T], b: Exp[T]) extends Def[T]
case class Const[T](t: T) extends Exp[T]
class Eq2[T](a: Exp[T], b: Exp[T]) extends Eq[T](a, b) with Exp[T] //This can be defined before or after f1 and f2, it makes no difference.
@Blaisorblade
Blaisorblade / Foo.scala
Created October 3, 2012 22:13
Bugreport example
object Foo {
trait Exp[+T]
case class Const[+T](t: T) extends Exp[T]
implicit def fooPure[T](t: T): Exp[T] = Const(t)
class WithAsFoo[T](t: T) {
def asFoo(implicit conv: T => Exp[T]) = conv(t)
}
implicit def toWithAsFoo[T](t: T) = new WithAsFoo(t)
implicit def toMapSeq[T](e: Exp[Seq[T]]) = new MapSeq(e)
+++ b/src/library/scala/Enumeration.scala
+ @deprecated("Names should be specified individually or discovered via reflection", "2.10.0")
+ def this(initial: Int, names: String*) = {
+ @deprecated("Names should be specified individually or discovered via reflection", "2.10.0")
def this(names: String*) = this(0, names: _*)
+++ b/src/library/scala/Predef.scala
+ // @deprecated("Use scala.AnyRef instead", "2.10.0")
+ // def AnyRef = scala.AnyRef