Instantly share code, notes, and snippets.

View closhrc.clj
;; See https://github.com/dundalek/closh/
(defn eval-string [s]
(eval (clojure.edn/read-string
(str "(do" s ")"))))
(defn closhrc []
(slurp (str (getenv "HOME") "/.closhrc")))
(defn eclosh []
View repl_compile.clj
(defn compile-inline [ns-name code]
(let [tmp-dir *compile-path*
sanitized-ns-name (.replace (str ns-name) "-" "_")
clj-file (clojure.java.io/file (str tmp-dir "/" (clojure.string/join "/" (clojure.string/split sanitized-ns-name #"\.")) ".clj"))]
(clojure.java.io/make-parents clj-file)
(with-open [file (clojure.java.io/writer clj-file)]
(binding [*out* file]
View eugene_and_jeroen.clj
(ns clojure-dojo.core)
(import '[javax.imageio ImageIO]
'[java.awt.image BufferedImage])
(def img (let [f "resources/image.png"]
(ImageIO/read (clojure.java.io/file f))))
View strict_keys_spec.clj
;; If you want to fail before your old https://github.com/plumatic/schema
;; function fails the strict-keys macros below will help you find it.
(require '[clojure.spec.alpha :as s]
'[clojure.set])
(defn allowed-keys? [ks]
(fn [m]
(empty? (clojure.set/difference (set (keys m)) ks))))
View set_and_identity.md

Empty sets are always identical (apparently)

(identical? #{} #{}) ;=> true

But when we add values this changes

(identical? #{1} #{1}) ;=> false
View alternative_dependencies.md

External dependencies: a different approach?

Libraries like https://github.com/danielsz/system offer a way to share reusable components/namespaces. The downside is that it is impossible to customize (without weird hacks or code). Often it is easier to copy-paste the code in your repo.

What if we could automate the copy-pasting in these cases? I would like to propose an idea for a (leiningen) plugin to share code without the pains of normal dependencies. No (Leftpad)[http://www.haneycodes.net/npm-left-pad-have-we-forgotten-how-to-program/] issues.

Requirements

  • No external dependencies (no risk of losing a remote dependency e.g. read about left-pad issues [1])
  • Less work than copy-pasting
  • No conflicts between dependencies
View example.clj
(ns adgoji.rum.reconciler.example
(:require [rum.core :as rum]
[adgoji.rum.subscriptive :as sub]))
(enable-console-print!)
(println "This text is printed from src/adgoji.rum.reconciler/core.cljs. Go ahead and edit it and see reloading in action.")
;; define your app data so that it doesn't get over-written on reload
View search.clj
(ns adgoji.planning.search
(:require [clojure.data.priority-map :as priority-map]))
(defprotocol IStack
(next-node [_])
(rest-nodes [_])
(add-nodes [_ nodes]))
(defrecord Queue [queue]
IStack
View sample_log_normal.clj
(require '[incanter.core :as incanter])
;; From http://stats.stackexchange.com/questions/95498/how-to-calculate-log-normal-parameters-using-the-mean-and-std-of-the-given-distr#95506
(defn sample-log-normal [n & {:keys [mean sd]}]
(let [sd-log-part (incanter/log (+ 1.0 (/ (* sd sd) (* mean mean))))
mu (- (incanter/log mean) (* 0.5 sd-log-part))
sigma (incanter/sqrt sd-log-part)]
(incanter/exp (incanter.stats/sample-normal n :mean mu :sd sigma))))
View manifold_test.clj
(ns manifold-test
(:require [midje.sweet :refer :all]
[manifold.stream :as s]))
;; Observed with [manifold "0.1.6-alpha1"] and [org.clojure/clojure "1.8.0"] and Java 8
;; Used [midje "1.8.3"] for testing
(facts "about manifold.stream/buffered-stream"
(fact "Issue 1: goes over capacity when manifold.stream/put doesn't respect back pressure"
(let [s (s/buffered-stream 10)