Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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)
}
// 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 / 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 = {
@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 / 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 / repositories
Created August 20, 2012 17:48
SBT repositories
[repositories]
local
my-maven-repo: http://artifactory.ebc/artifactory/repo/
my-ivy-artifacts: http://artifactory.ebc/artifactory/repo/, [organization]/[module]/[revision]/[type]s/[type].[ext], [organization]/[module]/[revision]/[type]s/[module].[ext]
my-ivy-classifiers: http://artifactory.ebc/artifactory/repo/, [organization]/[module]/[revision]/[type]s/[type].[ext], [organization]/[module]/[revision]/[type]s/[module]-[classifier].[ext]
@dcsobral
dcsobral / ListZipper.scala
Created August 30, 2012 23:18 — forked from tonymorris/ListZipper.scala
List Zipper
case class ListZipper[+A](lefts: List[A], x: A, rights: List[A]) {
def map[B](f: A => B): ListZipper[B] =
sys.error("todo")
// map with zipper context
def coFlatMap[B](f: ListZipper[A] => B): ListZipper[B] =
sys.error("todo")
def findRight(p: A => Boolean): Option[ListZipper[A]] =
sys.error("todo")