Skip to content

Instantly share code, notes, and snippets.

@m0smith
m0smith / map-indexed.clj
Created Sep 7, 2011
Updated map-indexed to add start and step to the indexing
View map-indexed.clj
(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]
View map-indexed-with-start.clj
(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 Sep 28, 2011
Proof of concept of using clojure and hibernate
View hibernate-clojure.clj
(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 Sep 28, 2011
Convert a number into a lazy sequence of digits as (digit-seq 1798) => (1 7 9 8)
View digit-seq.clj
(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 Sep 30, 2011
convert a long into a sequence of 8 bytes. The zeroes are padded to the beginning to make the BigIntger constructor happy
View long-to-bytes.clj
(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 Jan 26, 2012
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
View hexlify.clj
(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 Feb 1, 2012
Bad example of reading a database with a lazy seq
View baditer.clj
(with-session [session _]
(let [iter (.. session (createQuery "from Event") iterate)]
(iterator-seq iter)))
@m0smith
m0smith / gooditer.clj
Created Feb 1, 2012
Good example of reading database data
View gooditer.clj
(with-session [session _]
(let [iter (.. session (createQuery "from Event") iterate)]
(for [row (iterator-seq iter)]
(process-data row)))))
@m0smith
m0smith / multipartmime-zip.clj
Created Mar 8, 2012
A zipper for multi part mime parsing mht and mhtml files. Does not support updating
View multipartmime-zip.clj
(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 Jul 19, 2012
A simple function to add vectors. Zero pads vectors to the length of the longest
View vec+.clj
(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)))
You can’t perform that action at this time.