Skip to content

Instantly share code, notes, and snippets.

@michalmarczyk
michalmarczyk / pv-structure-sharing.clj
Created April 25, 2010 22:31
demonstrating structural sharing in clojure.lang.PersistentVector instances
;; demonstrating structural sharing
;; in clojure.lang.PersistentVector
;; instances
(let [v1 (vec (range 100))
v2 (conj v1 :foo)
v3 (conj v1 :bar)
v4 (conj (vec (range 100)) :quux)
PV-root (doto (.getDeclaredField clojure.lang.PersistentVector "root")
(.setAccessible true))
@michalmarczyk
michalmarczyk / type-fun.clj
Created April 24, 2010 05:14
fooling around with Clojure interfaces, part 1
(deftype Foo [x y z]
clojure.lang.IKeywordLookup
(getLookupThunk
[self k]
(reify
clojure.lang.ILookupThunk
(get
[self k]
(if-let [v (k {:x x :y y :z z})]
(inc v)))))
@michalmarczyk
michalmarczyk / update-bean.clj
Created April 23, 2010 02:50
update a Java bean given a Clojure (keywordised property name -> value) map
(defmacro update-bean
[obj props-map]
`(let [obj# ~obj
props-map# ~props-map
dots# (map (fn [[k# v#]]
`(. ~(->> k# as-str camelize upcase (str "set") symbol) ~v#))
props-map#)
doto# `(doto ~obj# ~@dots#)]
(eval doto#)))
@michalmarczyk
michalmarczyk / gist:374764
Created April 22, 2010 03:12
Peter Norvig's unifier in Clojure, take 2
(ns
#^{:doc "CL original by Peter Norvig, initial version in Clojure by Kevin Livingston. See http://groups.google.com/group/clojure/browse_thread/thread/996ecadf98328c6b#"}
unifier.core
(:use [clojure.contrib
[def :only (defvar defalias)]
[core :only (seqable?)]])
(:require [clojure
[walk :as walk]
[zip :as zip]]))
@michalmarczyk
michalmarczyk / unifier.clj
Created April 21, 2010 01:18
Peter Norvig's unifier in Clojure
(ns
#^{:doc "CL original by Peter Norvig, initial version in Clojure by Kevin Livingston. See http://groups.google.com/group/clojure/browse_thread/thread/996ecadf98328c6b#"}
unifier.core
(:use [clojure.contrib.def :only (defvar)]))
(defn variable?
"Is x a variable (a symbol beginning with '?')?"
[x]
(and (symbol? x) (= (first (name x)) \?)))
@michalmarczyk
michalmarczyk / clojure-font-lock-setup.el
Created March 19, 2010 06:02
coloured SLIME REPL for Clojure
;;; all code in this function lifted from the clojure-mode function
;;; from clojure-mode.el
(defun clojure-font-lock-setup ()
(interactive)
(set (make-local-variable 'lisp-indent-function)
'clojure-indent-function)
(set (make-local-variable 'lisp-doc-string-elt-property)
'clojure-doc-string-elt)
(set (make-local-variable 'font-lock-multiline) t)
@michalmarczyk
michalmarczyk / pi.clj
Created March 16, 2010 07:23
Calculate π through Euler-van Wijngaarden transformation of Gregory-Leibniz series
(use '[clojure.contrib.seq :only (reductions)])
;;; Calculate π through Euler-van Wijngaarden transformation of Gregory-Leibniz series
(def *use-exact-numbers* true)
(defn gregory-leibniz-series []
(map #(/ 4 %)
(interleave (iterate #(+ % 4) (if *use-exact-numbers* 1 1.))
(iterate #(- % 4) (if *use-exact-numbers* -3 -3.)))))
@michalmarczyk
michalmarczyk / clojure-project.el
Created February 18, 2010 01:44
environment setup for Clojure projects using lein file layout
(setq clojure-project-extra-classpaths
'(
; "deps/"
"src/"
"classes/"
"test/"
))
(setq clojure-project-jar-classpaths
'(
@michalmarczyk
michalmarczyk / scond.clj
Created February 3, 2010 00:43
Scheme-style cond macro for Clojure
(defmacro scond
"Scheme style cond macro.
Use :>> instead of => and :else instead of else."
[& clauses]
(when-let [clause (first clauses)]
(cond
(= (first clause) :else) (if (next clauses)
(throw (IllegalArgumentException.
(str ":else clause must be last in scond: "
@michalmarczyk
michalmarczyk / leftist-heap-wheel-sieves.clj
Created January 28, 2010 05:37
a leftist heap implementation and incremental wheel sieves built on top of it
(declare leftist-heap)
(defprotocol heap
(heap-empty? [h])
(heap-merge [h1 h2])
(heap-rank [h])
(heap-insert [h x])
(heap-get-min [h])
(heap-del-min [h]))