Skip to content

Instantly share code, notes, and snippets.

View egamble's full-sized avatar

Evan Gamble egamble

  • freiheit.com
  • Hamburg, Germany
View GitHub Profile
@egamble
egamble / reverse.py
Created March 26, 2015 23:33
Reverse a singly-linked list.
class List:
def __init__(self, data, next):
self.data = data
self.next = next
def __repr__(self):
return 'List({0.data!r}, {0.next!r})'.format(self)
def str_helper(self):
if self.next == None:
return '{0.data!s}'.format(self)
else:
@egamble
egamble / private.clj
Last active May 16, 2023 12:41
Two ways to call private methods in Clojure.
;; This fn allows calling any method, as long as it's the first with that name in getDeclaredMethods().
;; Works even when the arguments are primitive types.
(defn call-method
[obj method-name & args]
(let [m (first (filter (fn [x] (.. x getName (equals method-name)))
(.. obj getClass getDeclaredMethods)))]
(. m (setAccessible true))
(. m (invoke obj (into-array Object args)))))
@egamble
egamble / index-except.clj
Last active December 29, 2015 14:39
Examples of recursion, reduce, etc. for Clojure class.
(ns cljclass)
;; Various ways to solve the problem of transforming a collection to a seq or vector of indices starting at zero, but
;; using an index of -1 for items for which (pred item) is true.
;; E.g. (index-except odd? [1 4 18 7 9 2 4 3]) => [-1 0 1 -1 -1 2 3 -1]
;; NB:
;; This is not the same as assigning indices and replacing the elements for which pred is true with -1.
;; E.g. (index-except odd? [1 2 3]) => [-1 0 -1] and not [-1 1 -1].
@egamble
egamble / smoother.py
Last active December 27, 2015 08:49
Modified version of the MCEdit smoothing filter https://github.com/mcedit/mcedit/blob/master/filters/smooth.py. This version smooths under water correctly, uses the surrounding terrain blocks for fill, and both raises and lowers terrain correctly.
from numpy import zeros, array
import itertools
from pymclevel.level import extractHeights
terrainBlocktypes = [1, 2, 3, 7, 12, 13, 14, 15, 16, 24, 56, 73, 74, 82, 87, 88, 89]
terrainBlockmask = zeros((256,), dtype='bool')
terrainBlockmask[terrainBlocktypes] = True
inputs = (
("Repeat count", (1, 50)),
@egamble
egamble / macroexpand-n.clj
Created December 8, 2011 22:46
Recursively macroexpand to depth n
(defn macroexpand-n [n form]
(if (zero? n)
form
(recur (dec n)
(macroexpand-1 form))))