clojure

Easy d3 tooltips in Clojure reagent

Working with d3js I found myself in need of a tooltip to explicate data as it is explored in a chart. The best way to use d3 with Reagent (react.js) is to use reagent to handle the DOM manipulations and d3 to handle more elaborate visualizaion calculations. We don’t follow the d3 model of adding an event to every dom element to trigger a tooltip item to be created, which duplication would behave poorly with React state maintenance.

Leveraging Clojure-power for SQL generation with HoneySQL

One of the most meaningful testaments to Clojure’s data-driven and lisp-powered design is the ability to use it to refine any of its transpiling processes, here SQL generation (elsewhere route generation, CSS generation, and of course its actual hosted languages, including Javascript or Java generation*). In this post I was tasked with refactoring a 3-arg function that reads from the database into a map-taking function that works with one, two, or three items in the map.

Quickly Creating DB namespaces with shell, emacs, and init.sql

My project design includes creating a file/namespace for each database table, with a suite of CRUD operations applying to each table. The end result is that I have functions like db.my-table/CREATE, READ, UPDATE, and DELETE available for each table. The strategy is as follows: Establish the template clj file that has all your CRUD operations Obtain all table names (e.g. from init.sql) Use a script to create a matching clj file based on template (1) customized to refer to the tables from (2) 1.

My Garden CSS has ascended

I’ve been continually seeking the answer to Garden CSS’s title question: “what’s possible when you trade a preprocessor for a programming language?” I have used Garden exclusively for years, happily wielding garden-gnome to implement hot-loading on my front-end for an excellent development experience. Writing CSS with Clojure data-structures is a hands-down win over raw css-writing, and Garden also gives useful shortcuts that some of the preprocessors also have like lighten, darken, & selectors, and more.

A Gotcha: test.core/thrown?

https://clojureverse.org/t/a-gotcha-test-core-thrown/5595/1 Clojure is generally such a pristine and sensible language, it took me some debugging time to find out why this was failing: (is (let [proctorless-rmap {:tester (:id tester) :test (:id this-test)}] (thrown? Exception (xreg/register-for-exam proctorless-rmap)) "no proctor should fail")) ;; Unable to resolve symbol: thrown? in this context Trying to solve this at the repl, I was stumped for quite a while by why I couldn’t locate the thrown? function with C-c C-.

How to Save an Emacs Keyboard Macro Permanently

Courtesy of gnu manual, we can permanently save a keyboard macro for future use: https://www.gnu.org/software/emacs/manual/html_node/emacs/Save-Keyboard-Macro.html The steps are as follows: perform macro (start with f3, then do your stuff and return to where you started, then stop recording with <f4>) name that macro kmacro-name-last-macro insert macro code into buffer insert-kbd-macro <RET> macro-name I want the ability to go into a let-form and, in the spirit of REPL debugging and development, bind the X VAL part of a (let [ .

Sweet Honeysql and Postgres

I thought I’d share a snippet of what I love about my HoneySQL + Postgres setup (this is plain Honey without the Postgres extras, which never seem to cover my needs). I don’t take any pride in the actual database model, which is in-use and was not well designed. Nonetheless, the two things that worked out nicely here are: table applications has a one-to-many relationship with validations, so PG’s “distinct on” was the perfect tool for just matching the most recent of an application’s validations.

Extending HoneySQL with Array Intersection

Turns out that Postgres has beautiful support for “array” field types. In one database of mine, I use this to keep track of the recipients of emails: DROP TABLE IF EXISTS "email_log" CASCADE; CREATE TABLE "email_log" ( id SERIAL PRIMARY KEY, sender TEXT NOT NULL, recipients TEXT[] NOT NULL, message TEXT, sent_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, info JSONB); -- Note the recipients TEXT[] definition: that's an array of texts. Now for the use-case: a user might have multiple email addresses, and the email_log tracks recipient ADDRESSES not user-ids.

Extending HoneySQL with JSON operators

HoneySQL is a wonderful Clojure library that lets use the full power of Clojure collections to generate your SQL statements. Postgres supports json operators and data types, which is one of the main features for which I use it; however, since this is a non-SQL-standard feature, HoneySQL doesn’t support it directly, and I didn’t see that support in HoneySQL-Postgres yet. Fortunately, HoneySQL makes it easy to extend this way. (ns myapp.

Book Review: Clojure Cookbook: Recipes for Functional Programming

Clojure Cookbook: Recipes for Functional Programming by Luke VanderHart My rating: 4 of 5 stars An excellent book, not only as a reference but as a learning resource. It is by no means written as a general entry point to Clojure, but provides terse, practical, fast-paced insight with just enough explanation to be as instructive as it is useful. The quality of the “discussion” portions means this is good for more than just copy-paste reading, while the emphasis on projects and practicality make it a pre-eminent go-to as you refine your Clojure skills.