Skip to content

Instantly share code, notes, and snippets.

View Bronsa's full-sized avatar

Nicola Mometto Bronsa

View GitHub Profile
@cgrand
cgrand / balance.clj
Created February 22, 2013 17:05
I ♥ associativity! comp is associative and optimized up to arity 3, let's leverage it! (I ♥ commutativity more, though)
(defn- balance [n f & args]
(if (> (count args) n)
(apply balance n f (map #(apply f %) (partition-all n args)))
(apply f args)))
;; optimization kicks in for N >= 4
=> (doseq [N (range 1 16)]
(let [++ (apply comp (repeat N inc))
b++ (apply balance 3 comp (repeat N inc))]
(println "N=" N)
(ns net.cgrand.decay
"Exponentially decaying lists. See http://awelonblue.wordpress.com/2013/01/24/exponential-decay-of-history-improved/
for background and http://clj-me.cgrand.net/2013/02/12/decaying-lists-log-scale-for-lists/ for documentation")
;; PRNG, formulas straight from java.util.Random javadoc
(defn- seed ^long [^long n]
(bit-and (unchecked-multiply n 0x5DEECE66D)
(unchecked-dec (bit-shift-left 1 48))))
(defn- next-seed ^long [^long seed]
@athos
athos / reader_test.clj
Created December 12, 2012 12:47
an example of *default-data-reader-fn*, a feature of Clojure 1.5
(ns reader-test)
(alter-var-root #'*default-data-reader-fn* (fn [_] cons))
#defn(fibs [n]
#letfn([#f([a b] #lazy-seq(#cons(a, #f(b, #+(a, b)))))]
#take(n, #f(0, 1))))
; #fibs(10) => (0 1 1 2 3 5 8 13 21 34)
@richhickey
richhickey / thread.clj
Created October 13, 2012 17:43
new thread macros draft
(defmacro test->
"Takes an expression and a set of test/form pairs. Threads expr (via ->)
through each form for which the corresponding test expression (not threaded) is true."
[expr
& clauses]
(assert (even? (count clauses)))
(let [g (gensym)
pstep (fn [[test step]] `(if ~test (-> ~g ~step) ~g))]
`(let [~g ~expr
~@(interleave (repeat g) (map pstep (partition 2 clauses)))]
@cstrahan
cstrahan / chicken.rb
Created August 31, 2012 13:50 — forked from tenderlove/chicken.rb
YARV bytecode compiler for Scheme
###
# Scheme code is translated to YARV byte code, then evaluated in the
# Ruby Virtual Machine
require 'rbconfig'
require 'dl'
require 'fiddle'
require 'strscan'
class RubyVM