Skip to content

Instantly share code, notes, and snippets.

View Bronsa's full-sized avatar

Nicola Mometto Bronsa

View GitHub Profile
@Bronsa
Bronsa / gist:900795
Created April 3, 2011 20:48
project eulero #11
(defn sel [matrix & {:keys [x y]}]
((matrix y) x))
(defmulti select-values (fn [_ _ _ direction] direction))
(defmethod select-values :up [x y matrix _] (map #(sel matrix :x x :y (- y %)) (range 4)))
(defmethod select-values :down [x y matrix _] (map #(sel matrix :x x :y (+ y %)) (range 4)))
(defmethod select-values :left [x y matrix _] (map #(sel matrix :x (- y %) :y y) (range 4)))
(defmethod select-values :right [x y matrix _] (map #(sel matrix :x (+ y %) :y y) (range 4)))
(defn fnil [f & vals]
(fn [& args]
(apply f (loop [acc [] maybe-nils args vals vals]
(if maybe-nils
(if (nil? (first maybe-nils))
(recur (conj acc (first vals)) (next maybe-nils) (rest vals))
(recur (conj acc (first maybe-nils)) (next maybe-nils) vals))
acc)))))
(defmacro gen-vec [arity]
(let [Vec (gensym "Vec")
args (vec (for [i (range arity)] (gensym)))
ctor (symbol (str "->" Vec))
f (fn [f field] `(~f ~field (. ~'b ~field)))]
`(with-tags {u# ~Vec v# ~Vec ~'b ~Vec}
(do
(deftype ~Vec ~args
clojure.lang.IFn
(defn- emit-protocol [name opts+sigs]
(let [iname (symbol (str (munge (namespace-munge *ns*)) "." (munge name)))
[opts sigs]
(loop [opts {:on (list 'quote iname) :on-interface iname} sigs opts+sigs]
(condp #(%1 %2) (first sigs)
string? (recur (assoc opts :doc (first sigs)) (next sigs))
keyword? (recur (assoc opts (first sigs) (second sigs)) (nnext sigs))
[opts sigs]))
sigs (if-not (empty? sigs)
(reduce1 (fn [m s]
(deftype Atom [^AtomicReference state
^clojure.lang.IPersistentMap ^:volatile-mutable meta
^clojure.lang.IFn ^:volatile-mutable validator
^clojure.lang.IPersistentMap ^:volatile-mutable watches]
IDeref
(-deref [_] (.get state))
IEquiv
(-equiv [this other] (identical? this other))
(deftype Action [agent fn args solo?]
Runnable
(run [this]
(try
(.set nested [])
(let [error (atom nil)]
(try (let [old-value (-deref agent)
new-value (apply fn (-deref agent) args)]
(.setState agent new-value)
--- core_deftype.clj 2012-03-25 00:55:20.449487716 +0100
+++ core_deftype.clj.new 2012-03-25 00:54:39.281283567 +0100
@@ -45,6 +45,8 @@
(defn- parse-opts+specs [opts+specs]
(let [[opts specs] (parse-opts opts+specs)
impls (parse-impls specs)
+ [opts impls] [(dissoc opts :mixin)
+ (merge-with concat impls (eval (:mixin opts)))]
interfaces (-> (map #(if (var? (resolve %))
(:on (deref (resolve %)))
(ns brochure.lang
(:require [brochure.lang.protocols :refer :all]
[brochure.lang.implementations :refer :all]
[brochure.lang.utils :refer :all]
[brochure.lang.PersistentList :refer :all])
(:import java.util.concurrent.atomic.AtomicReference))
(deftype Atom [^AtomicReference state
^:unsynchronized-mutable meta
^:volatile-mutable validator
(defprotocol PushbackReader
(read-char [reader] "Returns the next char from the Reader, nil if the end of stream has been reached")
(unread [reader ch] "Push back a single character on to the stream"))
(defmacro update! [what f]
(list 'set! what (list f what)))
;;not thread-safe, but i guess i don't care?
(deftype StringPushbackReader
[^:unsynchronized-mutable ^String s ^"[C" buf ^:unsynchronized-mutable len ^:unsynchronized-mutable buf?]
(defprotocol PushbackReader
(read-char [reader] "Returns the next char from the Reader, nil if the end of stream has been reached")
(unread [reader ch] "Push back a single character on to the stream"))
(defmacro update! [what f]
(list 'set! what (list f what)))
;;not thread-safe, but i guess i don't care?
(deftype StringPushbackReader
[^:unsynchronized-mutable ^String s ^"[C" buf ^:unsynchronized-mutable len ^:unsynchronized-mutable buf?]