Skip to content

Instantly share code, notes, and snippets.

View timmc's full-sized avatar

Tim McCormack timmc

View GitHub Profile
(defn fibseq
([]
(cons 0 (cons 1 (fibseq 0 1))))
([n0 n1]
(let [n2 (+' n0 n1)] ;; using auto-promoting addition
(cons n2 (lazy-seq (fibseq n1 n2))))))
(reduce + (filter even? (take-while #(<= % 4e6) (fibseq))))
;; user=> (nth (fibseq) 500)
@timmc
timmc / 1-SimpleButWrong.java
Created October 18, 2012 15:46
Ugh, UTF-16
/**
* Truncate the input and add an ellipsis, if necessary.
* @param in String to possibly truncate.
* @param max Maximum length of output
* @param ellipsis Ellipsis string, defaults to "..."
* @return String whose length will never exceed <code>max</code> UTF-16 chars
* @throws IllegalArgumentException if <code>max &lt; ellipsis.length()</code>
*/
public static String ellipsize(String in, int max, String ellipsis) throws IllegalArgumentException {
if (in == null) {
@timmc
timmc / rest.swear.clj
Last active December 15, 2016 15:24
rest in curje [now called swearjure]
;; An exercise in writing #'rest without [0-9a-zA-Z], by Tim McCormack
;; Note that the input must be a vector, although the code could easily be
;; modified to vector-ify any input coll.
;; Let's take this a step at a time...
(#(((% 1) :main) %) ;; call the main method with the input and fns
[[0 1 2 3 4 5] ;; the input is hardcoded here
;; fns are accessed by static indices into the fn table
{:main #(if (= (% 0) [])
@timmc
timmc / gist:5040043
Last active December 14, 2015 06:08 — forked from dcolish/gist:5039976
(defn write-builder [cube & ats]
(loop [builder (WriteBuilder. cube)
ats ats]
(if (empty? ats)
builder
(recur (.at builder# (first ats#) ...missing-arg...)
(rest ats#)))))
(write-builder cube [[time (now)]])
@timmc
timmc / gist:5049288
Last active December 14, 2015 07:18 — forked from anonymous/gist:5049270
(defmacro builder [initial method & argsets]
(list* 'doto initial
(for [args argsets]
`(~method ~@args))))
;; Example:
(builder (StringBuilder.) .append [1] ["a"])
;; becomes...
@timmc
timmc / 1.repl.clj
Created March 6, 2013 17:04
Compiling Java against a class with abstract methods
;; Spit reified class to disk
(alter-var-root #'*compile-files* (constantly true))
;;=> true
;; Create an abstract-ish class
(class (reify java.util.List (size [this] 10)))
;;=> clj.core$eval1062$reify__1063
;; Nope, it's not an "abstract class"
@timmc
timmc / gist:5180134
Last active December 16, 2016 00:12
Deployment example in Leiningen

Using GPG with lein deploy

First, a keypair

I used gpg2 --gen-key to create a key. I chose 4096-bit RSA with a 27 month expiration. (I plan to renew my key every 2 years, with a 3 month grace period.) For the comment field I used "software signing".

Now I can see it in my key list:

In: {:creds :gpg, :username :gpg, :url https://clojars.org/repo/, :signing {:gpg-key 0x00D85767}, :password :gpg}
Out: [clojars {:signing :gpg-key, :url https://clojars.org/repo/, :username timmc, :password password-here}]
@timmc
timmc / gist:5180320
Created March 17, 2013 05:49
failing lein core user test
(deftest resolving-repo-creds
(with-redefs [credentials-fn (constantly {#"^https://clojars\.org/.*"
{:username "u" :password "p"}})]
(is (= (resolve-credentials {:url "https://clojars.org/repo"
:creds :gpg
:signing {:gpg-key "0x00D85767"}})
{:url "https://clojars.org/repo"
:username "u" :password "p"
:signing {:gpg-key "0x00D85767"}}))))
((clojure-mode . ((clojure-defun-indents . (do-monad checker)))))