Skip to content

Instantly share code, notes, and snippets.

View pbaille's full-sized avatar
🏠
Working from home

Pierre Baille pbaille

🏠
Working from home
  • Freelance
  • France
View GitHub Profile
@pbaille
pbaille / midi-parser.clj
Last active April 10, 2020 09:09
midi-parser in Clojure
(ns midi-parser.core
(:use clojure.java.data)
#_(:use utils.utils)
#_(:use vendors.debug-repl)
(:import (java.io File)
#_(java.util Arrays)
#_(java.nio ByteBuffer)
(javax.sound.midi MidiSystem Sequence MidiMessage MidiEvent ShortMessage MetaMessage Track)))
;***************** Utils ********************
(ns grayscott.core
"ported from toxiclibs by Karsten Schmidt
https://bitbucket.org/postspectacular/toxiclibs/src/44d9932dbc9f9c69a170643e2d459f449562b750/src.sim/toxi/sim/grayscott/GrayScott.java?at=default&fileviewer=file-view-default"
#?(:clj (:require [thi.ng.math.macros :as mm])
:cljs (:require-macros [thi.ng.math.macros :as mm])))
(defn clip [min max n]
(cond
(< n min) min
(> n max) max
@pbaille
pbaille / rwrap.clj
Last active December 6, 2016 19:03
reactive wrapper
(defn rwrap [build deps-map]
(let [this (atom (build))
deps (keys deps-map)
propagating (atom nil)]
(add-watch this
:build
(fn [_ _ _ n]
(when-not @propagating
(reset! propagating true)
(doseq [[dep upd] deps-map]
(ns rum-slave
(:require-macros [cljs.core.async.macros :refer [go-loop go]])
(:require [cljs.core.async :as async :refer [chan >! <!]]
[rum.core :as rum]))
;; helpers ---------------------------------------------------------------
(defn notify [s m]
((:notify s) m))
(ns type.exp
(:refer-clojure :exclude [extend-type reify])
;; state ---------------------
(def types (atom {::any identity}))
(def facets (atom {}))
;; helpers -------------------
@pbaille
pbaille / rate-limitor.clj
Last active December 27, 2016 15:55
rate limitor sketch
(def state (volatile! {}))
(def c (chan))
(defn add-bucket! [id {:as opts
:keys [ttl len]
:or {ttl 1000 len 10}}]
(vswap! state
assoc
id
{:ttl ttl
(defn set-timeout [f millis]
(let [running (atom true)]
(future
(while @running
(f)
(Thread/sleep millis)))
#(reset! running nil)))
;; start an interval, the stop function is returned and bound to stop-interval var
(def stop-interval (set-interval #(println "yop") 1000))
@pbaille
pbaille / qparamsParser.js
Created January 6, 2017 09:10
query params to hashmap
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
if (decodeURIComponent(pair[0]) == variable) {
return decodeURIComponent(pair[1]);
}
}
console.log('Query variable %s not found', variable);
(require '[reagent.core :as r])
(defn i []
(let [dims (r/atom {})
this (atom nil)
resized? (atom nil)]
(fn [][:div
[:img {:src "http://www.lispcast.com/img/pre-conj/rich-hickey.jpeg"
:ref (fn [x] (reset! this x))
:on-load
;; Transparent Functions
;; I was experimenting with transducers and wanted a way to understand how they worked. Transducer
;; code uses many nested functions in various locations with other nested functions defined as local
;; variables in scope. Typically after an anonymous Clojure function is defined you have no visibility
;; into the locals that were in scope when the function was defined, where the function came from,
;; or the code in the function. I defined a macro, tfn, that creates a transparent function. It's
;; a normal Clojure function with additional metadata including the function code and local
;; variable names and values.