Skip to content

Instantly share code, notes, and snippets.

View bmc's full-sized avatar

Brian Clapper bmc

View GitHub Profile
@bmc
bmc / mixin.py
Created April 4, 2012 17:03
Yet another way to do mixins in Python
import inspect
def mixin(cls, *funcs):
"""
Mix the specified class into the current class. If function names (as
strings) are passed, then only those functions are mixed in.
mixin() MUST be called within a class definition.
Example:
@bmc
bmc / Build.scala
Created March 27, 2012 20:05
sbt-lwm in Build.scala
import sbt._
import Keys._
import org.clapper.sbt.lwm.LWM._
object TestBuild extends Build {
override lazy val projects = Seq(root)
lazy val root = Project(
"root", file(".")
).settings (LWM.settings : _*).settings(
LWM.targetDirectory in LWM.Config <<= baseDirectory(_ / "target2")
@bmc
bmc / tostring.coffee
Created March 20, 2012 15:24
This should be standard in CoffeeScript and Javascript
# Get a printable version of all properties in an object. Ignores function
# properties. DO NOT MAKE THIS A PROPERTY OF Object!
window.objectToString = (obj) ->
t = typeof obj
if (t is "number") or (t is "boolean")
"#{obj}"
else if window.isArray(obj)
window._arrayToString(obj)
else if t is "string"
'"' + obj + '"'
@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
@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 / 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 / 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 / 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 / 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 / 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 =
{