Skip to content

Instantly share code, notes, and snippets.

package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
)
@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)))
@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 / 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 / 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)
(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 / 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
@ray1729
ray1729 / tweet.clj
Created June 29, 2013 22:18
Sending tweets from Clojure
(require '[clojure.java.io :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."
[resource-name]
(when-let [resource (io/resource resource-name)]
(let [properties (java.util.Properties.)]
(with-open [stream (io/input-stream resource)]
@ray1729
ray1729 / partitions.clj
Created June 21, 2013 11:27
Using Clojure to compute partitions of a set
(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
ray1729 / gist:5755484
Created June 11, 2013 09:03
Find 4-9 letter words in a file.
(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))))