Skip to content

Instantly share code, notes, and snippets.

View ring_of_primes.clj
(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 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 cms.1729.org.uk
description "CMS server"
start on filesystem or runlevel [2345]
stop on runlevel [!2345]
respawn
@ray1729
ray1729 / tweet.clj
Created Jun 29, 2013
Sending tweets from Clojure
View tweet.clj
(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 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
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))))
@ray1729
ray1729 / gist:4228815
Created Dec 6, 2012
My first parsatron
View gist:4228815
(ns parsatron-play.core
(:refer-clojure :exclude [char])
(:require [the.parsatron :refer :all]))
;; Goal is to parse something like:
;; ##INFO=<ID=foo,Type=Integer,Number=1,Description="Some description, which may contain commas">
(defparser not-char [& cs]
(token (complement (set cs))))
@ray1729
ray1729 / for_science.clj
Created Sep 10, 2012
Solution to 4clojure problem 117
View for_science.clj
(fn solve-maze
[maze]
(let [num-rows (count maze)
num-cols (count (first maze))
char-at-pos (fn [[row col]] (get (get maze row) col))
free? (fn [p] (when-let [c (char-at-pos p)] (not= c \#)))
neighbours (fn [[row col]] (filter free? (map (fn [[delta-row delta-col]]
[(+ row delta-row)
(+ col delta-col)])
[[0 0] [0 1] [0 -1] [1 0] [-1 0]])))
View gist:2929488
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use List::Util qw(max min);
my @COLS = qw( label _ chr strand start end );
my @KEY_COLS = qw( label chr strand );
@ray1729
ray1729 / uniq.pl
Created Jun 14, 2012
Build array of unique data from a file
View uniq.pl
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
my %seen;
my @uniq;
while ( <> ) {
chomp;
@ray1729
ray1729 / gist:1349435
Created Nov 8, 2011
How many ways can we roll 10 dice and have all the digits 1-6 appear at least once?
View gist:1349435
(defn factorial [n] (reduce * (range 1 (inc n))))
(defn multinomial [n & ms]
(/ (factorial n) (reduce * (map factorial ms))))
(def N (reduce + (for [m1 (range 1 6)
m2 (range 1 (- 7 m1))
m3 (range 1 (- 8 m1 m2))
m4 (range 1 (- 9 m1 m2 m3))
m5 (range 1 (- 10 m1 m2 m3 m4))