Skip to content

Instantly share code, notes, and snippets.

Michał Marczyk michalmarczyk

Block or report user

Report or block michalmarczyk

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@michalmarczyk
michalmarczyk / thread_and.clj
Last active Dec 19, 2015
Short-circuiting logical conjunction of several futures' values
View thread_and.clj
(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 Jul 12, 2013
Short-circuiting logical disjunction of several futures' results in core.async
View thread_or.clj
(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 Jul 12, 2013
Short-circuiting logical conjunction of several futures' results in core.async
View thread_and.clj
(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 Jul 12, 2013
Single consumer, multiple producers in core.async
View producer_consumer.clj
(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))))))
View nest.clj
;; [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 Dec 19, 2015
Same Fringe with core.async
View same_fringe.clj
(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)]
View keybase.md

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
Benchmarks for CLJS protocol dispatch
View timings.cljs
(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)
@michalmarczyk
michalmarczyk / merge-sorted.clj
Created May 1, 2012
Multiway merge of sorted sequences
View merge-sorted.clj
(defn merge-sorted [comparator & xss]
(let [xss (into-array Object (remove empty? xss))
pq (java.util.PriorityQueue.
(count xss) #(comparator (val %1) (val %2)))]
(dotimes [i (count xss)]
(let [xs (aget xss i)]
(.add pq (pair i (first xs)))
(aset xss i (next xs))))
((fn go []
(lazy-seq
@michalmarczyk
michalmarczyk / phm.js
Created Apr 30, 2012
ClojureScript perf tests (large map conversion threshold)
View phm.js
function c(a){throw a;}var g=!0,k=null,l=!1;function aa(){return function(a){return a}}function m(a){return function(){return this[a]}}function n(a){return function(){return a}}var o,ba=this;
function q(a){var b=typeof a;if("object"==b)if(a){if(a instanceof Array)return"array";if(a instanceof Object)return b;var d=Object.prototype.toString.call(a);if("[object Window]"==d)return"object";if("[object Array]"==d||"number"==typeof a.length&&"undefined"!=typeof a.splice&&"undefined"!=typeof a.propertyIsEnumerable&&!a.propertyIsEnumerable("splice"))return"array";if("[object Function]"==d||"undefined"!=typeof a.call&&"undefined"!=typeof a.propertyIsEnumerable&&!a.propertyIsEnumerable("call"))return"function"}else return"null";
else if("function"==b&&"undefined"==typeof a.call)return"object";return b}function r(a){return void 0!==a}function ca(a){return"string"==typeof a}function da(a){return a[ea]||(a[ea]=++fa)}var ea="closure_uid_"+Math.floor(2147483648*Math.random()).toString(36),fa=0;function s(a,b){var d=a.split(
You can’t perform that action at this time.