Skip to content

Instantly share code, notes, and snippets.

@m0smith
m0smith / map-indexed.clj
Created September 7, 2011 21:35
Updated map-indexed to add start and step to the indexing
(defn map-indexed
"Returns a lazy sequence consisting of the result of applying f to 0
and the first item of coll, followed by applying f to 1 and the second
item in coll, etc, until coll is exhausted. Thus function f should
accept 2 arguments, index and item."
{:added "1.2"}
([f coll] (map-indexed f 0 1 coll))
([f start coll] (map-indexed f start 1 coll))
([f start step coll]
(letfn [(mapi [idx coll]
@m0smith
m0smith / map-indexed-with-start.clj
Created September 8, 2011 15:13
map-indexed with start
(defn map-indexed
"Returns a lazy sequence consisting of the result of applying f to 0
and the first item of coll, followed by applying f to 1 and the second
item in coll, etc, until coll is exhausted. Thus function f should
accept 2 arguments, index and item."
{:added "1.2"}
([f coll] (map-indexed f 0 coll))
([f start coll]
(letfn [(mapi [idx coll]
(lazy-seq
@m0smith
m0smith / hibernate-clojure.clj
Created September 28, 2011 12:52
Proof of concept of using clojure and hibernate
(ns topoged.model.type
(:import
(javax.persistence Entity Id Column Table GeneratedValue)
))
(definterface IType (getId []))
(deftype
^{Entity {} Table {:name="TYPE"} org.hibernate.annotations.Entity {:mutable false}}
TypeX [^Long id]
@m0smith
m0smith / digit-seq.clj
Created September 28, 2011 22:35
Convert a number into a lazy sequence of digits as (digit-seq 1798) => (1 7 9 8)
(defn digit-seq
"Convert a number into a lazy sequence of digits as (digit-seq 1798) => (1 7 9 8)"
[num] (map #(- (int %) (int \0)) (str num)))
@m0smith
m0smith / long-to-bytes.clj
Created September 30, 2011 15:46
convert a long into a sequence of 8 bytes. The zeroes are padded to the beginning to make the BigIntger constructor happy
(defn long-to-bytes
"convert a long into a sequence of 8 bytes. The zeroes are padded to the
beginning to make the BigInteger contructor happy" [^long lng]
(let [pad (repeat 8 (byte 0))
bytes (map byte (.. (BigInteger/valueOf lng) toByteArray))]
(concat (drop (count bytes) pad) bytes)))
@m0smith
m0smith / hexlify.clj
Created January 26, 2012 19:16
Perform similar to hexlify in emacs. Accept a seq of bytes and convert it into a seq of vectors. The first element of the vector is a seq of 16 strings for the HEX value of each byte. The second element of the vector is a seq of the printable represent
(defprotocol Hexl
(hexl-hex [val])
(hexl-char [char]))
(extend-type Number
Hexl
(hexl-hex [i]
(let [rtnval (Integer/toHexString (if (< i 0) (+ 256 i) i)) ]
(if (< (count rtnval) 2) (str "0" rtnval) rtnval)))
(hexl-char [b]
@m0smith
m0smith / baditer.clj
Created February 1, 2012 16:00
Bad example of reading a database with a lazy seq
(with-session [session _]
(let [iter (.. session (createQuery "from Event") iterate)]
(iterator-seq iter)))
@m0smith
m0smith / gooditer.clj
Created February 1, 2012 16:24
Good example of reading database data
(with-session [session _]
(let [iter (.. session (createQuery "from Event") iterate)]
(for [row (iterator-seq iter)]
(process-data row)))))
@m0smith
m0smith / multipartmime-zip.clj
Created March 8, 2012 17:03
A zipper for multi part mime parsing mht and mhtml files. Does not support updating
(ns gist.multipartmime-zip
(:require [clojure.zip :as zip])
(:import [javax.mail.util ByteArrayDataSource]
[javax.mail.internet MimeMultipart]))
(defn multipart? [part]
(.startsWith (.getContentType part) "multipart"))
(defn zip-children [mime-multi-part]
(let [count (.getCount mime-multi-part)]
@m0smith
m0smith / vec+.clj
Created July 19, 2012 20:00
A simple function to add vectors. Zero pads vectors to the length of the longest
(defn vec+
([] nil)
([v1] v1)
([v1 v2]
(let [c1 (count v1)
c2 (count v2)
seq (if (< c1 c2)
(map + (concat v1 (repeat 0)) v2)
(map + (concat v2 (repeat 0)) v1))]
(vec seq)))