Skip to content

Instantly share code, notes, and snippets.

@gtrak
gtrak / deferred.clj
Created March 12, 2013 00:14
Deferred loading. Remove :use and :requires from the ns declaration, as that will load the referred classes recursively in the static initializer of your namespace. Total time spent loading classes doesn't change, but you can amortize it along the perceptions of the user in chunks.
(defmacro deferred
"Loads and runs a function dynamically to defer loading the namespace.
Usage: \"(deferred clojure.core/+ 1 2 3)\" returns 6. There's no issue
calling require multiple times on an ns."
[fully-qualified-func & args]
(let [func (symbol (name fully-qualified-func))
space (symbol (namespace fully-qualified-func))]
`(do (require '~space)
(let [v# (ns-resolve '~space '~func)]
(v# ~@args)))))
(defn set-style
[elt property value]
(doto (.-style elt)
(.setProperty property value))
elt)
(defn set-styles
[elt s]
(let [style (.-style elt)]
(doseq [[property value] s]
-> a = {json : "hello"}
Object {json: "hello"}
-> a.json
"hello"
-> a = {"json" : "hello"}
Object {json: "hello"}
-> a.json
@gtrak
gtrak / gist:5042933
Created February 26, 2013 22:30
Request-scoped compojure bindings, compatible with defroutes.
(defmacro wrap-request-binding
"Wraps the ring handler definition to provide request-scoped bindings
via symbol-capture on %."
[bindings handler-def]
`(fn [request#]
(let [~'% request#
~@bindings]
(~handler-def request#))))
(defmacro request-binding-handlers
com.garytrakhman.cordova_test.app.flat_button = function() {
var flat_button__delegate = function(text, p__6105) {
var vec__6108 = p__6105;
var width = cljs.core.nth.call(null, vec__6108, 0, null);
var height = cljs.core.nth.call(null, vec__6108, 1, null);
var b = function() {
var G__6109 = new goog.ui.Button([cljs.core.str(text)].join(""), goog.ui.FlatButtonRenderer.getInstance());
G__6109.render(com.garytrakhman.cordova_test.app.top.call(null));
return G__6109
}();
@gtrak
gtrak / gist:4948166
Last active December 13, 2015 17:28
;;; helpful to get rid of repetitive compojure destructuring.
(defmacro wrap-request-binding
"Wraps the ring handler definition to provide request-scoped bindings
via symbol-capture on %."
[bindings handler-def]
`(fn [request#]
(let [~'% request#
~@bindings]
(~handler-def req#))))
@gtrak
gtrak / gist:4726660
Created February 6, 2013 22:55
circular-lists.
user=> (take 6 (cycle [1 2 3]))
(1 2 3 1 2 3)
user=> (source cycle)
(defn cycle
"Returns a lazy (infinite!) sequence of repetitions of the items in coll."
{:added "1.0"}
[coll] (lazy-seq
(when-let [s (seq coll)]
(concat s (cycle s)))))
@gtrak
gtrak / gist:4548456
Last active December 11, 2015 04:58
(ns xml.test
(:require [clojure.java.io :as io]
[clojure.data.xml :as xml]))
;; wget
;; download.geofabrik.de/openstreetmap/north-america/us/maryland.osm.bz2
(def xml-seq
#(-> (io/file "/home/gary/dev/maryland.osm")
io/input-stream
(defmacro let-map
"Avoids having to build maps from let bindings manually"
[bindings]
(let [names (->> (take-nth 2 bindings)
(tree-seq #(or (sequential? %) (map? %)) identity)
(filter symbol?)
(into #{})) ; dumps it all into a set
bindings (destructure bindings)]
`(let [~@bindings]
(zipmap [~@(map keyword names)]
(defmacro let-map
[pairs]
(let [names (map first (partition 2 pairs))]
`(let [~@pairs]
(zipmap
[~@(map (comp keyword name) names)]
[~@names]))))