Skip to content

Instantly share code, notes, and snippets.

@sbocq
sbocq / flatten-keys.clj
Created February 18, 2015 23:10
Flatten keys
;; (flatten-keys {:name {:first "Rich" :last "Hickey"} :number [1 415 123 4567]})
;; => {"$.number[0]" 1, "$.number[1]" 415, "$.number[2]" 123, "$.number[3]" 4567, "$.name.first" "Rich", "$.name.last" "Hickey"}
(defn flatten-keys [thing]
(letfn [(-map-key [prefix k] (str prefix "." (name k)))
(-seq-key [prefix i] (str prefix "[" i "]"))
(-flatten-entry [make-key prefix result entry]
(let [[k v] entry]
(-flatten-thing result (make-key prefix k) v)))
(-flatten-thing [result key thing]
@jmreidy
jmreidy / clj_project-name.server
Last active August 29, 2015 14:14
CLJS setup
(ns project-name.server
(:require [clojure.java.io :as io]
[project-name.dev :refer [is-dev? inject-devmode-html]]
[compojure.core :refer [GET defroutes]]
[compojure.route :refer [resources]]
[compojure.handler :refer [api]]
[net.cgrand.enlive-html :refer [deftemplate]]
[net.cgrand.reload :refer [auto-reload]]
[ring.middleware.reload :as reload]
[environ.core :refer [env]]
@senior
senior / gist:3df55a058dbc4e91f0ef
Created December 18, 2014 21:20
Show all local branches, sorted by date
alias glist="git for-each-ref --sort=-committerdate refs/heads/ --format='%(committerdate:short) %(authorname) %(refname:short)'"
@ithayer
ithayer / reducers.clj
Last active August 29, 2015 14:05
Transducers Examples
;; Example of reducing fn that computes sufficient
;; statistics for a mean.
=> (defn mean-reducer [memo x]
(-> memo
(update-in [:sum] + x)
(update-in [:count] inc)))
=> (reduce mean-reducer {:sum 0 :count 0} (range 10))
{:count 10, :sum 45}
@taylorSando
taylorSando / datomic-rules.clj
Created June 5, 2014 01:15
Datomic recursion using rules
(defn recursive-rule
"A recursive rule for establishing prototype inheritance/isa relationship.
Can specify a maximum depth to travel, or none if there are no restrictinos.
rule The name of the rule
e The entity
a The attribute
v The value
Should be creating the rule like: (recursive-rule 'isa '?e '?a '?v)
Then within a query, can refer to it like this:
(isa ?e :thing/isa ?v) "
@candera
candera / ssh-repl.org
Last active February 9, 2019 07:50
ssh-repl

Embedding an SSH-accessible REPL in a Clojure process

N.B. This is now a library, thanks to the efforts of the wonderful @mtnygard. And the README does a good job of making clear just how terrible an idea it is to actually do this. :)

As any Clojurist knows, the REPL is an incredibly handy development tool. It can also be useful as a tool for debugging running programs. Of course, this raises the question of how to limit access to the REPL to authorized parties. With the Apache SSHD library, you can embed an SSH server in any JVM process. It takes only a little code to hook this up to a REPL, and to limit access either by public key or

@trptcolin
trptcolin / mavericks_upgrade
Created October 23, 2013 04:37
mavericks upgrade journal
√ Zephyros (window manager) needs universal access:
http://www.tekrevue.com/2013/06/25/how-to-enable-access-for-assistive-devices-in-os-x-mavericks/
√ gcc can’t find stdio.h (& similar) - /usr/include got blown away
xcode-select --install
see https://github.com/mxcl/homebrew/issues/20427
√ java install got blown away & replaced with a stub that says to download java
http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
@halgari
halgari / gist:6251731
Last active December 21, 2015 04:48
(defn throttle-chan [input]
(let [throttle-chan (chan)
output-chan (chan)]
(go
(loop [throttle-msec nil]
(let [t-chan (when-not (= :disabled throttle-msec)
(timeout throttle-msec))
[val c] (alt! [input throttle-chan])]
(if (= c throttle-chan)
(recur val)
(def cds (collection))
;; interact with database
(go
(>! (:in cds)
{:op :create
:val {:title "Soft Machine Vol. 1"
:artist "Soft Machine"
:year 1969}})
@adambard
adambard / errors.clj
Created May 13, 2013 05:48
An example of functional error handling in clojure.
(ns example.errors)
(defn clean-address [params]
"Ensure (params :address) is present"
(if (empty? (params :address))
[nil "Please enter your address"]
[params nil]))
(defn clean-email [params]
"Ensure (params :email) matches *@*.*"