Skip to content

Instantly share code, notes, and snippets.

@chrislewis
chrislewis / interpreter.scala
Created March 10, 2012 15:39
quick interpreter for nescala
package necsala.embedded
import scala.tools.nsc.interpreter.AbstractFileClassLoader
import scala.tools.nsc.{Global, Settings}
import scala.tools.nsc.util.BatchSourceFile
import scala.tools.nsc.io.{AbstractFile, VirtualDirectory}
import java.io.File
import java.util.jar.JarFile
import java.net.URLClassLoader
class Lens[A, B](
val get: A => B,
val set: (B, A) => A
) {
def andThen[C](l: Lens[B, C]): Lens[A, C] =
Lens(
(a) => l.get(get(a)),
(c, a) => set(l.set(c, get(a)), a)
)
}
@chrislewis
chrislewis / interpret_me.scala
Created December 6, 2011 04:58
Scala script file (embedded example)
/*
* Script for an embedded compiler like https://github.com/twitter/util/blob/master/util-eval/src/main/scala/com/twitter/util/Eval.scala
* The script is read as a string and wrapped in a randomly generated subclass of () => Any.
* The contents of the script constitute the body of the generated class' apply method.
* The interpreter loads the generated class, casts to () => Any, and invokes apply, presumably
* casting to a type parameter as needed.
* The end result of an interpreted file is just like a statement in scala: the last value
* is the result (also note that the entire body of the script is scoped to the containing method).
*/
trait Zero[+A] {
val zero: A
}
implicit object IntZero extends Zero[Int] {
val zero = 0
}
implicit def ListZero[A](implicit ev: Zero[A]) =
new Zero[List[A]] { val zero = Nil }
/* Real details omitted. */
val ssh = Ssh(Host("localhost"), UsernameAndPassword("xx", "xx"), Some("/xx/.ssh/known_hosts"))
/* Replicate some block as a set of Callable[A]s */
object Callable {
def replicate[A](count: Int)(block: => A) =
(1 to count).map(_ => new java.util.concurrent.Callable[A] {
def call() = block
})
}
object Concat {
def concat(s: String)(ss: String) = s + ss
def concat(s: String)(i: Int) = i + s
}
protovisLine("requestPerformanceChart", { container: $("#requestPerformance"), series: [ { label: "Request Completed", data: [ [ 1314113720851 , 510] , [ 1314113729370 , 700] , [ 1314113739397 , 512] , [ 1314113749359 , 486] , [ 1314113759371 , 433] , [ 1314113769387 , 371] , [ 1314113779362 , 294] , [ 1314113789358 , 357] , [ 1314113799362 , 317] , [ 1314113809362 , 302] , [ 1314113819389 , 368] , [ 1314113829385 , 268] , [ 1314113839372 , 326] , [ 1314113849377 , 378] , [ 1314113859397 , 402] , [ 1314113869373 , 310] , [ 1314113879368 , 281] , [ 1314113889367 , 425] , [ 1314113899361 , 311] , [ 1314113909358 , 305] , [ 1314113919368 , 329] , [ 1314113929367 , 288] , [ 1314113939372 , 275] , [ 1314113949363 , 303] , [ 1314113959367 , 268] , [ 1314113969370 , 321] , [ 1314113979371 , 391] , [ 1314113989369 , 395] , [ 1314113999367 , 331] , [ 1314114009368 , 354] , [ 1314114019372 , 410] , [ 1314114029586 , 309]
@chrislewis
chrislewis / lazy_pimp.scala
Created August 16, 2011 01:28
lazy json conversion via lift-json with explicit pimp
object LazyJson {
import net.liftweb.json._
import net.liftweb.json.Extraction._
/* Hintless format. Used by decompose, which will end up doing reflective footwork. */
implicit val formats = Serialization.formats(NoTypeHints)
/* Pimp anything with toJson using a reflective conversion. */
implicit def any2JValue(any: Any): AnyPimp = new AnyPimp(any)
class AnyPimp(a: Any) {
def toJson = decompose(a)
@chrislewis
chrislewis / get_gae.sh
Created August 14, 2011 18:10
Install GAE SDK on most linux dists.
#!/bin/bash
# GAE
VER="1.5.2"
SRC="http://googleappengine.googlecode.com/files/appengine-java-sdk-$VER.zip"
# Local
SDK_ROOT=/usr/local/lib
FILE=`basename $SRC`
PKG=`basename $FILE .zip`
TMP=/tmp/$FILE
@chrislewis
chrislewis / gist:1027766
Created June 15, 2011 18:34
statement-vs-expression
// It's a statement.
int bInt = 0;
if (cond)
bInt = 1;
else
bInt = 0;
// It's an expression.
val bInt = if (cond) 1 else 0