Skip to content

Instantly share code, notes, and snippets.

View asflierl's full-sized avatar

Andreas Flierl asflierl

View GitHub Profile
import java.lang.Character.{isWhitespace, charCount}
implicit class SuperString(str: String) extends AnyVal {
def isBlank: Boolean = {
@tailrec def check(index: Int): Boolean =
if (index >= str.length) true
else {
val cp = str.codePointAt(index)
if (isWhitespace(cp)) check(index + charCount(cp)) else false
}
@asflierl
asflierl / scala_2.10_
Created January 30, 2013 19:44
Dependent method types in Scala 2.10.... ooooOOOO yeah!
Welcome to Scala version 2.10.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_11).
Type in expressions to have them evaluated.
Type :help for more information.
scala> trait Meep { type Moop; def f: Moop }
defined trait Meep
scala> def fnord(a: Meep): a.Moop = a.f
fnord: (a: Meep)a.Moop
scala> def f[A](x: A, xs: List[A]): List[A] = x :: xs
f: [A](x: A, xs: List[A])List[A]
scala> def g[A](x: A)(xs: List[A]): List[A] = x :: xs
g: [A](x: A)(xs: List[A])List[A]
scala> f(3, _)
<console>:9: error: missing parameter type for expanded function ((x$1) => f(3, x$1))
f(3, _)
^
@asflierl
asflierl / compile output
Created January 4, 2013 17:21
line 3 and 5 seem to work just fine; line 4 yields that unfriendly error message
[error] package.scala:4: macros cannot be partially applied
[error] def mf(args: Any*): String = sc.f(args:_*).stripMargin
[error] ^
@asflierl
asflierl / build.sbt
Created April 15, 2012 10:18 — forked from jmhofer/build.sbt
sbt build file with sample jacoco4sbt configuration
import de.johoop.jacoco4sbt._
import JacocoPlugin._
organization := "my.organization"
scalaVersion := "2.9.1"
name := "my-project"
seq(jacoco.settings : _*)
@asflierl
asflierl / gist:1015483
Created June 8, 2011 21:40
test setup
testOptions <+= crossTarget { ct =>
Tests.Setup(() => { System.setProperty("specs2.outDir", ct.getAbsolutePath); () })
}
@asflierl
asflierl / gist:762943
Created January 2, 2011 23:55
benchmark for specialized numeric abstraction
trait Plus1[A] {
def plus(a: A, b: A): A
}
object Plus1 {
implicit val LongPlus1 = new Plus1[Long] {
def plus(a: Long, b: Long) = a + b
}
}
trait Plus2[@specialized A] {
object Looping {
val l = 1000
val l2 = l * l
val l3 = l * l * l
def main(args: Array[String]): Unit = {
warmup()
measure()
}
case class Cat(
name: String,
age: Int,
weight: Int,
food: String,
likesMe: Boolean)
val real = Cat("Garfield", 11, 8, "lasagna", false)
val wish = real.copy(likesMe = true)