Skip to content

Instantly share code, notes, and snippets.

View oliyh's full-sized avatar

Oliver Hine oliyh

View GitHub Profile

Keybase proof

I hereby claim:

  • I am oliyh on github.
  • I am oliyh (https://keybase.io/oliyh) on keybase.
  • I have a public key ASBqyXdQrYvjtU8IfcLNJC61cKYjatjbrLgEHYEarpwVvgo

To claim this, I am signing this object:

@oliyh
oliyh / debounce.clj
Last active February 17, 2024 22:34
Debounce in Clojure on the JVM
(import '[java.util Timer TimerTask])
(defn debounce
([f] (debounce f 1000))
([f timeout]
(let [timer (Timer.)
task (atom nil)]
(with-meta
(fn [& args]
(when-let [t ^TimerTask @task]
@oliyh
oliyh / header.clj
Created November 23, 2018 09:37
Patterns for standalone data fetching subscriptions in re-frame
(require '[re-frame.core :as re-frame])
(require '[reagent.ratom :as reagent])
(require '[re-graph.core :as re-graph])
(require '[martian.re-frame :as martian])
;; handler for storing things when they arrive
(re-frame/reg-event-db
::on-things
(fn [db [_ things]]
(assoc db ::raw-things things)))
@oliyh
oliyh / comp-arrow.clj
Created March 24, 2017 19:58
comp functions that preserve additional arguments: comp-> and comp->>
(defn comp->>
"Like comp but multiple arguments are passed to all functions like partial, except the last which is threaded like ->>"
[f & fs]
(fn [& args]
(reduce (fn [r f']
(apply f' (conj (into [] (butlast args)) r)))
(apply f args)
fs)))
(defn comp->
@oliyh
oliyh / model.cljs
Last active August 2, 2016 12:05
Testing re-frame subscriptions with synchronous dispatch
(ns model
(:require [re-frame.core :as re-frame]))
(re-frame/def-event ::toggle-sort-direction []
(fn [db _]
(update db :sort-asc? not)))
(re-frame/def-sub ::sort-asc?
(fn [db _]
(get db :sort-asc? true)))
@oliyh
oliyh / clj-sse.clj
Last active September 19, 2024 14:37
Clojure client for Server Sent Events (SSE)
(require '[clj-http.client :as http])
(require '[clojure.core.async :as a])
(require '[clojure.string :as string])
(require '[clojure.java.io :as io])
(import '[java.io InputStream])
(def event-mask (re-pattern (str "(?s).+?\r\n\r\n")))
(defn- parse-event [raw-event]
(->> (re-seq #"(.*): (.*)\n?" raw-event)
@oliyh
oliyh / Material Design button group.md
Last active May 20, 2016 19:10
Material Design button with Bootstrap-like dropdown functionality
@oliyh
oliyh / image-url-to-data-uri.js
Created November 7, 2015 22:17
Convert an image url to a data URI without canvas
// hard won knowledge from http://stackoverflow.com/questions/20035615/using-raw-image-data-from-ajax-request-for-data-uri
var xmlHTTP = xhr.XMLHttpRequest();
xmlHTTP.open('GET', url, true);
xmlHTTP.responseType = 'arraybuffer';
xmlHTTP.onload = function(e) {
var arr = new Uint8Array(this.response);
var raw = String.fromCharCode.apply(null,arr);
var b64 = base64.encode(raw);
var dataURL="data:image/png;base64," + b64;
};
@oliyh
oliyh / async-handler.clj
Created October 6, 2015 18:33
Pedestal interceptor which allows you to return core.async channels from defhandlers
(swagger/defafter async-handler
{:description "Wraps asynchronous responses into the context (lets you use defhandler)"}
[context]
(if (satisfies? clojure.core.async.impl.protocols/Channel (:response context))
(a/pipe (:response context) (a/chan 1 (map #(assoc context :response %))))
context))
{"swagger":"2.0","info":{"title":"Swagger API","version":"0.0.1"},"produces":["application/json"],"consumes":["application/json"],
"paths":{"/ab":{"get":{"parameters":[{"in":"body","name":"SchemaB","description":"","required":true,"schema":{"$ref":"#/definitions/SchemaA"}}],"
responses":{"default":{"description":""}}}},
"/cb":{"get":{"parameters":[{"in":"body","name":"SchemaB","description":"","required":true,"schema":{"$ref":"#/definitions/SchemaC"}}],
"responses":{"default":{"description":""}}}}},
"definitions":{"SchemaA":{"type":"object","properties":{"a":{"type":"string"}},"required":["a"]},