Skip to content

Instantly share code, notes, and snippets.

View teaforthecat's full-sized avatar

Chris Thompson teaforthecat

View GitHub Profile
@teaforthecat
teaforthecat / gist:d5921a1516f27877ddda
Created July 13, 2014 01:50
broken upsert transaction
;; implicit upsert due to unique constraint on message
;; temp id will get replaced with real id if message exists
(defn upsert-message-tx [greeting]
(let [tempid (d/tempid :db.part/user -1 )]
[{ :db/id tempid
:greeting/message greeting}
[:inc tempid :greeting/hit-count 1]]))
@teaforthecat
teaforthecat / gist:85c518b3164445602334
Last active August 29, 2015 14:23
neat idea to bookmark commands
;; not working: service is not found in services;
(let ((service (prodigy-define-service
:name "* head *"
:cwd "~/.emacs.d/lisp"
:command "head"
:args '("my-functions.el")
))
)
(prodigy-start-service service))
@teaforthecat
teaforthecat / comment-or-uncomment-sexp.el
Created October 18, 2015 18:10
one function with a few lines addded to keep the structure of lisp forms when commenting a form inside another form
(defun comment-sexp--raw ()
"Comment the sexp at point or ahead of point."
(pcase (or (bounds-of-thing-at-point 'sexp)
(save-excursion
(skip-chars-forward "\r\n[:blank:]")
(bounds-of-thing-at-point 'sexp)))
(`(,l . ,r)
(goto-char r)
(skip-chars-forward "\r\n[:blank:]")
(save-excursion
@teaforthecat
teaforthecat / clj-kafka-test.clj
Last active December 2, 2015 04:44
trying to fetch data from kafka, not being able to use "take"
(defn consumer-config [] {"zookeeper.connect" "localhost:2182"
"group.id" (str (java.util.UUID/randomUUID))
"auto.offset.reset" "smallest"
"auto.commit.enable" "false"})
(defn fetch-first [topic]
(with-resource [c (zk/consumer (consumer-config))]
zk/shutdown
(first (zk/messages c topic))))
@teaforthecat
teaforthecat / jobs.clj
Created December 18, 2015 03:30
An onyx job builder, basically a shortcut to this: kafka-input->function->kafka-output
(ns bones.jobs
" given a symbol and config, build all components of an onyx job
with three tasks kafka-input->function->kafka-output
(def x.y/fn [s] (str s \"-yo\"))
(api/submit-jobs (bones.jobs/build-jobs {} [:x.y/fn]))
(kafka/produce \"x.y..fn-input\" \"hello\")
(kafka/consume \"x.y..fn-output\") => \"hello-yo\"
")
(defn topic-reader [^String topic]
@teaforthecat
teaforthecat / personal_consumer.clj
Last active December 26, 2015 14:19
Consume a kafka message stream of filtered keys
(def consumer-registry (atom {}))
(defn get-or-create-message-stream [topic]
(if-let [message-stream (get @consumer-registry topic)]
message-stream
(let [[cnsmr messages] (kafka/open-consumer "consumer-registry" topic)
new-message-stream (ms/->source messages)]
;; create lifespan for consumer of 1 minute, should probably be a component
(a/go (a/<! (a/timeout 60e3 ))
(kafka/shutdown cnsmr)

Keybase proof

I hereby claim:

  • I am teaforthecat on github.
  • I am teaforthecat (https://keybase.io/teaforthecat) on keybase.
  • I have a public key ASB1dfXKtsulaHTXHgRPQC4pzMzpu2CbqIJGJOEw2whMcwo

To claim this, I am signing this object:

@teaforthecat
teaforthecat / bones-editable-concept.clj
Created January 5, 2017 03:31
example showing intent of bones-editable library
(defn detect-controls [{:keys [enter escape]}]
(fn [keypress]
(case (.-which keypress)
13 (enter)
;; chrome won't fire 27, so use on-blur instead
27 (escape)
nil)))
(defn field [form-type identifier attr html-attrs]
(let [path [:editable form-type identifier :inputs attr]
@teaforthecat
teaforthecat / collate_transducer.clj
Last active February 5, 2018 02:43
a descriptive experiment making a clojure transducer
(defn collate
"This is a transducer for collecting things that are connected in series. It
will store one previous input. The previous input can then be compared to it's
successor using COMPARES-FN and optionally integrated into a single output
using COLLATE-FN.
COMPARES-FN must accept 2 arguments, being 2 inputs (which are in order)
COLLATE-FN must accept 2 arguments, being an accumulator and an input
COLLATE-FN is a reducing function. A third arg can be given as the starting
@teaforthecat
teaforthecat / real_date.clj
Created February 13, 2018 17:01
Protect against hard to catch date corruption
(defn date?
"Ensure a date string is a date and after a cutoff date. This protects against technically valid but corrupt dates like from a century ago or in the future."
([s]
;; store the year 10 years from now so we don't have to compute it every call
(date? s 2010 `~(+ 10 (clj-time.core/year (clj-time.core/now)))))
([s past-cutoff future-cutoff]
(try
(if-let [d
(clj-time.format/parse