Skip to content

Instantly share code, notes, and snippets.

(defn -inheritable*
[var]
(let [*i (doto (proxy [InheritableThreadLocal] []
;; This is where the magic happens.
;; childValue is evaluated in the parent thread where bindings should still valid.
(childValue [{:keys [bindings]}]
(let [new-bindings (merge bindings (get-thread-bindings))]
{:bindings new-bindings
:get-val (fn [] (with-bindings new-bindings
(deref var)))})))
(defn deep-merge
"Recursively merges maps. If vals are not maps, the last value wins."
[& vals]
(if (every? map? vals)
(apply merge-with deep-merge vals)
(last vals)))
(defn deep-merge-with
"Like merge-with, but merges maps recursively, applying the given fn
only when there's a non-map at a particular level."
@Frozenlock
Frozenlock / test.cljs
Last active June 16, 2017 13:15 — forked from skrat/test.cljs
;; some speed tests of functions to access nested properties in JS objects.
(def test-data (->> (into {} (for [k1 (range 10)]
[k1 (into {} (for [k2 (range 10)]
[k2 (into {} (for [k3 (range 10)]
[k3 k3]))]))]))
(clj->js)))
;; some speed tests of functions to access nested properties in JS objects.
(def test-data (->> (into {} (for [k1 (range 10)]
[k1 (into {} (for [k2 (range 10)]
[k2 (into {} (for [k3 (range 10)]
[k3 k3]))]))]))
(clj->js)))
(defn build-paths
"Given a tree (map), return all the paths leading to a value."
[m]
(->> (for [[k v] m]
(if (map? v)
(map #(cons k %) (build-paths v))
[[k]]))
(apply concat)))
(ns cea.buddy-token
"The token based authentication and authorization backends."
(:require [buddy.auth.protocols :as proto]
[buddy.auth.http :as http]
[buddy.auth :refer [authenticated?]]
[buddy.sign.jws :as jws]
[buddy.sign.jwe :as jwe]
[clojure.string :as s]))
(defn with-random
"Add a random geospatial queries to a fetch. Should be used with a
limit, otherwise default limit to 5 items." [fetch-fn coll & {:as args}]
(let [where (:where args)
new-args (vec (merge {:limit 5} ;security limit if none is provided
args {:where (merge where {:random_point {:$near [(rand) 0]}})}))]
(apply fetch-fn (concat [coll] (apply concat new-args)))))
(defn get-object-historic-data
(defun replace-string-in-rectangle (from-string to-string &optional delimited start end)
(interactive
(let ((common
(query-replace-read-args
"Replace string in rectangle" nil)))
(list (nth 0 common) (nth 1 common) (nth 2 common)
(region-beginning)
(region-end))))
(let ((replacement-rectangle
(mapcar '(lambda (arg) (replace-in-string arg from-string to-string))
@Frozenlock
Frozenlock / gist:3194922
Created July 28, 2012 21:44
Open command shows a list of recent file
;----------------------------------------------------
;;;==== Open command shows a list of recent file ====
;----------------------------------------------------
(require 'recentf)
;; enable recent files mode.
(recentf-mode t)
; 200 files ought to be enough.
(setq recentf-max-saved-items 200)
(defun ido-recentf-open ()
@Frozenlock
Frozenlock / gist:3140372
Created July 19, 2012 02:28
Uppercase word in emacs (with save-excursion)
(global-set-key (kbd "C-c u")
(lambda()(interactive)
(save-excursion
(let (pt)
(skip-chars-backward "-_A-Za-z0-9")
(setq pt (point))
(skip-chars-forward "-_A-Za-z0-9")
(upcase-region pt (point)))
(message "UPPERCASE!"))))