Skip to content

Instantly share code, notes, and snippets.

(ns datasifter)
;; Write a data sifter, sift, that partitions a string into a list of lists.
;; Start with the case of using letters as a delimiter, and numbers as data.
;; There can be any number of repetitions of numbers & letters.
;;
;; user=>(sift "a1b2cd34")
;; (("a" ("1")) ("b" ("2")) ("c" ()) ("d" ("3" "4")))
;;
;; from http://fulldisclojure.blogspot.com/2010/01/code-kata-data-sifter.html
(defn count-ips [logfile]
(remove empty?
(persistent! (reduce
#(assoc! %1 %2 (inc (get %1 %2 0)))
(transient {})
(of-IPs
(find-lines "Deny tcp src outside" logfile))))))
@devn
devn / demo.clj
Created March 30, 2010 21:37 — forked from cgrand/demo.clj
;; this file is a walkthrough of Moustache features, a web framework for Clojure
;; http://github.com/cgrand/moustache/tree/master
;; Moustache allows to declare routes, apply middlewares and dispatch on http methods.
;; Moustache is compatible with all frameworks built on Ring, including Compojure
(ns demo
(:use net.cgrand.moustache)
(:use [ring.adapter.jetty :only [run-jetty]])) ;; hmmm Ring without servlets
(defn categorize-code [files]
(reduce (fn [result code]
(try
(let [r (*sandbox* code)]
(update-in result [:good] conj [code r])
[code (pr-str r)])
(catch Exception e
(update-in result [:bad] conj code))))
{}
(find-lines text files)))
@devn
devn / web.clj
Created April 1, 2010 14:47 — forked from swannodette/web.clj
(ns defn-test
(:use clj-html.core
[net.cgrand.moustache :only [app]]
[ring.adapter.jetty :only [run-jetty]]
[ring.util.response :only [response]]))
(defhtml application [text body]
[:html
[:head
[:title text]]
@devn
devn / core.clj
Created April 1, 2010 16:16 — forked from Licenser/core.clj
(defn truncate [t coll]
(map (fn [ct]
(if (>= (count ct) t)
(apply str (take t ct) "...")
ct)) coll))
(defn walton*
[#^String s t m?]
(let [result (walton-doc s)]
(defn rand-range [m n]
(+ m (rand-int (- n m))))
(defn with-transient [x f]
(persistent! (f (transient x))))
(defn swap-entries! [x i j]
(assoc! x i (x j) j (x i)))
(defn knuth-shuffle [xs]
(def *enc-files-dir* "/home/blah/sandbox/enc")
(def *dec-files-dir* "/home/blah/sandbox/dec")
(defstruct file-map :filename :path :modified)
(defn #^{:doc "Return vec of file-maps"}
file-list [path]
(vec
(map (fn [n]
(struct-map file-map
(def file-info-extractors
{:filename #(.getName #^java.io.File %)
:path #(str (.toURI #^java.io.File %))
:modified #(.lastModified #^java.io.File %)})
(map #(reduce (fn [m [k e]]
(assoc m k (e %)))
{}
file-info-extractors)
(.listFiles (java.io.File. ".")))
user> (read-string (str {:hai "there" "hello" 3 5 "5"}))
{:hai "there", "hello" 3, 5 "5"}