Skip to content

Instantly share code, notes, and snippets.

@michalmarczyk
michalmarczyk / validate_under.clj
Last active February 29, 2016 17:09
validateur nesting
(defn validate-under [attr validator]
(fn [m]
(let [result (validator (get m attr))]
[(empty? result) (v/nest attr result)])))
(comment
(def example-v
(v/validation-set
(validate-under :foo (v/validation-set (v/presence-of :bar)))))
@michalmarczyk
michalmarczyk / thread_or.clj
Created July 14, 2013 01:09
Short-circuiting logical disjunction of several futures' results
(defn thread-or
"Call each of the fs on a separate thread. Return logical
disjunction of the results. Short-circuit (and cancel the calls to
remaining fs) on first truthy value returned."
[& fs]
(let [ret (promise)
fps (promise)]
(deliver fps
(doall (for [f fs]
(let [p (promise)]
@michalmarczyk
michalmarczyk / thread_and.clj
Last active December 19, 2015 17:28
Short-circuiting logical conjunction of several futures' values
(defn thread-and
"Computes logical conjunction of return values of fs, each of which
is called in a future. Short-circuits (cancelling the remaining
futures) on first falsey value."
[& fs]
(let [done (promise)
ret (atom true)
fps (promise)]
(deliver fps (doall (for [f fs]
(let [p (promise)]
@michalmarczyk
michalmarczyk / thread_or.clj
Created July 12, 2013 22:19
Short-circuiting logical disjunction of several futures' results in core.async
(defn thread-or
"Call each of the fs on a separate thread. Return logical
disjunction of the results. Short-circuit (and cancel the calls to
remaining fs) on first truthy value returned."
[& fs]
(let [futs-and-cs
(doall (for [f fs]
(let [c (chan)]
[(future (>!! c (f))) c])))]
(loop [futs-and-cs futs-and-cs]
@michalmarczyk
michalmarczyk / thread_and.clj
Created July 12, 2013 21:50
Short-circuiting logical conjunction of several futures' results in core.async
(defn thread-and
"Call each of the fs on a separate thread. Return logical
conjunction of the results. Short-circuit (and cancel the calls
to remaining fs) on first falsey value returned."
[& fs]
(let [futs-and-cs
(doall (for [f fs]
(let [c (chan)]
[(future (>!! c (f))) c])))]
(loop [futs-and-cs futs-and-cs]
@michalmarczyk
michalmarczyk / producer_consumer.clj
Created July 12, 2013 01:25
Single consumer, multiple producers in core.async
(use 'clojure.core.async)
(def output (atom []))
(defn producer [ctrl k]
(go (loop [i 0]
(when-let [c (<! ctrl)]
(>! c [k i])
(>! ctrl c)
(recur (inc i))))))
;; [1 2 3] -> [1 [2 [3]]]
(defn f [xs]
(if (next xs)
[(first xs) (vec (f (next xs)))]
(vec xs)))
(defn g [v]
(reduce (fn [acc x]
[x acc])
@michalmarczyk
michalmarczyk / same_fringe.clj
Last active December 19, 2015 10:09
Same Fringe with core.async
(defn same-fringe
"http://c2.com/cgi/wiki?SameFringeProblem"
[t1 t2]
(letfn [(walk [t c]
(go (if (seq? t)
(doseq [t' t]
(<! (walk t' c)))
(>! c t))))]
(let [c1 (chan)
c2 (chan)]

Keybase proof

I hereby claim:

  • I am michalmarczyk on github.
  • I am michalmarczyk (https://keybase.io/michalmarczyk) on keybase.
  • I have a public key ASDT-njSq0f89ZYOOhDYbdSb5MZkLq6LD_veaTD7vzegmQo

To claim this, I am signing this object:

@michalmarczyk
michalmarczyk / timings.cljs
Created May 4, 2012 23:53
Benchmarks for CLJS protocol dispatch
(ns timings)
(defn -main []
(println ";;; satisfies?")
(println "(satisfies? ISeq (list 1 2 3))")
(let [coll (list 1 2 3)] (time (dotimes [_ 10000000] (satisfies? ISeq coll))))
(println "(satisfies? ISeq [1 2 3])")
(let [coll [1 2 3]] (time (dotimes [_ 10000000] (satisfies? ISeq coll))))
(println)