Skip to content

Instantly share code, notes, and snippets.

View mwmitchell's full-sized avatar

Matt Mitchell mwmitchell

View GitHub Profile
(def s {:P1 {:queen [{:rate [1.0 0.0]}]
:king [{:rate [1.0 0.0] :strate [1.4 0.0]}]}
:P2 {:double []
:king [{:rate [1.0 0.0] :strate [1.4 0.0]} {:rate [1.0 0.0]}]}
:P3 {:double []}})
(into {} (for [[pc rooms] s
[rt rates] rooms
r rates
:let [st (:strate r)]
@mwmitchell
mwmitchell / gist:5479064
Created April 29, 2013 00:44
mapping-macros
(defmacro field+ [n fields & forms]
(let [f (mapv keyword fields)
args {:keys fields :as '$doc}]
`(hash-map ~n {:fields ~f
:fn (let [~'$fields ~f]
(fn [~args] ~@forms))})))
(defmacro defmodel [name & forms]
`(def ~name (merge {} ~@forms)))
(:import [java.util UUID])
(defn uuid->long [uuid]
(if (string? uuid)
(try (when-let [x (UUID/fromString uuid)] (uuid->long x))
(catch Exception e nil))
(bit-xor (.getLeastSignificantBits uuid) (.getMostSignificantBits uuid))))
(defn redefine [binding-map func]
(let [old-vals (zipmap (keys binding-map)
(map #(.getRawRoot ^clojure.lang.Var %) (keys binding-map)))
hits (zipmap (keys binding-map)
(map (fn [_] (atom 0)) (keys binding-map)))
wrappers (zipmap (keys binding-map)
(map #(fn [& args]
(let [[f limit] (get binding-map %)
h (get hits %)
old (get old-vals %)]
@mwmitchell
mwmitchell / validate.clj
Created December 14, 2012 16:22
clojure validation and transform function
;; Validation and transformation handling of input maps.
;; Provides input validation, transform, and output validation capabilities
;; Allows input values to be marked as required or not by setting required error message
(letfn [(check! [value fun-msg-pairs]
(when (seq fun-msg-pairs)
(doall (for [[f m] (partition 2 fun-msg-pairs)]
(when-not (f value) (throw (Exception. m)))))))]
@mwmitchell
mwmitchell / getgeo.sh
Created October 5, 2012 18:17 — forked from scottynomad/getgeo.sh
Script to import Geonames into PostgreSQL taken from http://forum.geonames.org/gforum/posts/list/15/926.page
#!/bin/bash
#===============================================================================
#
# FILE: getgeo.sh
#
# USAGE: ./getgeo.sh
#
# DESCRIPTION: run the script so that the geodata will be downloaded and inserted into your
# database
#
;; a function that provides the ability to filter a collection using multiple predicate functions, *lazily*.
;; The predicate functions must accept 2 args:
;; - the first arg is the accumulating collection (think "reduce").
;; - the second arg is the current item in the collection.
;; The return value is a reduced version of the input
;;
(defn filters [res items & fns]
(last (reductions
(fn [r i]
(if (every? #(% r i) fns) (conj r i) r))
Could not decrypt credentials from /Users/mmitchell/.lein/credentials.clj.gpg
gpg: can't query passphrase in batch mode
gpg: decryption failed: secret key not available
@mwmitchell
mwmitchell / output.txt
Created August 14, 2012 20:59
gpg lein credentials
foobar:apij mmitchell$ gpg --version
gpg (GnuPG) 1.4.12
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
foobar:apij mmitchell$ gpg --default-recipient-self -e ~/.lein/credentials.clj
You did not specify a user ID. (you may use "-r")
;; [org.clojure/math.combinatorics "0.0.3"]
(ns wordhash
(:require [clojure.math.combinatorics :as comb]
[clojure.string :as string]))
(defn words->code [words n]
(apply str
(take n (keep identity
(map (fn [[x y]]