Skip to content

Instantly share code, notes, and snippets.

View bmc's full-sized avatar

Brian Clapper bmc

View GitHub Profile
@bmc
bmc / scalatest-sbt-fragment.scala
Created May 17, 2011 13:41
Supporting ScalaTest with Scala 2.8 and 2.9 in SBT
val (scalatestArtifact, scalatestVersion) = buildScalaVersion match
{
case "2.7.7" => ("scalatest", "1.2")
case "2.8.0" => ("scalatest", "1.3")
case "2.8.1" => ("scalatest", "1.3")
case "2.9.0" => ("scalatest_2.9.0", "1.4.1")
case n => error("Unsupported Scala version " + n)
}
val scalatest = "org.scalatest" % scalatestArtifact % scalatestVersion % "test"
@bmc
bmc / doofus.rb
Created June 30, 2011 20:27
Sample, simple Sinatra + JSON thingie
require 'rubygems'
require 'sinatra'
require 'json'
get '/', :provides => 'json' do
data = {"key1" => ["a", "b"],
"key2" => 10}
JSON::dump(data)
end
@bmc
bmc / testwrapmany.rb
Created August 2, 2011 15:11
Wrapping multiple objects in Ruby
class Foo
attr_reader :a, :b
def initialize
@a = 'a-Foo'
@b = 'b-Foo'
end
end
class Bar
attr_reader :a, :c
@bmc
bmc / yuck.scala
Created August 18, 2011 20:46
Ugly, but damned useful, structural type example
class ClassDumper
{
type Meth =
{
def getParameterTypes: Array[Class[_]]
def getName: String
}
def methodToString(m: Meth): String =
{
@bmc
bmc / scedit.el
Created August 30, 2011 15:11
Making my Scaladoc tombstones conform
; I've been trying to convert my Scaladoc comments from Java style to Scala style.
; i.e., from:
; /**
; * This amazing method washes your hair, trims your beard, and
; * and brushes your teeth.
; */
;
; to:
;
; /** This amazing method washes your hair, trims your beard, and
@bmc
bmc / RiddleMeThis.scala
Created September 2, 2011 15:08
Scala compiler oddity #1
// So, given this declaration:
import java.io.File
val scalaDirs = new File("target").listFiles.filter(_.getName.startsWith("scala"))
// Why does this work:
scalaDirs.take(1).map(new File(_, "classes")).toList(0)
// but this fails to compile?
@bmc
bmc / boot.rb
Created October 28, 2011 01:35
Rails 3 local configuration
# Load a config-local.yml configuration file, which has special
# configuration directives per environment.
require 'rails'
CONFIG_LOCAL = 'config-local.yml'
OPTIONS = {
:listen_port => 3000,
# Default email addresses.
@bmc
bmc / stack.sh
Created October 28, 2011 21:04
A stack implementation, in bash
# A stack, using bash arrays.
# ---------------------------------------------------------------------------
# Create a new stack.
#
# Usage: stack_new name
#
# Example: stack_new x
function stack_new
{
@bmc
bmc / uuid.coffee
Created February 23, 2012 16:03
Generate UUID, using Math.random(), in CoffeeScript
# RFC1422-compliant Javascript UUID function. Generates a UUID from a random
# number (which means it might not be entirely unique, though it should be
# good enough for many uses). See http://stackoverflow.com/questions/105034
uuid = ->
'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) ->
r = Math.random() * 16 | 0
v = if c is 'x' then r else (r & 0x3|0x8)
v.toString(16)
)
@bmc
bmc / ThinkingAboutThisApproach.scala
Created March 15, 2012 22:41
Argot: New DSL possibility
package ThinkingAboutThisApproach {
object Types {
type Converter[T] = (String, ArgSpec) => Either[String, T]
}
import Types._
class ArgOption[T](val name: String, val desc: String, val cvt: Converter[T]) {
override def toString = "ArgOption<%s>" format name