Skip to content

Instantly share code, notes, and snippets.

Michał Marczyk michalmarczyk

Block or report user

Report or block michalmarczyk

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
@michalmarczyk
michalmarczyk / sieve.py
Created Jan 7, 2010
priority queue-based, wheel-using incremental prime number sieve in Python
View sieve.py
# 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
View transient-incremental-sieves.clj
(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 Jan 28, 2010
a leftist heap implementation and incremental wheel sieves built on top of it
View leftist-heap-wheel-sieves.clj
(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 Feb 3, 2010
Scheme-style cond macro for Clojure
View scond.clj
(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 Feb 18, 2010
environment setup for Clojure projects using lein file layout
View clojure-project.el
(setq clojure-project-extra-classpaths
'(
; "deps/"
"src/"
"classes/"
"test/"
))
(setq clojure-project-jar-classpaths
'(
@michalmarczyk
michalmarczyk / pi.clj
Created Mar 16, 2010
Calculate π through Euler-van Wijngaarden transformation of Gregory-Leibniz series
View pi.clj
(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 / unifier.clj
Created Apr 21, 2010
Peter Norvig's unifier in Clojure
View unifier.clj
(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 Apr 22, 2010
Peter Norvig's unifier in Clojure, take 2
View gist:374764
(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 Apr 23, 2010
update a Java bean given a Clojure (keywordised property name -> value) map
View update-bean.clj
(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 / type-fun.clj
Created Apr 24, 2010
fooling around with Clojure interfaces, part 1
View type-fun.clj
(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)))))
You can’t perform that action at this time.