Skip to content

Instantly share code, notes, and snippets.

@noisesmith
noisesmith / user.clj
Created August 2, 2019 23:36
function to run clojure tests under a directory by ns regex
(require '[clojure.java.io :as io]
'[clojure.main :as m]
'[clojure.test :as test])
(defn run-matching-tests
"(re)loads and runs tests under dir/ which match your regex,
useful for running all tests under a specific prefix
or containing a common name element like \"integration\""
([match]
(run-matching-tests match "test/" false))
@noisesmith
noisesmith / user.clj
Created June 13, 2019 22:22
custom user.clj, including a bunch of stuff I don't often use...
(ns user
(:require [clojure.test :as test]))
(defmacro lf
[f]
(load-file (str "/tmp/" (str f) ".clj")))
(defmacro l
[f]
`(lf ~(str "/clj-tmp-files/" f)))
--exclude=.git
--exclude=.svn
--exclude=resources/*
--exclude=*/resources/*
--exclude=*/public/*
--exclude=.repl/*
--exclude=*/.repl/*
--exclude=out/*
--exclude=*/out/*
--exclude=target/*
@noisesmith
noisesmith / repl session
Created July 28, 2018 00:04
using rename with refer-clojure
(ins)user=> (ns foo.bar (:refer-clojure :rename {map pam}))
nil
(ins)foo.bar=> (pam inc [1 2 3])
(2 3 4)
@noisesmith
noisesmith / repl session
Created June 1, 2018 00:58
weird circleci bug with assertion errors
user=> (ns foo.broken)
nil
foo.broken=> (require 'circleci.test 'clojure.test)
nil
foo.broken=> (defn foo [] {:pre [false]} true)
#'foo.broken/foo
foo.broken=> (clojure.test/deftest foo-test (foo))
#'foo.broken/foo-test
foo.broken=> (circleci.test/run-tests 'foo.broken)
test-results-dir: target/test-results
@noisesmith
noisesmith / agreeable.clj
Created June 3, 2017 22:05
core.async/thread but it cancels the future if possible if you close the channel
(ns org.noisesmith.agreeable
(:require [clojure.core.async :as >]
[clojure.core.async.impl.protocols :as >proto]
[clojure.core.async.impl.channels :as >chan])
(:import (clojure.lang Var)))
(deftype CancelableThreadChan [future-task channel]
>proto/ReadPort
(take! [this fn1-handler]
(>proto/take! channel fn1-handler))
@noisesmith
noisesmith / download.cljs
Created May 25, 2017 21:47
create a download of some data in a clojurescript repl (eg. figwheel)
(defn download
; adapted from https://stackoverflow.com/a/3665147
[data filename encoder]
(let [el (.createElement js/document "a")
data-str (js/encodeURIComponent (encoder data))]
(doto el
(.setAttribute "href" (str "data:text/plain;charset=utf-8,"
data-str))
(.setAttribute "download" filename))
(aset (.-style el) "display" "none")
@noisesmith
noisesmith / .ctags
Created April 17, 2017 17:06
improved exuberant ctags config for clojure users
--exclude=.git
--exclude=.svn
--exclude=resources/*
--exclude=*/resources/*
--exclude=*/public/*
--exclude=.repl/*
--exclude=*/.repl/*
--exclude=out/*
--exclude=*/out/*
--exclude=target/*
@noisesmith
noisesmith / gist:d0ee8172e1e24f574ffa760c4dc30be2
Created March 27, 2017 20:45
a macro to make varargs interop more readable
=> (defmacro vararg
[method object & args]
(let [[regular [_ t & variable]] (split-with #(not= % '|) args)]
`(. ~object ~method ~@regular (into-array ~t ~(vec variable)))))
#'user/vararg
=> (macroexpand-1 '(vararg get java.nio.file.Paths "foo" | String "bar" "baz"))
(. java.nio.file.Paths get "foo" (clojure.core/into-array String ["bar" "baz"]))
=> (vararg get java.nio.file.Paths "foo" | String "bar" "baz")
#object[sun.nio.fs.UnixPath 0x14ebb640 "foo/bar/baz"]
@noisesmith
noisesmith / gist:2f85a37c9f340ff21a14e00012737b78
Created February 25, 2017 01:05
using core.async in lumo via andare
justin@S: ~/.config-git/deleteme/deleteme$ lumo -c ~/.m2/repository/andare/andare/0.5.0/andare-0.5.0.jar
Lumo 1.1.0
ClojureScript 1.9.456
Docs: (doc function-name-here)
Exit: Control+D or :cljs/quit or exit
cljs.user=> (require-macros '[cljs.core.async.macros :as >>])
nil
cljs.user=> (require '[cljs.core.async :as >])
nil