Skip to content

Instantly share code, notes, and snippets.

@dcsobral
dcsobral / gist:3353398
Created August 14, 2012 22:01
Proxy problems
dcs@shadowfax:~/github/example-sbt-project$ cat ~/bin/sbt
#!/bin/sh
if test -f ~/.sbtconfig; then
. ~/.sbtconfig
fi
#java -Xmx512M -noverify -javaagent:/home/dcs/ZeroTurnaround/JRebel/jrebel.jar -jar `dirname $0`/sbt-launch.jar "$@"
java \
-XX:+CMSClassUnloadingEnabled -XX:+UseParallelGC -Xss1m -XX:MaxPermSize=512m -Xmx1536M \
-jar `dirname $0`/sbt-launch.jar "$@"
# -Dhttp.auth.preference=ntlm -Dhttp.auth.ntlm.domain=domain \
@dcsobral
dcsobral / gist:3093046
Created July 11, 2012 20:19
Scala 2.10 -- De onde viemos e para onde vamos? Snippets de código
// Try
def percentCompleted(total: Int,
done: Int): Int =
Try ( done * 100 / total ) getOrElse 100
// Try/rescue/recover/andThen
Try {
new FileInputStream(a)
@dcsobral
dcsobral / Test.scala
Created July 2, 2012 03:59
JSON serialization
import scala.reflect.runtime.universe._
import scala.collection.GenTraversableOnce
case class Numero(n: Int, s: String)
case class Xyzzy(lst: List[Int])
case class Nested(nums: List[Numero], xyzzy: Xyzzy)
object Test extends App {
def serialize[T: TypeTag](obj: T): String = {
// a.scala
// Fri Jun 29 16:15:08 PDT 2012
import scala.reflect.makro.Context
import collection.mutable.ListBuffer
import collection.mutable.Stack
import language.experimental.macros
object Macros {
val conversionChars = "%BbCcdEefGgHhinosuXx"
@dcsobral
dcsobral / ForwardPipe.scala
Created April 16, 2012 15:51
Forward Pipe of the Future
// This needs to be put inside an object, I think, but SIP13+SIP-15 is not working right now, so I couldn't test it
implicit class ForwardPipe[T](val obj: T) extends AnyVal {
def |> [U](f : T => U) = f(obj)
}
@dcsobral
dcsobral / SBT.sublime-build
Created February 23, 2012 18:31 — forked from jboner/SBT.sublime-build
Sublime Editor SBT build setup
{
"cmd": ["sbt-no-color test"],
"file_regex": "^\\[error\\] ([.a-z_A-Z0-9/-]+[.]scala):([0-9]+):",
"selector": "source.scala",
"working_dir": "${project_path:${folder}}",
"shell": "true"
}
@dcsobral
dcsobral / ProcTrav.scala
Created February 1, 2012 18:25
Process Traversable
import scala.sys.process._
import scala.util.control.ControlThrowable
class ProcTrav(pb: ProcessBuilder) extends Traversable[String] {
var iterating: Boolean = _
var exception: Option[Throwable] = _
// This is required to stop the process, but since we are not doing that...
// var process: Process = _ // Need to synchronize access to this
def logger[U](f: String => U): String => Unit = (input: String) => if (iterating) try { f(input) } catch {
@dcsobral
dcsobral / prompt.sbt
Created January 24, 2012 17:15
Project:(git)Branch> SBT Prompt
shellPrompt <<= name(name => { state: State =>
object devnull extends ProcessLogger {
def info(s: => String) {}
def error(s: => String) { }
def buffer[T](f: => T): T = f
}
val current = """\*\s+(\w+)""".r
def gitBranches = ("git branch --no-color" lines_! devnull mkString)
"%s:%s>" format (
name,
@dcsobral
dcsobral / hashmap-bench.scala
Created January 8, 2012 01:15 — forked from erikrozendaal/hashmap-bench.scala
Scala TreeMap benchmarks
import collection.mutable.ArrayBuffer
import collection.immutable.HashMap
object HashMapTest {
val random = new util.Random(1234)
def time(block: => Unit): Double = {
val start = System.nanoTime()
block
@dcsobral
dcsobral / gist:1565038
Created January 5, 2012 12:20
Trailing commas in Scala
dcs@ayanami:~/github/GenRegex (master)$ ~/scala-2.7.7.final/bin/scala -deprecation
Welcome to Scala version 2.7.7.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_26).
Type in expressions to have them evaluated.
Type :help for more information.
scala> List(1, 2, 3,)
<console>:1: warning: Trailing commas have been deprecated
List(1, 2, 3,)
^
<console>:2: warning: Trailing commas have been deprecated