Skip to content

Instantly share code, notes, and snippets.

@michalmarczyk
michalmarczyk / join_xform.clj
Created March 6, 2018 02:50
data.avl-based join transducer as discussed in the hallway track of Clojure/conj 2018
(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 November 18, 2017 05:39
Compute permutation that sorts the input
;; 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 November 18, 2017 05:36
Compute permutation that sorts the input
;; 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 January 17, 2017 00:13
Clojure local shadowing, loop semantics
;;; 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
@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)))))

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 19:57
letrec for Clojure using tools.macro's symbol-macrolet
;(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 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]