Skip to content

Instantly share code, notes, and snippets.

View smee's full-sized avatar

Steffen Dienst smee

View GitHub Profile
(defn js->clj
"Recursively transforms JavaScript arrays into ClojureScript
vectors, and JavaScript objects into ClojureScript maps. With
option ':keywordize-keys true' will convert object fields from
strings to keywords."
([x] (js->clj x :keywordize-keys false))
([x & opts]
(let [{:keys [keywordize-keys]} opts
keyfn (if keywordize-keys keyword str)
f (fn thisfn [x]
@smee
smee / multicompare.clj
Last active February 5, 2018 14:43
inital draft of a multi comparator, bug in the macro expansion
(ns multicompare)
;;; FIXME the ~key-fn expansion does not resolve all the symbols in `key-fn`, so advanced compilation in clojurescript fails!
(defn- multi-compare* [a b [[key-fn order] & key-fn-order-pairs]]
(let [c (gensym "compare-result-")
k (gensym "key-fn-")
compare-clause `(compare ~@(if (#{:asc :ASC :ascending :ASCENDING} order)
`[(~k ~a) (~k ~b)]
`[(~k ~b) (~k ~a)]))]
(if (nil? key-fn-order-pairs)
(ns diamond-square
(:require [clojure.string :as str]))
(defn average-of-coords
"Calculate the average of a number of cells in a 2D doubles array.
Wraps around if coordinates are out of bounds."
[^"[[D" arr coords]
(let [|arr| (dec (alength arr))
;; use modulo to make the grid tiling
coords (map (fn [[i j]] [(mod i |arr|) (mod j |arr|)]) coords)]
@smee
smee / keybase.md
Created August 15, 2014 14:55
keybase.md

Keybase proof

I hereby claim:

  • I am smee on github.
  • I am steffen (https://keybase.io/steffen) on keybase.
  • I have a public key whose fingerprint is 27AC ABAE F19D 5432 556D D2F2 6A09 5F97 851C 9D55

To claim this, I am signing this object:

@smee
smee / _.md
Created March 4, 2014 11:28
color scale tests - 2D
@smee
smee / problems wip.clj
Created November 7, 2011 18:37
problem 79 temp
(fn [coll] (reduce (fn [l c] (map + c (map (partial min) (partition 2 1 l)))) (reverse coll)))
problem 132 (oome)
(fn [pred sep coll]
(flatten
(for [[a b] (partition 2 1 (concat coll [(last coll)]))]
(if (pred a b) [a sep] a))))
@smee
smee / wheel.clj
Created May 6, 2011 15:35
Wheel of fortune for arbitrary wheel slot sizes
(defn wheel-of-fortune [weights]
(let [sum (reduce + weights)
scaled (cons 0 (reductions + (map #(/ % sum) weights)))
intervals (indexed (partition 2 1 scaled))
in-interval? (fn [[l r] val] (and (<= l val) (>= r val)))]
(fn [] (let [rnd-val (rand 1)]
(some #(when (in-interval? (second %) rnd-val) (first %)) intervals)))
))
import java.util.Collections;
import org.apache.wicket.ResourceReference;
public class MoveDownButton extends EditorButton {
public MoveDownButton(final String id) {
super(id, new ResourceReference(MoveDownButton.class, "images/down.png"));
}