Skip to content

Instantly share code, notes, and snippets.

View IvayloDankolov's full-sized avatar

Ivaylo Dankolov IvayloDankolov

View GitHub Profile
@IvayloDankolov
IvayloDankolov / example.cpp
Last active August 29, 2015 14:19
Compile-time resolved variable arity map, or why one should never try to do that in C++
vector<int> a{{2,4,6}};
vector<int> b{{1,2,3,4,5}};
vector<int> c{{-1,1-1,1,-1,1,-1,1}};
vector<int> res = map([](int x, int y, int z) {return a*b*c;}, a,b,c);
//res -> {-2,8,-18}
@IvayloDankolov
IvayloDankolov / point-free-4clojure-111.clj
Created April 12, 2014 08:55
Why? Because we can, obviously.
(def flip
(comp
(partial apply comp)
reverse
(partial list list reverse)
(partial partial apply)))
(def unspace
(partial mapv (comp
(partial apply str)
(defn cartesian [coll1 coll2]
(for [x coll1 y coll2]
[x y]))
(defn collapse-if [pred f coll]
(->> coll
(reduce (fn [processed curr]
(let [prev (peek processed)]
(if (and prev (pred prev curr))
(conj (pop processed) (f prev curr) nil)
@IvayloDankolov
IvayloDankolov / rng.clj
Last active August 29, 2015 13:57
Modified version of shuffle, rand, rand-int that take random number generators as a parameter.
(defn shuffle-with
"Returns a random permutation of coll using
rng as the random number generator."
[^java.util.Random rng
^java.util.Collection coll]
(let [al (java.util.ArrayList. coll)]
(java.util.Collections/shuffle al rng)
(clojure.lang.RT/vector (.toArray al))))
(defn rand-with