Skip to content

Instantly share code, notes, and snippets.

@stuartsierra
stuartsierra / Sum.java
Created June 18, 2013 17:15
Brief demonstration of dead-code elimination in the JVM
/* Demonstration of dead-code elimination in the JVM.
*
* This class demonstrates some of the difficulties of benchmarking
* code which may be subject to dead-code elimination by the JVM.
* There are two loops, both calling the same function. But only the
* second loop actually uses the result of the function.
*
* When I run this code, I get results like this:
*
* Dead code:
@stuartsierra
stuartsierra / gist:5788531
Last active August 14, 2018 16:45
Cannot convey data from an exception within a Datomic transaction
;; Example showing that I cannot convey data from an exception within
;; a transaction.
(ns example
(:require [datomic.api :as d])) ; Datomic Free 0.8.4007
(def uri "datomic:mem:example")
(d/create-database uri)
@stuartsierra
stuartsierra / data_json_0_1_compat.clj
Created October 25, 2012 02:29
data.json 0.1.x compatibility shim
;;; data.json 0.1.x compatibility shim
;; Loading this file alongside data.json version 0.2.0 adds
;; definitions which make it compatible with the public API of
;; data.json version 0.1.3.
;; Copyright (c) Stuart Sierra, 2012. All rights reserved. The use and
;; distribution terms for this software are covered by the Eclipse
;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
@stuartsierra
stuartsierra / lein2-issue.md
Created July 6, 2012 21:04
'file:' repositories in Leiningen 2.0.0-preview5 and later

Sample Project

Starting from:

lein new foo
cd foo

Say I have a random JAR file that is not available in any repository:

touch README.md

@stuartsierra
stuartsierra / errors.clj
Created April 20, 2012 14:27
Example error messages from common mistakes with Datomic
;; Some example error messages resulting from common mistakes
;; using Datomic 0.8.4138
(ns errors
(:use [datomic.api :as d :only (db q)]))
(def uri "datomic:mem://database")
(d/create-database uri)
@stuartsierra
stuartsierra / clojurescript-project.clj
Created January 20, 2012 18:04
project.clj for ClojureScript project
(defproject foo "1.0.0-SNAPSHOT"
:description "FIXME: write description"
:dependencies [[org.clojure/clojure "1.3.0"]
[org.clojure/clojurescript "0.0-927"]])
@stuartsierra
stuartsierra / test_clj_byte_chunk_seq.clj
Created December 13, 2011 13:43
Clojure chunked binary sequences
(ns test-clj-byte-chunk-seq
(:import (java.io InputStream OutputStream
FileInputStream FileOutputStream)))
(set! *warn-on-reflection* true)
(def ^:const ONE_MEG (* 1024 1024))
(deftype ByteArrayChunk [^bytes array ^int offset ^int end]
clojure.lang.IChunk
@stuartsierra
stuartsierra / gist:1212314
Created September 12, 2011 20:29
Enable Electric Ruby Mode in Emacs
;; Add this to ~/.emacs
;; Missing from ruby-mode.el, see https://groups.google.com/group/emacs-on-rails/msg/565fba8263233c28
(defun ruby-insert-end ()
"Insert \"end\" at point and reindent current line."
(interactive)
(insert "end")
(ruby-indent-line t)
(end-of-line))
@stuartsierra
stuartsierra / gist:1188879
Created September 2, 2011 15:14
XML parsing and clojure.zip
;;; XML parsing and clojure.zip
;;; There's got to be a cleaner way to do this.
;;; I have code like this:
(defn jdks-loc [xml-zipper]
(zip/down
(first
(filter #(let [n (zip/node %)]
@stuartsierra
stuartsierra / phone_code.clj
Created July 25, 2011 23:46
Phone number coding program in Clojure
;; Adapted from "An empirical comparison of C, C++, Java, Perl,
;; Python, Rexx, and Tcl for a search/string-processing program"
;; by Lutz Prechelt
;; This was a 15-minute effort while listening to a talk. It's *not*
;; well thought-out or designed.
;; By Stuart Sierra,
;; @stuartsierra on Twitter