Skip to content

Instantly share code, notes, and snippets.

(ns cklist.model.generic
(use cklist.util))
;; =========================================================================
;; revision and timestamping support
(defn timestamp
"Returns object timestamped with the current system time in milliseconds"
[obj]
(assoc obj :timestamp (System/currentTimeMillis)))
(ns cklist.validation
(:use clojure.test))
(defn- vresult
[k msg]
{k #{msg}})
(defn required
"Returns validator which requires field to be non-empty"
[msg k]
@jcromartie
jcromartie / markdown.clj
Created August 17, 2011 16:27
Markdown
(ns util.markdown
(import (java.io
BufferedReader
InputStreamReader)))
(defn markdown
[s]
(let [builder (java.lang.ProcessBuilder. ["Markdown.pl"])
proc (.start builder)
stdin (.getOutputStream proc)
@jcromartie
jcromartie / form_seq.clj
Created October 12, 2011 16:45
Lazy seq of forms
(import '(java.io BufferedWriter FileWriter
PushbackReader BufferedReader FileReader))
(defn form-seq
"Return lazy seq of forms in file at path. Closes reader only after
all forms are read."
[path]
(let [reader (-> path FileReader. BufferedReader. PushbackReader.)
eof (Object.)
next-form (fn next-form []
@jcromartie
jcromartie / journal.clj
Created October 13, 2011 14:31
simple persistence through journaled refs
(import '(java.io File
BufferedWriter FileWriter
PushbackReader BufferedReader FileReader))
(defn apply-code
[x code]
(apply (eval (first code))
x
(rest (map eval code))))
@jcromartie
jcromartie / staticase.clj
Created October 14, 2011 13:49
Clojure "case", compatible with Java static values
;; warning: this is probably dangerous and stupid
(defmacro staticase
[expr & cases]
(let [case-pairs (partition 2 cases)
evaled-pairs (map (fn [[l r]] [(if (symbol? l) (eval l) l) r]) case-pairs)
evaled-cases (flatten evaled-pairs)]
`(case ~expr ~@evaled-cases)))
@jcromartie
jcromartie / spark.rb
Created November 15, 2011 13:37
Sparklines in Ruby
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
# prints a sparkline in the terminal using the supplied list of numbers
# examples:
# spark.rb 10 20 30 100 90 80
# spark.rb 1 2 0.4 0.1 1.3 0.7
@ticks = %w[▁ ▂ ▃ ▄ ▅ ▆ ▇]
values = ARGV.map { |x| x.to_f }
@jcromartie
jcromartie / example.sh
Created November 18, 2011 19:21
Shell script example
#!/usr/bin/env bash
for i in {1..1000}
do
fname=example-$(printf %08x $i).pdf
foo=$(basename $fname .pdf)
echo $i "=>" $fname
done
@jcromartie
jcromartie / sudoku.clj
Created November 21, 2011 03:52 — forked from jkk/sudoku.clj
;;;; Translation of Peter Norvig's sudoku solver to idiomatic Clojure
;;;; See http://norvig.com/sudoku.html
;;;;
;;;; Throughout this program we have:
;;;; r is a row, e.g. :a
;;;; c is a column, e.g. 3
;;;; s is a square, e.g. [:a 3]
;;;; d is a digit, e.g. 9
;;;; u is a unit, e.g. [[:a 1] [:b 1] [:c 1] ... [:i 1]]
;;;; grid is a grid, e.g. 81 non-blank chars, e.g. starting with ".18...7..."
@jcromartie
jcromartie / math.clj
Created November 23, 2011 15:10
fast math
(ns anyclj.math)
(defn mean
"Returns the mean value of a dataset"
[coll]
(/ (apply + coll) (count coll)))
(defn median
"Returns the median value of a dataset"
[coll]