Skip to content

Instantly share code, notes, and snippets.

@matthandlersux
matthandlersux / tree.markdown
Created March 10, 2012 18:39 — forked from hrldcpr/tree.md
one-line tree datastructure in python

in python:

from pprint import pprint
from collections import defaultdict


# one-line definition of a tree:
def tree(): return defaultdict(tree)
@matthandlersux
matthandlersux / PrivateMethodCaller.scala
Created September 15, 2012 12:37 — forked from jorgeortiz85/PrivateMethodCaller.scala
Calling private methods in Scala
// Usage:
// p(instance)('privateMethod)(arg1, arg2, arg3)
class PrivateMethodCaller(x: AnyRef, methodName: String) {
def apply(_args: Any*): Any = {
val args = _args.map(_.asInstanceOf[AnyRef])
def _parents: Stream[Class[_]] = Stream(x.getClass) #::: _parents.map(_.getSuperclass)
val parents = _parents.takeWhile(_ != null).toList
val methods = parents.flatMap(_.getDeclaredMethods)
val method = methods.find(_.getName == methodName).getOrElse(throw new IllegalArgumentException("Method " + methodName + " not found"))