Skip to content

Instantly share code, notes, and snippets.

View etorreborre's full-sized avatar
🏠
Working from home

Eric Torreborre etorreborre

🏠
Working from home
View GitHub Profile
class Example(description: String) {
def in[T](expectations: =>T)(implicit def m: scala.reflect.Manifest[T]): Example = {
if (m.erasure == this.getClass)
hasSubExamples = true
// store expectations for later evaluation
}
}
/**
object ExtendedFunctions extends ExtendedFunctions
trait ExtendedFunctions {
implicit def extend[A, B](f: Function[A, B]) = new ExtendedFunction(f)
}
/**
* this class allows to use a function as if it was a partial function.
*
* It is especially useful when one want to overload a function with both functions and partial function
* literals. This usually is an issue because of the way the compiler interprets function and
* partial function literals (see the Scala Language Specification, section 8.5).
// you can refactor all those mustEqual examples
// to be more concise and provide a complete description in the reports
"Json" should {
"quote strings" in {
"unicode within latin-1" in {
Json.quote("hello\n\u009f") mustEqual "\"hello\\n\\u009f\""
}
"unicode outside of latin-1 (the word Tokyo)" in {
Json.quote("\u6771\u4eac") mustEqual "\"\\u6771\\u4eac\""
}
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d9799dc, pid=6948, tid=7116
#
# JRE version: 6.0_20-b02
# Java VM: Java HotSpot(TM) Client VM (16.3-b01 mixed mode windows-x86 )
# Problematic frame:
# V [jvm.dll+0xc99dc]
#
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d9572f3, pid=1384, tid=3640
#
# JRE version: 6.0_21-b05
# Java VM: Java HotSpot(TM) Client VM (17.0-b15 mixed mode windows-x86 )
# Problematic frame:
# V [jvm.dll+0xa72f3]
#
/**
* A quick debugging tip
*/
/**
* If you have lots of definitions using the same type parameter names, like 'S'
*/
def beEmpty[S <: HasIsEmpty]: Matcher[S] = new EmptyMatcher()
def compose[S] (f: S => T) = new Matcher[S] { ... }
/**
* remove the first element satifying the predicate
* @return an iterable minus the first element satisfying the predicate
*/
def removeFirst(predicate: T => Boolean): Iterable[T] = {
if (xs.isEmpty) xs
else if (predicate(xs.head)) xs.drop(1)
else xs.take(1) ++ xs.removeFirst(predicate)
}
/**
import org.specs._
import org.scalacheck._
import com.codecommit.collection.Vector
/**
* An enhanced specification of the Vector class from http://github.com/djspiewak/scala-collections
*
* It uses "verifies" to remove the boilerplate declaration of a property which "must pass"
* It uses must throwAn[Exception] to specify that an exception must be thrown
/** I think it's better if we can define replaceFirst generally on Iterables */
def replaceFirst[T](it: Iterable[T], predicate:T=>Boolean, f:T=>T): Iterable[T] = {
if (it.isEmpty) it
else if (predicate(it.head)) it.take(1) .map(f) ++ it.tail
else it.take(1) ++ replaceFirst(it.tail, predicate, f)
}
/**
* Otherwise on Lists, that can be fun to use a foldLeft.
* It's a bit ugly though with the trailing _2
/*
This fails to compile with:
type mismatch;
found : Iterable[T]
required: CC[T]
else if (predicate(xs.head)) xs.drop(1)
^
type mismatch;
found : Iterable[T]