Skip to content

Instantly share code, notes, and snippets.

@ray1729
ray1729 / cms.conf
Last active December 21, 2015 05:39
Example upstart script to start Ring web app
# cms - simple CMS
#
# The simple-cms server is a Compojure application serving content for cms.1729.org.uk
description "CMS server"
start on filesystem or runlevel [2345]
stop on runlevel [!2345]
respawn
(ns ring-of-primes
(:gen-class))
(defn prime?
[n]
(and (> n 1)
(not-any? #(zero? (mod n %)) (range 2 (inc (int (Math/sqrt n)))))))
(defn neighbours
[xs ring]
@ray1729
ray1729 / square_roots.clj
Created November 20, 2013 21:01
Benchmarking some different implementations of sqrt. Requires <https://github.com/hugoduncan/criterium> to run the benchmarks.
(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)
@ray1729
ray1729 / sliding-window-counter.clj
Created March 30, 2016 21:42
Simple time-based sliding window counter implementation in Clojure
(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]
ICounter
@ray1729
ray1729 / split.clj
Created December 9, 2016 08:55
Split on pred implemented by reducing with a state machine
(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
ray1729 / insert.clj
Created December 9, 2016 08:57
Insert before/after pred implemeted with split-with
(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)))
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
)