Skip to content

Instantly share code, notes, and snippets.

@devn
devn / recursive-transient-persistent-transform.clj
Last active December 5, 2022 15:02
Recursively transform a nested map into transients, and then back to persistents
(require '[clojure.walk :as walk])
;; => nil
(defn transient? [x]
(instance? clojure.lang.ITransientCollection x))
;; => #'user/transient?
(let [transients (walk/postwalk (fn [x]
(if (map? x)
(transient x)
@devn
devn / quick-pr-str.clj
Created December 1, 2022 22:03
race car go vroom
(defn quick-pr-str
([data]
(let [acc (StringBuilder.)]
(quick-pr-str acc data)
(.toString acc)))
([^StringBuilder acc data]
(cond
(string? data)
(do
(.append acc \")
javascript: (function () {
function c() {
var e = document.createElement("link");
e.setAttribute("type", "text/css");
e.setAttribute("rel", "stylesheet");
e.setAttribute("href", f);
e.setAttribute("class", l);
document.body.appendChild(e)
}
function h() {
@devn
devn / midijam.clj
Created September 4, 2015 23:47
playing midi notes from overtone to ableton, reason, etc. using MIDI Patchbay
;; http://notahat.com/midi_patchbay/
;; Create a virtual input and virtual output in MIDI patchbay
;; input name: "Overtone"
;; output name: "yourchoice"
;; In Reason, Ableton, etc. select "yourchoice"
;; as the midi input for a channel.
(ns jam.core
(:use [overtone.live]))
@devn
devn / day05.clj
Created December 6, 2020 01:52
Day 5, Advent of Code 2020
(ns day05
(:require [clojure.string :as str]
[clojure.set :as set]))
(def boarding-passes "...")
(defn find-pos [ops size lower-op upper-op]
(loop [ops ops
xs (range 0 (inc size))]
(if (seq ops)
@devn
devn / autotune.clj
Created July 15, 2014 17:48
autotune/autocorrect pitch overtone
(defsynth pitch-follow-1 []
(let [in (mix [(sound-in)])
amp (amplitude:kr in 0.05 0.05)
[freq has-freq] (pitch:kr in
:amp-threshold 0.02
:median 7)
out-1 (mix [(var-saw:ar (mul-add:ar 0.5 1 2)
0
(lf-noise1:kr (mul-add:kr 0.3 0.1 0.1)))])
out-2 (loop [n 6
@devn
devn / cheating.clj
Created September 5, 2014 21:40
Implementing IFn
(defn gen-nonvariadic-invokes [f]
(for [arity (range 1 21),
:let [args (repeatedly arity gensym)]]
`(~'invoke [~@args] (~f ~@args))))
(defn gen-variadic-invoke [f]
(let [args (repeatedly 22 gensym)]
`(~'invoke [~@args] (apply ~f ~@args))))
(defn gen-apply-to [f]
@devn
devn / clojure_zippers.clj
Last active September 11, 2019 18:51
clojure zippers, returning a path that can be used with update-in or get-in, has alternate versions of update-in and assoc-in which work with clojure sets
(require '[clojure.zip :as z])
(defn update-in*
"Like update-in, but also works with nested sets."
[m ks f & args]
(let [up (fn up [m ks f args]
(let [[k & ks] ks]
(if ks
(if (set? m)
(conj (disj m k) (up (get m k) ks f args))
(ns coinstar
(:import [java.time LocalDate]))
(def working? (constantly false))
(defn go
"Provided a `who` and `when`, returns the state of
a coinstar `when` the `who` goes to it.
Example:
@devn
devn / numbers-to-english.clj
Created January 27, 2012 22:31
numbers to english in clojure
(ns quiz.core
(:require [clojure.pprint :as pp]))
(defn to-english [n]
(pp/cl-format nil "~@(~@[~R~]~^ ~A.~)" n))
(to-english 99999999999999999)
;=> "Ninety-nine quadrillion, nine hundred ninety-nine trillion, nine hundred ninety-nine billion, nine hundred ninety-nine million, nine hundred ninety-nine thousand, nine hundred ninety-nine"
(map to-english (range 0 101))