Skip to content

Instantly share code, notes, and snippets.

(require '[clojure.set :as set])
(def info
[{:year 2017
:month 4
:data "x"}
{:year 2017
:month 4
:data "y"}
@gonewest818
gonewest818 / jmx-thread-stats.clj
Last active February 28, 2020 15:15
getting thread stats via jmx in clojure
(ns xyz.core
(:require [clojure.string :refer [lower-case]])
(:import java.lang.management.ManagementFactory))
(defn describe-thread
"describe thread based on thread-info"
[thread-mx-bean thread-info]
(let [id (.getThreadId thread-info)]
{:id id
@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")
@vbedegi
vbedegi / react-sortable-hoc-in-reagent.cljs
Last active November 29, 2017 15:50
using react-sortable-hoc in reagent
(def react-sortable-hoc (aget js/window "deps" "react-sortable-hoc"))
(defn make-sortable-element-component [wrapped-component]
(let [sortable-element-factory (.-SortableElement react-sortable-hoc)]
(-> wrapped-component
r/reactify-component
sortable-element-factory
r/adapt-react-class)))
(defn make-sortable-container-component [wrapped-component]
@reborg
reborg / rich-already-answered-that.md
Last active February 23, 2024 13:09
A curated collection of answers that Rich gave throughout the history of Clojure

Rich Already Answered That!

A list of commonly asked questions, design decisions, reasons why Clojure is the way it is as they were answered directly by Rich (even when from many years ago, those answers are pretty much valid today!). Feel free to point friends and colleagues here next time they ask (again). Answers are pasted verbatim (I've made small adjustments for readibility, but never changed a sentence) from mailing lists, articles, chats.

How to use:

  • The link in the table of content jumps at the copy of the answer on this page.
  • The link on the answer itself points back at the original post.

Table of Content

(require '[net.cgrand.xforms :as x])
(defn rollup [dimensions valfn]
(let [[dim & dims] (reverse dimensions)]
(reduce
(fn [xform dim]
(comp
(x/by-key dim xform)
(x/transjuxt
{:detail (x/into {})
@viebel
viebel / defn-args.cljs
Last active January 21, 2020 11:15
The `spec` for `defn` arguments is called `:defn-args` and it is defined in [clojure.core.specs namespace](https://github.com/clojure/clojure/blob/clojure-1.9.0-alpha13/src/clj/clojure/core/specs.clj#L78-L84). But there are two problems with this implementation: 1. It has [not yet been ported](http://dev.clojure.org/jira/browse/CLJS-1813) to `cl…
(s/def ::local-name (s/and simple-symbol? #(not= '& %)))
(s/def ::binding-form
(s/or :sym ::local-name
:seq ::seq-binding-form
:map ::map-binding-form))
;; sequential destructuring
(s/def ::seq-binding-form
(s/and vector?
@henryw374
henryw374 / clojure-jstack.clj
Last active February 28, 2020 15:15
thread dump clojure programmatic jstack
(ns thread-dump
(:require [clojure.stacktrace :as st])
(:import [java.lang.management ManagementFactory]))
(defn jstack [n]
(let [threadMXBean (ManagementFactory/getThreadMXBean)
info (.getThreadInfo threadMXBean (.getAllThreadIds threadMXBean) 100)]
(print
(for [threadInfo info]
(str
@favila
favila / datomic-reset-attributes.clj
Last active February 21, 2021 13:37
Datomic transaction functions to "reset" attributes: i.e. make them have a post-transaction value you specify without having to enumerate the retractions.
(def tx-fns
[{:db/ident :db.fn/reset-attribute-values
:db/doc "Transaction function which accepts an entity identifier, attribute identifier
and set of values and expands to any additions and retractions necessary to
make the final post-transaction value of the attribute match the provided
values. Attribute values must be scalars.
If multiple values are provided on a cardinality-one attribute you will get a
datom conflict exception at transaction time."
:db/fn (d/function
@alandipert
alandipert / ec2query.boot
Last active July 31, 2018 16:33
Example of querying AWS infrastructure with Amazonica and Datomic, in Clojure with Boot-clj
#!/usr/bin/env boot
;; or `BOOT_FILE=ec2query.boot boot repl' for interactive use
(set-env! :dependencies '[[amazonica "0.3.23"]
[com.datomic/datomic-free "0.9.5344"]])
(require '[amazonica.aws.ec2 :as ec2]
'[amazonica.core :refer [defcredential]]
'[boot.cli :refer [defclifn]]
'[boot.util :refer [info]]