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 / join_xform.clj
Created Mar 6, 2018
data.avl-based join transducer as discussed in the hallway track of Clojure/conj 2018
View join_xform.clj
(alembic/distill '[org.clojure/data.avl "0.0.17"])
(alembic/distill '[net.cgrand/xforms "0.12.1"])
(require '[clojure.data.avl :as avl]
'[net.cgrand.xforms :as x])
(defn join [keyfn xforms-map]
(comp
(x/multiplex xforms-map)
@michalmarczyk
michalmarczyk / order3.clj
Created Nov 18, 2017
Compute permutation that sorts the input
View order3.clj
;; Written to answer https://stackoverflow.com/questions/47254742/sort-primitive-array-with-custom-comparator-on-clojure
;; See https://gist.github.com/michalmarczyk/11bbfd0b19b6357f533b192bf9da84ac for the single-threaded version
(defn order3 [xs]
(let [rnd (java.util.Random.)
a1 (double-array xs)
a2 (long-array (alength a1))]
(dotimes [i (alength a2)]
(aset a2 i i))
(letfn [(quicksort [^long l ^long h]
@michalmarczyk
michalmarczyk / order2.clj
Created Nov 18, 2017
Compute permutation that sorts the input
View order2.clj
;; Written to answer https://stackoverflow.com/questions/47254742/sort-primitive-array-with-custom-comparator-on-clojure
(defn order2 [xs]
(let [rnd (java.util.Random.)
a1 (double-array xs)
a2 (long-array (alength a1))]
(dotimes [i (alength a2)]
(aset a2 i i))
(letfn [(quicksort [^long l ^long h]
(if (< l h)
@michalmarczyk
michalmarczyk / locals_loops.clj
Created Jan 17, 2017
Clojure local shadowing, loop semantics
View locals_loops.clj
;;; See the comment threads here:
;;; http://stackoverflow.com/questions/41677617/are-all-variables-in-clojure-constant
(loop [x 1
f (fn [] x)]
(if (== 1 x)
(recur 0 f)
(f)))
;= 1
View validate_under.clj
(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)))))
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 / letrec2.clj
Created May 16, 2014
letrec for Clojure using tools.macro's symbol-macrolet
View letrec2.clj
;(use '[clojure.tools.macro :only [symbol-macrolet]])
(defmacro letrec
"Like let, but the bindings may be mutually recursive, provided that
the heads of all values can be evaluated independently.
This means that functions, lazy sequences, delays and the like can
refer to other bindings regardless of the order in which they
appear in the letrec form."
[bindings & body]
@michalmarczyk
michalmarczyk / thread_or.clj
Created Jul 14, 2013
Short-circuiting logical disjunction of several futures' results
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 [ret (promise)
fps (promise)]
(deliver fps
(doall (for [f fs]
(let [p (promise)]
@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]
You can’t perform that action at this time.