Skip to content

Instantly share code, notes, and snippets.

@pesterhazy
pesterhazy / guard-nil.clj
Last active August 29, 2015 14:21
guard-nil: ensure non-nil argument, raise descriptive exception otherwise
(defmacro guard-nil
"Returns `x` unless it is nil, in which case a descriptive exception is raised"
[x]
`(if-some [v# ~x]
v#
(throw (AssertionError. (str "Form should not be nil: " (pr-str '~x))))))
@pesterhazy
pesterhazy / profiles.clj
Last active August 29, 2015 14:22
profiles.clj for leiningen: vinyasa.pull pulls lein dependencies into a running repl
;; An example of `profiles.clj'. You can install it by placing it in the
;; ~/.lein/profiles.clj folder
;;
;; Based on the example given in: https://github.com/zcaudate/vinyasa
{:user {:dependencies [[org.clojure/tools.namespace "0.2.4"]
[leiningen #=(leiningen.core.main/leiningen-version)]
[im.chit/vinyasa "0.3.4"]]
:injections
[(require '[vinyasa.inject :as inject])
@pesterhazy
pesterhazy / into
Created June 23, 2015 08:51
redirect output to a file without ">"
#!/usr/bin/env bash
#
# Save to /usr/bin/into
#
# Usage: into output.txt tac input.txt
#
set -euo pipefail
to="$1"
@pesterhazy
pesterhazy / csv2xls
Last active August 29, 2015 14:23
csv2xls: convert CSV file to XLS (Excel 2007 format) on the command line
#!/usr/bin/env bash
#
# You need gnumeric installed, which provides the
# ssconvert tool (on Ubuntu: sudo apt-get install gnumeric)
#
# Usage:
#
# generate_csv | csv2xls > out.xls
@pesterhazy
pesterhazy / log
Created September 11, 2015 11:47
these derivations will be built:
/nix/store/cqqqv7mv1b3v33bjrrvg5xqyf71wa1j5-go1.5-crypto-2015-08-29.drv
building path(s) ‘/nix/store/6r1jzp8lvnlg9winakiz96di9ddqjqfd-go1.5-crypto-2015-08-29’, ‘/nix/store/vwxj59hfils0340fz0mibmigrrl157vx-go1.5-crypto-2015-08-29-bin’
unpacking sources
unpacking source archive /nix/store/bz8pb5lwcghk9mv8slwxgsykymm4gl0q-crypto-d5c5f1769f2fcd2377be6f29863081f59a4fc80f-src
source root is crypto-d5c5f1769f2fcd2377be6f29863081f59a4fc80f-src
patching sources
configuring
building
golang.org/x/crypto/blowfish
@pesterhazy
pesterhazy / nuke-certs.sh
Created February 16, 2016 11:18
Reset code signing certificates to fix builds using `gym`
#!/usr/bin/env bash
# nuke-certs
#
#
# This script has been reported to help with errors like
#
# this:
# + xcodebuild -exportArchive -exportOptionsPlist /var/folders/rj/8lnf662s3mlgzv5mcq2r2fnc0000gn/T/gym20160216-41694-14bt0ur_config.plist -archivePath '/Users/pe/Library/Developer/Xcode/Archives/2016-02-16/016-02-16 12.09.02.xcarchive' -exportPath /var/folders/rj/8lnf662s3mlgzv5mcq2r2fnc0000gn/T/gym20160216-41694-17mfck9.gym_output
# 2016-02-16 12:10:03.004 xcodebuild[43629:464102] [MT] IDEDistribution: -[IDEDistributionLogging _createLoggingBundleAtPath:]: Created bundle at path '/var/folders/rj/8lnf662s3mlgzv5mcq2r2fnc0000gn/T/016-02-16_12-10-03.003.xcdistributionlogs'.
@pesterhazy
pesterhazy / group-by-keys.clj
Created April 13, 2016 13:16
Group a map by one or more keys
(defn group-by-keys
"Group maps by a set of keys. For each key not grouped, all values are
returned as a set"
[ms kys]
(let [kys (set kys)
grouped (group-by #(select-keys % kys) ms)]
(map (fn [[pk pv]]
(into pk
(reduce (fn [a [k v]] (update a k #(conj (or % #{}) v)))
{}
(ns background
(:require [clojure.tools.logging :as log])
(:import [java.util.concurrent]))
(defonce !executor (delay (java.util.concurrent.Executors/newCachedThreadPool)))
(defn background
"Calls the fn passed in a thread from a thread pool, returning immediately.
Unlike future, background does not swallow exceptions."
[f]
;; Display arbitrary EDN-like values in a browser
;;
;; See https://github.com/yogthos/json-html
;;
;; in project.clj / build.boot:
;; [json-html "0.3.9"]
;; [hiccup "1.0.5"]
(require '[json-html.core :as jh]
'[hiccup.page])
@pesterhazy
pesterhazy / datomic-entity-history.clj
Last active December 7, 2020 11:13
Inspect a datomic entity's history
;; Show history of an entity
;;
;; useful for interactively inspecting what happened to a datomic entity in its lifetime
;;
;; use `entity-history` to get a list of transactions that have touched the entity (assertions, retractions)
;;
;; use `explain-tx` to find out what else was transacted in the txs
(defn entity-history
"Takes an entity and shows all the transactions that touched this entity.