Skip to content

Instantly share code, notes, and snippets.

(let [method (.getMethod Long "valueOf" (into-array Class [String]))]
(.invoke method Long (into-array Object ["123"])))
(let [method (.getMethod String "indexOf" (into-array Class [String Integer/TYPE]))]
(.invoke method "hello" (into-array Object ["ll" 0])))
(with-open [rdr (io/reader (io/file "file.txt"))]
(doseq [line (line-seq rdr)]
(println "line:" line)))
(with-open [lines (line-seq (io/file "file.txt"))]
(doseq [line lines]
(println "line:" line)))
(defn call [f & args]
(when f (apply f args)))
(def callbacks
{:do-this #(println "this")
:do-that #(println "that")})
(call (:do-this callbacks)) ;; prints "this"
(call (:do-that callbacks)) ;; prints "that"
(call (:do-other callbacks)) ;; nil
@mefesto
mefesto / gist:990219
Created May 25, 2011 02:38 — forked from swannodette/gist:990176
bug.clj
(deftype Foo []
Object
(equals [this x]
(.. this getClass (isInstance x))))
(let [f (Foo.)]
(.equals f f)) ; false, creates problems when looking up items in map
@mefesto
mefesto / test_mystuff.clj
Created February 2, 2011 23:57
clojure test example
(ns blah.test-mystuff
(:use clojure.test))
(defn my-fixture [f]
(println "setup")
(f)
(println "tear down"))
(use-fixtures :each my-fixture)
@mefesto
mefesto / qsort.clj
Created January 3, 2011 05:01
Quicksort implementation courtesy of Introduction to Algorithms (3rd Ed.)
(ns mefesto.qsort)
(set! *warn-on-reflection* true)
(defn- aswap! [^doubles vals ^ints idxs ^long x ^long y]
(let [val (aget vals x)
idx (aget idxs x)]
(aset vals x (aget vals y))
(aset vals y val)
(aset idxs x (aget idxs y))
(require '[clojure.contrib.sql :as sql])
(sql/with-connection dbspec
(sql/transaction
(sql/do-prepared
"update mytable set counter = counter + 1
where id = ?"
[1000])))
(defn encode [^String str]
(ChannelBuffers/wrappedBuffer
^byte[] (into-array (concat (.getBytes "hello" "UTF-8") [(byte 0)]))))
(defn encode [^String str]
(ChannelBuffers/wrappedBuffer
(.getBytes str "UTF-8")
(byte-array [(byte 0)])))