Skip to content

Instantly share code, notes, and snippets.

View teaforthecat's full-sized avatar

Chris Thompson teaforthecat

View GitHub Profile
(ns how-to.content
(:require [interlope.core :as i]))
(defn format-for
" Returns a function for easy access to I18N content.
Take this closure and pass it around, puff puff pass
example:
(let [f (format-for alerts :en)]
(f [:title] {:state-name \"MN\"}))"
[content-map lang state-abbrev]
(def state-specific-content
"Some states require just a little... extra."
{:en {
;; Alaska
:ak {:disclaimer "<p>Alaska will have an option to transmit absentee ballot applications electronically in 2020. You can request an absentee ballot application by electronic transmission until Monday, November 2, 2020. For more information, please visit <a href=\"http://www.elections.alaska.gov/Core/votingbyonline.php\">http://www.elections.alaska.gov/Core/votingbyonline.php</a></p>"}
;; California
:ca {:disclaimer "&nbsp;Conditional voter registration is a safety net for Californians who miss the deadline to register to vote or update their voter registration information. Voters can use the conditional voter registration process from the day after the deadline all the way through Election Day. Eligible citizens can go to their county election office or a designated satellite location to register and vote conditionally. These ballots will be processed once the county elections office
@teaforthecat
teaforthecat / gist:d250d355225133c57f1da2c3eb191616
Created February 11, 2019 23:07
lazy stream of kafka messages in clojure
(defn message-stream
([c]
(message-stream c ()))
([c a]
(lazy-seq
(cons a
(message-stream c (seq (poll! c)))))))
state (atom :continue)
;; state event new state
state-machine {:continue {:pause :paused
:halt :halting}
:paused {:resume :resuming
:halt :halting}
:resuming {:continue :continue}
:halting {:halt :halted}}
step-state (fn [transition]
@teaforthecat
teaforthecat / sql.clj
Created March 12, 2018 21:06
sql macro; finding a short path to named parameters
(ns sql)
(defn sub-qmark [i form]
(if (keyword? form)
['? form] ;;todo need a stateful transducer
form))
(defn params [body]
(map-indexed sub-qmark body))
@teaforthecat
teaforthecat / accumulate.clj
Last active February 19, 2018 23:01
fun with accumlating window transducer
(defn accumulate
"If an input satisfies ACC-PRED, accumulate it with ACC-FN (defaults to merge)
All inputs will be passed on normally.
All accumulated inputs will be merged (using ACC-FN) to following inputs until RESET-PRED.
The input that satisfies RESET-PRED will receive the accumulated values and the input following it will receive none.
The default ACC-FN, `merge`, assumes a stream of maps"
([acc-pred reset-pred]
(accumulate acc-pred reset-pred merge))
@teaforthecat
teaforthecat / in-many.clj
Created February 14, 2018 19:59
Hugsql custom parameter type to protect against Oracles maximum in clause items of 1000
(defmethod hugsql.parameters/apply-hugsql-param :in-many
[param data options]
(let [in-clause-split-by (get options :in-clause-split-by 1000) ;; 1000 is Oracle's max
singl-map (get options :singularize-map);; override singularize
values (get-in data (hugsql.parameters/deep-get-vec (:name param)))
singularize (fn [s] (string/replace s #"s$" ""))
column (get singl-map (:name param) (singularize (name (:name param))))
prefix (str column " in ")
join-or (str " or " column " in " )
@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
@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 / 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]