Skip to content

Instantly share code, notes, and snippets.

@michalmarczyk
michalmarczyk / sieve.py
Created January 7, 2010 22:40
priority queue-based, wheel-using incremental prime number sieve in Python
# A priority queue-based, wheel-using incremental Sieve of Eratosthenes.
# See `The Genuine Sieve of Eratosthenes' by Melissa E. O'Neill
# (http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf)
# for the Haskell incremental sieve which inspired this implementation
# (along with detailed analyses of performance of several Haskell
# sieves).
#
# Usage:
# Sieve() -> simple SoE
# Sieve(Wheel(4)) -> SoE + a wheel based on 4 initial primes
@michalmarczyk
michalmarczyk / transient-incremental-sieves.clj
Created January 27, 2010 16:46
transient incremental sieves
(defn transient-incremental-sieve []
(map first
(iterate (fn [[previous crossouts]]
(loop [current (inc previous)
crossouts (transient crossouts)]
(if-let [witnesses (crossouts current)]
(recur (inc current)
(dissoc! (loop [cs crossouts
ws witnesses]
(if-let [w (first ws)]
@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]))
@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 / 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 / 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-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 / 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 / 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 / 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#)))