Skip to content

Instantly share code, notes, and snippets.

didibus / custom_function_creating_tagged_literals.clj
Last active Jan 5, 2021
Some Clojure tagged literals to help create functions in various ways, such as create a function that discards any argument passed to it.
View custom_function_creating_tagged_literals.clj
;; This one will create a function that takes 0 or more args and discards them all (doesn't use them).
(defn &-tag-reader
[[f & args]]
`(fn [~'& ~'args] (~f ~@args)))
(set! *data-readers* (assoc *data-readers* '& user/&-tag-reader))
(mapv #&(rand-int 5) (range 10))
;;=> [3 0 4 0 2 0 4 4 4 2]
didibus / control_pmap_concurrency_level_with_rechunking.clj
Last active Jan 5, 2021
Example to control Clojure pmap concurrency level using the chunk size of lazy-seqs.
View control_pmap_concurrency_level_with_rechunking.clj
;; From:
(defn re-chunk [n xs]
(when-let [s (seq (take n xs))]
(let [cb (chunk-buffer n)]
(doseq [x s] (chunk-append cb x))
(chunk-cons (chunk cb) (re-chunk n (drop n xs)))))))
;; Simply wrap the collection or seq/lazy-seq with re-chunk and the configured chunk size will make pmap's concurrency level grow to the size of the chunk.
(time (dorun (pmap (fn[e] (Thread/sleep 100) (inc e)) (re-chunk 100 (range 1000)))))
didibus / agent_concurrently_filter_api_call.clj
Created Jan 5, 2021
Use Clojure agents to concurrently filter out elements from a collection which requires calling a remote API to know if the element should be filtered or not.
View agent_concurrently_filter_api_call.clj
(defn api-pred [e]
(Thread/sleep 100)
(even? e))
(let [coll-to-process (range 1000)
concurrency 100
agents (repeatedly concurrency #(agent []))]
(doseq [[i agnt] (map vector coll-to-process (cycle agents))]
(send-off agnt
#(if (api-pred i)
didibus / #letnoshadow.clj
Last active Oct 30, 2020
A Clojure macro which gives you a let that does not allow shadowing of symbols already declared.
View #letnoshadow.clj
didibus / user.clj
Created Jan 29, 2020
A Clojure user.clj file which lets you lazy load certain namespace at the REPL
View user.clj
(def safe-requires
"List of namespaces to require and refer when inside user ns at load time.
Can be given an initialization body to execute after having been required.
To do so, wrap the lib spec in a vector, and all elements after the lib
spec vector will be evaled after the lib spec has been required."
'[[clojure.repl :as repl :refer (source apropos dir pst doc find-doc)]
[ :as javadoc :refer (javadoc)]
[clojure.pprint :as pprint :refer (pp pprint)]
[clojure.stacktrace :as stacktrace :refer (e)]
didibus / #datafy-nav-example
Last active Oct 30, 2020
Clojure datafy/nav example
View #datafy-nav-example
didibus / defnk.clj
Last active Nov 3, 2018
Clojure macro to define strict keyword argument functions
View defnk.clj
(defmacro defnk
[name & fdecl]
(let [doc-string? (when (string? (first fdecl))
(first fdecl))
fdecl (if (string? (first fdecl))
(next fdecl)
attr-map? (when (map? (first fdecl))
(first fdecl))
fdecl (if (map? (first fdecl))
didibus / reagent-counter.cljs
Last active Jul 16, 2018
A simple counter implemented in ClojureScript using reagent as the only dependency.
View reagent-counter.cljs
(ns counter.core
(:require [reagent.core :as r]))
;;;; Utils
(defn ratom? [a]
(= reagent.ratom/RAtom (type a)))
(defn make-state [state]
didibus / #clojurescript-counter
Last active Oct 30, 2020
Example of a simple ClojureScript implementation of a counter, using no dependencies.
View #clojurescript-counter
didibus / abstract-factory.clj
Created Apr 13, 2017
My attempt at what the abstract factory pattern would look like in Clojure
View abstract-factory.clj
(defn draw-ui [{:keys [label casing content]}]
(let [pad (Math/floor (/ (- (Math/max (count label) (count (or (:label content) "")))
(+ 2 (Math/min (count label) (count (or (:label content) "")))))
(println (str "|" (if (= :upper casing)
(.toUpperCase label)
(.toLowerCase label)) "|" \newline
"|" (reduce (fn [a b] (str a " ")) "" label) "|" \newline
(when content
(str "|" (apply str (repeat pad \space))