Skip to content

Instantly share code, notes, and snippets.

@bmabey
bmabey / bwmorph.py
Created May 24, 2018 15:58
Python implementation of matlab's `bwmorph` function for the operations:`thin`, `spur`, `bracnhes`, and `endpoints`
"""
Functions that implement some of the same functionality found in Matlab's bwmorph.
`thin` - was taken and adapted from https://gist.github.com/joefutrelle/562f25bbcf20691217b8
`spur` - Not perfect but pretty close to what matlab does via LUTs
`endpoints` - lines up perfectly with matlab's output (in my limited testing)
`branches` - this results in more clustered pixels than matlab's version but it pretty close
"""
import numpy as np
import scipy.ndimage as ndi
@bmabey
bmabey / default_maps.clj
Last active February 14, 2023 18:02
Maps with default values in Clojure
(ns default-maps
"Maps with default values, similar to how you can specify default values and blocks to Ruby's Hash.
Examples:
user> (def b (assoc-default {:foo 34 :bar 34} (fn [_ k] (str k))))
#'user/b
user> (get b :foo-bar)
\":foo-bar\"
user> (def m (assoc-default {:foo 34 :bar 34} 343))
(ns monte-carlo-solitaire.card
(use [clojure.contrib.str-utils :only [re-split]]))
(defn- inverse-map
"Inverses map m so that the vals become the keys, and the keys become vals."
[m]
(zipmap (vals m) (keys m)))
(def suits [:hearts :diamonds :clubs :spades])
@bmabey
bmabey / memoize_fn.clj
Created August 10, 2011 05:00
memoized anonymous functions in clojure
; inspired from http://stackoverflow.com/questions/3906831/how-do-i-generate-memoized-recursive-functions-in-clojure
(defmacro memoize-fn
"Produces a memoized anonymous function that can recursively call itself."
[fn-name & fn-args]
`(with-local-vars
[~fn-name (memoize
(fn ~@fn-args))]
(.bindRoot ~fn-name @~fn-name)
@~fn-name))
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@bmabey
bmabey / ex1_4.scm
Created September 19, 2009 21:43 — forked from redsquirrel/gist:189193
SICP Exercise 1.4
;; SICP 1.4
;; Exercise 1.4. Observe that our model of evaluation allows for
;; combinations whose operators are compound expressions. Use this
;; observation to describe the behavior of the following procedure:
(define (a-plus-abs-b a b)
((if (> b 0) + -) a b))
brew 'ack'
brew 'ag'
brew 'coreutils'
brew 'bash-completion'
brew 'openssl'
brew 'readline'
brew 'ispell'
brew 'wget'
brew 'tree'
brew 'autoenv'
@bmabey
bmabey / ex1_7.scm
Created September 23, 2009 02:32 — forked from jimweirich/gist:189261
SICP Exercise 1.7
;; Exercise 1.7.
;; The good-enough? test used in computing square roots will not be very effective for
;; finding the square roots of very small numbers. Also, in real computers, arithmetic operations are almost
;; always performed with limited precision. This makes our test inadequate for very large numbers. Explain
;; these statements, with examples showing how the test fails for small and large numbers. An alternative
;; strategy for implementing good-enough? is to watch how guess changes from one iteration to the
;; next and to stop when the change is a very small fraction of the guess. Design a square-root procedure
;; that uses this kind of end test. Does this work better for small and large numbers?
; The problem with the good-enough? procedure for small numbers is that there radicant is much smaller than
@bmabey
bmabey / pi.clj
Created August 11, 2011 17:37 — forked from davidbody/pi.clj
Calculate Pi in Clojure
(defn calculate-pi
"Calculates Pi using the approximation 4 * (1 - 1/3 + 1/5 - 1/7 + ...)"
[iterations]
(let [odd-numbers (filter odd? (iterate inc 1))]
(* 4.0
(apply + (map / (cycle [1 -1]) (take iterations odd-numbers))))))
(println "calculated pi =" (calculate-pi 100000))
(println "Math/PI =" Math/PI)
@bmabey
bmabey / prometheus-text004.instaparse
Last active March 4, 2018 05:19
instaparse parser for prometheus text format exporter (text version 0.0.4)
(* see https://github.com/prometheus/docs/blob/master/content/docs/instrumenting/exposition_formats.md#text-format-details *)
statements = (type-def | help | metric)*
type-def = <"# TYPE "> metric-name <space> type <newline>?
type = #"counter|gauge|histogram|summary|untyped"
help = <"# HELP "> metric-name <space> docstring <newline>?
metric = metric-name labels? <space> value (<space> timestamp)? <newline>?
labels = <'{'> label-name label-value {<','> label-name label-value} <','>? <'}'>
(* the below doesn't take into account escaping but probably not an issue for our use case *)