Skip to content

Instantly share code, notes, and snippets.

(require '[clojure.core.async :as ca])
(def some-ch (ca/chan))
(def result-ch (ca/reduce
(fn [xs msg]
(if (= msg :done)
(reduced xs)
(conj xs msg)))
[]
some-ch))
@shaunxcode
shaunxcode / gist:6646155
Created September 21, 2013 01:35
idiomatic way to apply a function with various arities
;;say you have something that looks like
(fnx a b)
(fnx a b2)
(fnx a b c)
(fnx a1 b2 c)
(fnx a2 b2 c2 d)
;;my goal is to avoid the repetition of fnx
@shaunxcode
shaunxcode / datomic-query-util.clj
Last active December 20, 2015 00:39
utilities for going between vector and map form of datomic query
(def query '[:find ?e ?fname ?lname
:in $ ?fname ?lname
:where [?e :user/firstname ?fname]
[?e :user/lastName ?lname]])
(defn group-by-kw [v]
(map (fn [[[f] l]] (conj [f] l))
(partition 2 (partition-by keyword? v))))
(defn query-vec-as-map [vquery]
@shaunxcode
shaunxcode / gist:5833985
Created June 21, 2013 20:12
datomic excise ns
[:find ?e ?ident
:where [?e :db/ident ?ident]
[(namespace ?ident) ?ns]
[(= ?ns "db.excise")]]
@shaunxcode
shaunxcode / gist:5811307
Created June 19, 2013 02:39
sweet use of jquery promises
$.when.apply(null, (ajaxRequest p for p in items)).done ->
console.log "all ajaxRequests complete"
CodeMirror.extendMode("clojure", {
newlineAfterToken: function(type, content, textAfter) {
console.log(type, content, textAfter);
return /[\(\{\[].+\s/.test(content);
}
});
// Applies automatic mode-aware indentation to the specified range
CodeMirror.defineExtension("autoIndentRange", function (from, to) {
var cmInstance = this;
@shaunxcode
shaunxcode / gist:3869104
Created October 10, 2012 23:02
fetch all attributes for a namespace in datomic
[:find ?id ?name ?attr
:in $ ?in-ns
:where [?id :db/ident ?name]
[(name ?name) ?attr]
[(namespace ?name) ?ns]
[(= ?ns ?in-ns)]]
@shaunxcode
shaunxcode / gist:3795605
Created September 27, 2012 18:34
query info for datomic
;;takes datomic query and turns it into map so it may be interrogated
(defn query-info [query]
(let [parts (partition-by keyword? query)
is-kw #(keyword? (first %))]
(zipmap
(map first (filter is-kw parts))
(remove is-kw parts))))
;;eg
((query-info '[:find ?x ?y ?z :in $ ?d :where [?x :some/thing ?y] [?x :some/otherthing ?z]]) :find)
@shaunxcode
shaunxcode / gist:2929101
Created June 14, 2012 08:42
quick coffeescript jquery dsl
String.prototype.$tag = (args...) ->
args.unshift "<#{@toString()}/>"
$.apply window, args
("ul".$tag
class: "someUL"
html: "li".$tag
text: "some text in an li"
class: "some class"
click: ->
@shaunxcode
shaunxcode / gist:2887304
Created June 7, 2012 08:00
keyword dispatch/doesNotUnderstand in coffeescript
#this is absolutely just for fun
keyWordDispatcher = (obj, msg) ->
selectorName = ""
args = []
for k, v of msg
selectorName += k + "_"
args.push v
if obj[selectorName]?