Skip to content

Instantly share code, notes, and snippets.

@hadronzoo
hadronzoo / replace-hash-rockets.el
Last active August 29, 2015 13:56
Replace hash rockets with Ruby 1.9 symbol keys
;; Replace hash rockets with Ruby 1.9 symbol keys
(defun replace-hash-rockets ()
(interactive)
(goto-char 1)
(while (search-forward-regexp ":\\(\\(\\w\\|_\\)+\\) =>" nil t)
(replace-match (concat (match-string 1) ":") t nil)))
@hadronzoo
hadronzoo / char_counts.clj
Last active August 29, 2015 14:00
Select low and high character frequencies, choosing the lowest code point in case of a tie
(def input "bbbddddaaacccc")
(defn- lowest-code-point [coll]
(first (into (sorted-map-by #(< (int %1) (int %2))) coll)))
(defn- take-while= [value coll]
(take-while (fn [[_ v]] (== value v)) coll))
(let [counts (frequencies input)
sorted (sorted-map-by
(defn minmax-by-codepoint [s]
(let [sorted (reduce (fn [r [c cnt]]
(update-in r [cnt] #((fnil conj (sorted-set)) % c)))
(sorted-map)
(frequencies s))
get-char (juxt (comp first second) first)]
[(get-char (first sorted))
(get-char (last sorted))]))
@hadronzoo
hadronzoo / core.clj
Last active August 29, 2015 14:02
core.typed.async error
(ns test.core
(:require [clojure.core.async :refer [<! >!]]
[clojure.core.typed :refer [ann U]]
[clojure.core.typed.async :refer [Chan go>]]))
(ann test-go [(Chan Integer) -> (U (Chan Integer) nil)])
(defn test-go [in] (go> (<! in)))
@hadronzoo
hadronzoo / jvm_cursor.clj
Created November 5, 2014 23:03
Clojure MapCursor
(ns cursors.core
(:refer-clojure :exclude [swap! reset!]))
(defprotocol ICursor
(-path [c])
(-state [c]))
(defprotocol IToCursor
(-to-cursor [c state path]))
@hadronzoo
hadronzoo / arity.clj
Last active August 29, 2015 14:14
No matching arities
(ns tctest.core
(:require [clojure.core.typed :as t]))
(t/defprotocol AProtocol
(some-method [this] :- t/AnyInteger
[this arg :- t/Any] :- t/AnyInteger))
(t/ann-datatype AType [])
(deftype AType []
AProtocol
@hadronzoo
hadronzoo / example.log
Last active April 2, 2016 15:54
Generator Example
g = generator(0)
=> [Function]
g()
=> 0
g()
=> 1
g()
=> 2
@hadronzoo
hadronzoo / contiguous-xf.clj
Created November 2, 2016 17:59
Partition contiguous groups of integers
(ns xf.contiguous
(:require [clojure.data.int-map :as i]))
(defn partition-contiguous
([]
(let [buffer (volatile! (i/dense-int-set))]
(fn [xf]
(fn
([] (xf))
([result]
@hadronzoo
hadronzoo / watermark-example.clj
Last active November 4, 2016 15:48
Emit watermark changes — useful for keeping track of Kafka partition log offsets
(let [partition (juxt :topic :partition)
raise? #(case (:operation %)
:fetch false
:ack true
(throw (ex-info "unknown operation" %)))
level :offset
operations [{:topic "test" :partition 0 :operation :fetch :offset 0}
{:topic "test" :partition 0 :operation :fetch :offset 1}
{:topic "test" :partition 0 :operation :ack :offset 1}
{:topic "test" :partition 1 :operation :fetch :offset 0}
@hadronzoo
hadronzoo / flink-statsd-mapping.conf
Created May 11, 2017 21:09
statsd-exporter configuration for exporting Flink statsd metrics to Prometheus