Skip to content

Instantly share code, notes, and snippets.

(def v (into [] (repeatedly 1e5 #(rand 100))))
(defn rnd [x]
(Math/round x))
(defn prnd [x]
(let [^double x x]
(Math/round x)))
(time (r/fold + (r/map rnd v)))
(defn longest-palindrome [s]
(let [palindrome? #(= (seq %) (reverse %))
slice-all (fn [s] (mapcat #(partition % 1 s)
(map first
(take-while seq
(iterate rest
(range (count s) 1 -1))))))]
(first (filter palindrome? (slice-all s)))))
@amalloy
amalloy / calculate_change.clj
Created August 26, 2012 00:24 — forked from actsasbuffoon/calculate_change.clj
My first attempt at Clojure. Suggestions/criticism welcome.
(let [currency-values [[:hundreds 10000]
[:fifties 5000]
[:twenties 2000]
[:tens 1000]
[:fives 500]
[:ones 100]
[:quarters 25]
[:dimes 10]
[:nickels 5]
[:pennies 1]]]
(macro-do [name]
(let [{:keys [varname meta]} (graph-impl name)]
`(defn ~name ~(:doc meta)
(~'[head-id tail-id]
(apply ~name :over-layer ~'[head-id tail-id]))
(~'[layer-name head-id tail-id]
(apply ~varname (layer ~'layer-name) ~'[head-id tail-id]))))
merge-node! unmerge-node!)
(macro-do [name]
(let [{:keys [varname meta]} (graph-impl name)]
`(defn ~name ~(:doc meta)
(~'[head-id tail-id]
(apply ~name :over-layer ~'[head-id tail-id]))
(~'[layer-name head-id tail-id]
(apply ~varname (layer ~'layer-name) ~'[head-id tail-id]))))
merge-node! unmerge-node!)
@amalloy
amalloy / gist:1996359
Created March 7, 2012 21:29 — forked from dgrnbrg/gist:1996228
Rate limit clojure fn
(defn rate-limit [interval s]
(let [cache (atom {})]
(for [x s
:let [t (System/currentTimeMillis)]
:when (< (+ (get @cache x 0) interval) t)]
(do (swap! cache assoc x t)
x))))
@amalloy
amalloy / Mist proposal.md
Created February 25, 2012 22:46 — forked from jamii/Mist.md
Mist: an open-source clojure library for p2p NAT traversal, dynamic addressing and pubsub

About

Building decentralised services is an exercise in accidental complexity. Connectivity is the first hurdle. Users move around, go on- and off-line and lurk behind NAT. For centralised systems we have ICE. For decentralised systems there is no standard solution.

Mist will take an abstract address representing a user, locate their machine, punch a hole and return a local UDP proxy to talk to that user. It will be cross-platform, provide a simple api and be easy to use with existing UDP programs.

As a bonus, the implementation also provides a p2p pubsub system.

The design is based heavily on Jeremy Miller's telehash protocol (although simplified and streamlined) and draws ideas from libswift. It is also based on lessons learned from my telehash implementation.

@amalloy
amalloy / gist:1590781
Created January 10, 2012 19:47 — forked from anonymous/gist:1590658
ugly macro
(defmacro defcontroller [name & actions]
(let [iname (symbol (str "I" name))]
`(do
(defprotocol ~iname
~@(for [[m as & fs] actions]
`(~m [~@as])))
(deftype ~name []
~iname
~@(for [[m as & fs] actions]
`(~(with-meta m {:tag "test"}) [~'this ~@as] ~@fs))))))
@amalloy
amalloy / gist:1590779
Created January 10, 2012 19:47 — forked from anonymous/gist:1590658
ugly macro
(defmacro defcontroller [name & actions]
(let [iname (symbol (str "I" (str name)))
methods (for [[m as & fs] actions] `(~m ~(vec (cons 'this as)) ~@fs))
sigs (for [[m as & fs] methods] `(~m ~(vec as)))]
`(do
(defprotocol ~iname ~@sigs)
(deftype
~'name
[]
~iname
;; fixed indenting
(fn my-count [coll]
(let [my-count-plus (fn my-count-plus [coll accumulator]
(let [r (rest coll)]
(cond
(= '() coll) accumulator
:else (my-count-plus r (+ 1 accumulator)))))]
(my-count-plus coll 0)))