Skip to content

Instantly share code, notes, and snippets.

View bhugueney's full-sized avatar

bhugueney

View GitHub Profile
;; simple stats computed using xforms transducers
=> (require '[net.cgrand.xforms :as x])
=> (into {}
(x/by-key #(> % 0.5)
(x/transjuxt {:min x/min :max x/max :avg x/avg :sd x/sd}))
(repeatedly 1e6 rand))
{false
{:min 1.0036018363024368E-6,
:max 0.4999987525112054,
:avg 0.2502046186242099,
@cgrand
cgrand / merge3.clj
Last active February 18, 2016 23:29
;; not quite an apple-to-apple comparison (merge3 and into) but the order of magnitude is correct:
;; into: 5µs
;; merge3: 17ns (yes, ns not µs)
;;
;; this huge difference comes from the fact that merge3 runtime varies with the number of modified keys (here 1)
;; while into runtime varies with the number of keys.
user=> (def e clojure.lang.PersistentHashMapKVMono/EMPTY)
(defn merge3 [anc a b fix] (clojure.lang.PersistentHashMapKVMono/merge anc a b fix))
#'user/e
@jackrusher
jackrusher / naive-plant.clj
Last active March 12, 2016 16:24
Preview of my new 3D Turtle library, Gamera.
(defn branch
"A recursive branching tree structure with half-cylinder flowers."
[level]
(if (= 0 level)
[(length 1.5) cylinder]
[(angle (fn [_] (m/random 3 5))) ry-
(length (fn [t] (* (m/random 0.75 1.5) (or (:last-length t) 1.5))))
(map (fn [[a b]] (concat (take 8 (cycle [a b])) (branch (dec level))))
[[ry line] [ry- line] [rx line] [rx- line]])]))
;; pagerank in podwerkeg
(reduce (fn [ranks _]
(keg/rdd (keg/join links ranks)
(x/for [[page-id [page-links rank]] %
dest page-links]
[dest (/ rank (count page-links))])
(keg/by-key (x/reduce +) (map #(+ 0.15 (* 0.85 %))))))
(keg/rdd links (keg/by-key (map (constantly 1)))) (range 10))
@postspectacular
postspectacular / tetrahedron-intersect.clj
Last active July 26, 2021 13:25
Tetrahedron intersection (currently runs in ~36% time than original revision of this gist)
;; Tetrahedron intersection based on this paper & algorithm:
;; http://vcg.isti.cnr.it/Publications/2003/GPR03/fast_tetrahedron_tetrahedron_overlap_algorithm.pdf
;;
;; Unlike original algorithm this implementation produces correct
;; results regardless of the orientation/ordering of points defining
;; the two tetrahedra. This is achieved via the orient-tetra function,
;; which ensures the correct ordering of vertices for this algorithm
;; to function properly.
;;
;; Implementation extracted from upcoming geometry library
@cgrand
cgrand / repl.clj
Created January 22, 2014 22:31
Mon historique de l'atelier data vs fns du Paris Clojure User Group http://www.meetup.com/Paris-Clojure-User-Group/ du 22 Janvier 2014
;; Clojure 1.5.1
=> ; coll (fn [f acc]) renvoie (reduce f acc coll)
=> ; coll (fn [f init]) renvoie (reduce f init coll)
=> (defn fnil [f init] init)
WARNING: fnil already refers to: #'clojure.core/fnil in namespace: cljug.core, being replaced by: #'cljug.core/fnil
#'cljug.core/fnil
=> (reduce + 0 nil)
0
=> (fnil + 0)
0