Skip to content

Instantly share code, notes, and snippets.

View domino.go
package main
import (
ray1729 / insert.clj
Created Dec 9, 2016
Insert before/after pred implemeted with split-with
View insert.clj
(defn insert-after
[pred coll x]
(let [[before after] (split-with (complement pred) coll)]
(concat before (take 1 after) x (drop 1 after))))
(defn insert-before
[pred coll x]
(let [[before after] (split-with (complement pred) coll)]
(concat before x after)))
ray1729 / split.clj
Created Dec 9, 2016
Split on pred implemented by reducing with a state machine
View split.clj
(defn split-after
[pred coll]
(let [post-accum (fn [accum x]
(update accum :after conj x))
pre-accum (fn [accum x]
(cond-> (update accum :before conj x)
(pred x) (assoc :f post-accum)))]
((juxt :before :after)
(reduce (fn [{:keys [f] :as accum} x] (f accum x))
{:before [] :after [] :f pre-accum}
ray1729 / sliding-window-counter.clj
Created Mar 30, 2016
Simple time-based sliding window counter implementation in Clojure
View sliding-window-counter.clj
(ns sliding-window-counter.core
(:refer-clojure :exclude [inc]))
(defprotocol ICounter
(reset [this])
(inc [this])
(value [this]))
(defrecord SlidingWindowCounter [window-size bucket-size num-buckets buckets]
ray1729 / square_roots.clj
Created Nov 20, 2013
Benchmarking some different implementations of sqrt. Requires <> to run the benchmarks.
View square_roots.clj
(ns square-roots
(:require [criterium.core :refer [bench]]))
(defn square [x] (* x x))
(defn abs [x] (if (< 0 x) (- x) x))
(defn sqrt-repeated-average
[x lo hi tolerance]
(if (<= (- hi lo) tolerance)
View ring_of_primes.clj
(ns ring-of-primes
(defn prime?
(and (> n 1)
(not-any? #(zero? (mod n %)) (range 2 (inc (int (Math/sqrt n)))))))
(defn neighbours
[xs ring]
ray1729 / cms.conf
Last active Dec 21, 2015
Example upstart script to start Ring web app
View cms.conf
# cms - simple CMS
# The simple-cms server is a Compojure application serving content for
description "CMS server"
start on filesystem or runlevel [2345]
stop on runlevel [!2345]
ray1729 / tweet.clj
Created Jun 29, 2013
Sending tweets from Clojure
View tweet.clj
(require '[ :as io])
(require '[oauth.twitter :refer [oauth-client]])
(defn read-properties
"Parse a properties file, convert the property keys to Clojure
keywords and return as a Clojure map."
(when-let [resource (io/resource resource-name)]
(let [properties (java.util.Properties.)]
(with-open [stream (io/input-stream resource)]
ray1729 / partitions.clj
Created Jun 21, 2013
Using Clojure to compute partitions of a set
View partitions.clj
(defn expand-partition
"Given a partition of size n and an element, generate n new
partitions by adding `element` to each subset in turn."
[partition element]
(let [partition (vec partition)]
(for [i (range (count partition))]
(set (update-in partition [i] #(conj % element))))))
(defn partitions
"Return all partitions of the set `X` into `n` non-empty subsets.
ray1729 / gist:5755484
Created Jun 11, 2013
Find 4-9 letter words in a file.
View gist:5755484
(defn filter-file [filename]
  (with-open [rdr (io/reader filename)]
    (reduce (fn [words line]
              (into words (filter #(<= 4 (count %) 9) (str/split line #"\s+"))))
            (line-seq rdr))))