Skip to content

Instantly share code, notes, and snippets.

@noisesmith
noisesmith / gist:ad14d0b091626de380ad093780ff9915
Created February 22, 2017 00:30
r-> like -> but it respects reduced
(defmacro r->
[x & forms]
(loop [x x, forms forms]
(if forms
(let [form (first forms)
threaded (with-meta `(let [x# ~x]
(if (reduced? x#)
x#
(~(first form) x# ~@(next form))))
(meta form))]
@noisesmith
noisesmith / user.clj
Created February 16, 2017 23:18
attach debugging info to a var, detach cleanly later
(defn set-debug
[target-var]
(let [metadata (meta target-var)
target (::original metadata @target-var)
debug-data (:debug-trace metadata (atom []))]
;; only one layer of debugging, if we were already debugging,
;; just replace the previous layer
(when-not (and (:debug-trace metadata)
(::original metadata))
(alter-meta! target-var assoc
@noisesmith
noisesmith / chunker.clj
Last active February 7, 2017 01:42
using core.async to combine data into chunks with a timeout
(ns noisesmith.chunker
(:require [clojure.core.async :refer [alts! <! >! timeout go-loop]]))
(defn launch-chunks
[chunk-size time-out rq-chan result-chan]
(go-loop
[acc []]
(let [[res c] (alts! [(timeout time-out)
rq-chan])
fresh (= c rq-chan)
@noisesmith
noisesmith / shell session
Created January 23, 2017 21:22
def is run even with aot
justin@borbetomagus: /tmp/aot-test$ cat src/aot_test/core.clj
(ns aot-test.core
(:gen-class))
(def loaded (java.util.Date.))
(defn -main
"I don't do a whole lot ... yet."
[& args]
(println "loaded at" loaded))
@noisesmith
noisesmith / repl session
Created January 13, 2017 19:11
clojure compiler doesn't try to be clever
kingfisher.core=> (require 'no.disassemble)
nil
kingfisher.core=> (no.disassemble/disassemble (fn [] (= 1 1)))
"// Compiled from form-init3743260354239398999.clj (version 1.5 : 49.0, super bit)\npublic final class kingfisher.core$eval42227$fn__42228 extends clojure.lang.AFunction {\n \n // Method descriptor #7 ()V\n // Stack: 1, Locals: 1\n public core$eval42227$fn__42228();\n 0 aload_0 [this]\n 1 invokespecial clojure.lang.AFunction() [9]\n 4 return\n Line numbers:\n [pc: 0, line: 1]\n \n // Method descriptor #11 ()Ljava/lang/Object;\n // Stack: 4, Locals: 1\n public java.lang.Object invoke();\n 0 lconst_1\n 1 lconst_1\n 2 invokestatic clojure.lang.Util.equiv(long, long) : boolean [17]\n 5 ifeq 14\n 8 getstatic java.lang.Boolean.TRUE : java.lang.Boolean [23]\n 11 goto 17\n 14 getstatic java.lang.Boolean.FALSE : java.lang.Boolean [26]\n 17 areturn\n Line numbers:\n [pc: 0, line: 1]\n [pc: 2, line: 1]\n Local variable
@noisesmith
noisesmith / repl
Last active January 11, 2017 20:39
use all non-macros from target ns in local scope
phoenix.core=> (user/with-use user (+ 1 1))
2
phoenix.core=> (macroexpand-1 '(user/with-use user (+ 1 1)))
(clojure.core/let [help user/help find-name user/find-name non-macros user/non-macros src-dir user/src-dir exercise-coverage user/exercise-coverage apropos-better user/apropos-better] (+ 1 1))
@noisesmith
noisesmith / clojure.jar repl
Created December 28, 2016 23:03
weird lein repl printing behavior with some errors
(ins)user=> ({1 2 3} 3)
RuntimeException Map literal must contain an even number of forms clojure.lang.Util.runtimeException (Util.java:221)
3
RuntimeException Unmatched delimiter: ) clojure.lang.Util.runtimeException (Util.java:221)
(ins)user=> 2
2
@noisesmith
noisesmith / timed_future.clj
Last active December 16, 2016 02:11
acts like a future, but has a time-out after which it fails on deref
(ns condor.future-timeout
(:require [clojure.core.async :as >])
(:import (java.util Date)
(java.util.concurrent CancellationException)))
(defn timestamp
[]
(.getTime (Date.)))
(defn timed-future-call
@noisesmith
noisesmith / async_transducing.clj
Last active November 12, 2016 21:31
transducer on a channel
(ns foo.bar.async-transducing
(:require [clojure.core.async :as >]))
(defn run-it
[]
(let [c (>/chan 20 (partition-all 10))]
(>/go-loop []
(when-let [things (>/<! c)]
(println "things:" (pr-str things))
(recur)))
@noisesmith
noisesmith / gist:d6f8ed1a75c0b57873f100390f0bec54
Created October 27, 2016 21:37
get a file over http, at an arbitrary offset, without downloading everything
(import (java.net URL))
(defn skipped-url
[url byte-skip byte-end]
(let [http-connection (.openConnection url)]
(.setRequestProperty http-connection
"Range" (str "bytes=" byte-skip "-" byte-end))
(.getInputStream http-connection)))