Skip to content

Instantly share code, notes, and snippets.

View jclaggett's full-sized avatar

Jonathan Claggett jclaggett

View GitHub Profile
(ns ezducerdemo)
;;
;; 1st take
;;
(defn ezducer [constructor]
(fn [reducer]
(let [steps (fn [a vs]
;; Use loop/recur instead of reduce to avoid reduce's
@jclaggett
jclaggett / xflib.cljs
Created November 12, 2022 18:23
demultiplex in clojure
(ns xflib)
(defn demultiplex [n]
(if (= n 1)
identity
(let [expected-result-calls* (atom n)
shared-reducer* (atom nil)
reduced-value* (atom nil)]
(fn [r]
(if-let [shared-reducer @shared-reducer*]
(ns squarespec
(:require [clojure.spec :as s]
[clojure.spec.gen :as sgen]
[clojure.spec.test :as stest]))
;; Implementation
(defn square
"Define a square path given an origin and length."
[[x1 y1 :as origin] length]
(let [x2 (+ x1 length)
@jclaggett
jclaggett / deftype+.clj
Created March 10, 2014 13:32
:delegate option for deftype
(ns deftype+
"Augmented deftype sporting a new :delegate option.")
;; code to get the methods of interfaces and protocols
(defmulti get-methods
"Return a map of all method names to their arity."
class)
(defmethod get-methods clojure.lang.PersistentArrayMap
[protocol]
(defn sparkline [values]
(let [low (apply min values)
high (apply max values)
bars "▁▂▃▄▅▆▇█"
step (/ (- high low) (dec (count bars)))
scale #(nth bars (int (/ (- % low) step)))]
(apply str (map scale values))))
@jclaggett
jclaggett / context.sh
Created April 14, 2011 20:24
context switcher tool
#!/bin/bash -e
status_icon_000="/usr/share/icons/Humanity/apps/48/stock_delete-bookmark.svg"
status_icon_100="/usr/share/icons/Humanity/apps/48/stock_bookmark.svg"
function context_loop() {
declare time_task
while time_task=$(context_switch_dialog $*)
do
set $time_task # reference using positional args
(defn conjoin [& preds]
(fn [& args]
(let2
% (apply juxt preds)
% (map % args)
% (flatten %)
% (every? identity %))))
(defn disjoin [& preds]
(fn [& args]
(defmacro let2
"Alternate let block that doesn't have a body, just bindings. The last binding is returned."
([] nil)
([& bindings]
`(let [~@bindings] ~(first (take-last 2 bindings))) ) )
#!/bin/bash -e
new_status()
{
gajim-remote change_status "$@"
}
lock_screen()
{
gnome-screensaver-command --lock
(defmacro topic
([t] nil)
([t form] form)
([t form & more]
`(let [~t ~form] (topic ~t ~@more))) )
(def topic-ex1
(topic % 1
(+ 1 %)
(/ % 2) ) )