Skip to content

Instantly share code, notes, and snippets.

luxbock luxbock

Block or report user

Report or block luxbock

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
View playground.clj
(transform
[MAP-VALS
(view frequencies)
(collect-one (view #(apply max (vals %))))]
(fn [mx fs] (transform MAP-VALS #(/ % mx) fs))
{:a [1 1 3 4] :b [1 1 2 2 2 3]})
;; => {:a {1 1, 3 1/2, 4 1/2}, :b {1 2/3, 2 1, 3 1/3}}
View expand.clj
;; Expanding the following expression as far as it can go:
(filter even? (range 10))
;; equals
(lazy-seq
(when-let [s (seq (range 10))]
(if (chunked-seq? s)
(let [c (chunk-first s), size (count c), b (chunk-buffer size)]
View rdp.clj
(ns rdp-214.core
(:require [clojure.java.io :as io]
[clojure.string :as str]
[clojure.core.typed :as t
:refer [ann U Seq Str Vec Sequential]]))
;;;; Boilerplate ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(t/tc-ignore
@luxbock
luxbock / benchmark.clj
Last active Aug 29, 2015
The Little Schemer vs. regular Clojure versions of the Collatz function
View benchmark.clj
(ns little-schemer.benchmark
"Benchmarking the function C of the Collatz Conjencture:
http://en.wikipedia.org/wiki/Collatz_conjecture
in regular Clojure (C*) and as it is defined in the book The Little Schemer,
where all of the parts are defined recursively in terms of add1 and sub1."
(:gen-class)
(:refer-clojure :exclude [even?])
(:require [criterium.core :refer [quick-bench]]))
View results.clj
(defn C
[n]
(cond
(one? n) 1
(even? n) (C (div n 2))
:else (C (add1 (times 3 n)))))
(defn C* [n]
(cond
(= 1 n) 1
View anonymous-gist.clj
(defn C [n]
(cond
(one? n) 1
(even? n) (C (div n 2))
:else (C (add1 (times 3 n)))))
(defn C* [n]
(cond
(= 1 n) 1
(clojure.core/even? n) (C (quot n 2))
@luxbock
luxbock / anonymous-gist.clj
Created Jan 25, 2015
Referential Transparency
View anonymous-gist.clj
(filter even? (range 10))
;; equals
(new clojure.lang.LazySeq
(fn []
(when-let
[s (. clojure.lang.RT
(seq (new clojure.lang.LazySeq
#(let [b (clojure.lang.ChunkBuffer. 32)
View core.clj
(ns debug-walker.core
(:refer-clojure :exclude [read-string])
(:require [clojure.java.io :as io]
[clojure.repl :as repl]
[clojure.tools.reader :refer [read-string]]
[clojure.pprint :refer [pprint]]
[rewrite-clj.zip :as z])
(:import [java.io LineNumberReader InputStreamReader PushbackReader]))
(defn file-source-fn
View anonymous-gist.clj
;; Given that:
(destructure
'[[f [a b] {:keys [c d]} & rs]
[:first [:a 1 :b 2] {:c "C" :d "D"} 3 4 5]])
;; =>
[vec__22487 [:first [:a 1 :b 2] {:c "C", :d "D"} 3 4 5]
f (clojure.core/nth vec__22487 0 nil)
vec__22488 (clojure.core/nth vec__22487 1 nil)
a (clojure.core/nth vec__22488 0 nil)
@luxbock
luxbock / bfs.clj
Created Dec 29, 2014
Clojure Zipper Breadth First Search
View bfs.clj
(defn breadth-first-search [z]
(letfn [(zip-children [loc]
(when-let [first-child (zip/down loc)]
(take-while (comp not nil?)
(iterate zip/right first-child))))])
(loop [ret []
queue (conj clojure.lang.PersistentQueue/EMPTY z)]
(if (seq queue)
(let [[node children] ((juxt zip/node zip-children) (peek queue))]
(recur (conj ret node) (into (pop queue) children)))
You can’t perform that action at this time.