Skip to content

Instantly share code, notes, and snippets.

View mwmitchell's full-sized avatar

Matt Mitchell mwmitchell

View GitHub Profile
java.lang.RuntimeException: java.lang.ClassCastException: clojure.lang.Keyword cannot be cast to java.lang.String
AFunction.java:39 clojure.lang.AFunction.compare
Arrays.java:1270 java.util.Arrays.mergeSort
Arrays.java:1210 java.util.Arrays.sort
core.clj:2339 clojure.core/sort
core.clj:2335 clojure.core/sort
debugging.clj:62 apij.debugging/report-log-status
debugging.clj:98 apij.debugging/show-logging
response.clj:29 compojure.response/eval5394[fn]
response.clj:7 compojure.response/eval5363[fn]
(ns http-agent
(:import
(java.net URL
HttpURLConnection)
(java.io InputStreamReader
BufferedReader
InputStream)))
(defn GET
[{:keys [url on-success on-fail]}]
(let [res (atom nil)
response-future (medusa/medusa-future
;; logging the result of client/post shows the correct value
(swap! res (fn [_]
(client/post config/rtd-url
{:headers default-headers
:body xml })))]
;; this yields nil
(.get response-future 3000 java.util.concurrent.TimeUnit/MILLISECONDS)
@mwmitchell
mwmitchell / list_filtering.clj
Created March 27, 2012 15:37
list filtering
(let [candidates [{:udicode 1} {:udicode 2} {:udicode 3} {:udicode 4} {:udicode 1} {:udicode 9} {:udicode 20}]
result (atom #{})
filters [#(not (@result %))
#(< (:udicode %) 10)]
include? (fn [h]
(if (every? #(% h) filters)
(swap! result conj h)))]
(take 7 (filter include? candidates)))
;; using juxt and reduce for applying the filters:
@mwmitchell
mwmitchell / record+.clj
Created March 24, 2012 21:07
defrecord+
;; Provides the ability to use :pre and :or options to defrecord via a constructor function.
;; The constructor function is named new-X, where X is the name passed into (defrecord+ X ...)
(defmacro defrecord+
[name fields & etc]
(let [[[{:as opts}] etc]
(if (map? (first etc))
(split-at 1 etc)
[nil etc])
@mwmitchell
mwmitchell / l_split_with.clj
Created March 16, 2012 12:44
l-split-with (lazy)
(defn l-split-with [i pred]
(lazy-seq (loop [in i out []]
(if-let [f (first in)]
(if (pred f)
(list out in)
(recur (rest in) (conj out f)))))))
(l-split-with [1 2 3 4 :true true] keyword?)
; =>
'([1 2 3 4] (:true true))
@mwmitchell
mwmitchell / divisions.clj
Created February 9, 2012 15:42
divisions
(defn divisions [n t]
(loop [nn n nt t z []]
(let [q (quot nn nt) nz (concat z [q])]
(if (> nt 1)
(recur (- nn q) (dec nt) nz)
nz))))
(divisions 10 8)
@mwmitchell
mwmitchell / division_expansion_2.clj
Created February 9, 2012 02:07
division expansion 2
(defn de2 [x n]
(concat
(map (constantly (int (quot x n))) (range (- n 1)))
[(int (java.lang.Math/ceil (/ x n)))]))
(de2 7 2)
(de2 10 2)
(de2 10 5)
@mwmitchell
mwmitchell / division_expansion.clj
Created February 9, 2012 01:48
division expansion
(defn de [x n]
(loop [x x nr (vec (range 1 (+ n 1))) z []]
(let [q (quot x (last nr))]
(if-let [xx (not-empty (subvec nr 0 (- (count nr) 1)))]
(recur (- x q) xx (concat z [q]))
(concat z [q])))))
(de 8 2)
(de 7 3)
(de 7 2)
(facts "/locations/:id/rates should limit the rate response count by the :limit param"
(let [limit 3
params [:check_in (gen-check-in)
:check_out (gen-check-out)
:guests 1
:rooms 1
:limit limit]]
(apply location-rate-request (:tid location-one) params) => truthy
(provided
(apij.models.hotel-search/search