Skip to content

Instantly share code, notes, and snippets.

@jafingerhut
jafingerhut / inputstream.clj
Created July 8, 2014 15:01
Example of reading java.io.InputStream in Clojure
(ns inputstream.core
(:require [clojure.java.io :as io]))
(defn read-is [^java.io.InputStream is]
(let [bufsize 8192
buf (byte-array bufsize)]
(loop [total-len 0]
(let [n (.read is buf)]
(cond
;; Examples showing that when *print-meta* is bound to true, clojure.pprint/pprint appears
;; to show metadata on symbols, but not on collections. Can be misleading when only *some*
;; metadata is shown. Led me to believe the other metadata wasn't there until I double-checked
;; with pr.
(defn ppm [f m]
(binding [*print-meta* m]
(clojure.pprint/pprint f)))
(defn prm [f m]
@jafingerhut
jafingerhut / gist:8359201
Last active January 2, 2016 20:49
do's inside top level do's are 'flattened'
;; In a fresh Clojure 1.5.1 'lein repl'
;; clojure.edn is not loaded yet
user=> (clojure.edn/read-string "(5)")
ClassNotFoundException clojure.edn java.net.URLClassLoader$1.run (URLClassLoader.java:366)
;; trying to eval this form does not do it, because it fails to analyze as a whole
user=> (eval `(when true (require 'clojure.edn) (clojure.edn/read-string "(5)")))
@jafingerhut
jafingerhut / str-seq-reader.clj
Last active December 31, 2015 17:19
str-seq-reader - Take a sequence of strings and return a Java Reader that, when read, returns the contents of the concatenation of those strings. Only as much of the sequence as needed by the reader is consumed. Examples of use at the bottom.
(ns piper.core
(:require [clojure.java.io :as io]
[clojure.string :as str])
(:import (java.io Reader Writer BufferedReader)))
(defn str-seq-reader
"Return a java.io.Reader that returns the same sequence of
characters as are in the concatenation of the sequence of strings
given as an argument. Consumes only as much of the sequence as needed
for any given read operation."
@jafingerhut
jafingerhut / foo.java
Created November 9, 2012 16:40
AndroChef Java decompiler output of class file compiled from Cedric Greevey's Clojure code
package perf;
import clojure.lang.AFunction;
import clojure.lang.IFn;
import clojure.lang.Indexed;
import clojure.lang.Numbers;
import clojure.lang.RT;
import clojure.lang.Var;
import java.awt.image.BufferedImage;
import perf.core.foo.fn__14;
@jafingerhut
jafingerhut / apropos2.clj
Created February 7, 2012 05:27
apropos2 and unresolve
;; Example use of unresolve:
;; user=> (unresolve #'replace)
;; (replace clojure.core/replace)
;; user=> (use 'clojure.string)
;; WARNING: replace already refers to: #'clojure.core/replace in namespace: user, being replaced by: #'clojure.string/replace
;; WARNING: reverse already refers to: #'clojure.core/reverse in namespace: user, being replaced by: #'clojure.string/reverse
;; nil
;; user=> (unresolve #'replace)
;; (replace clojure.string/replace)