Skip to content

Instantly share code, notes, and snippets.

View halgari's full-sized avatar

Timothy Baldridge halgari

View GitHub Profile
(defn search-log [log term]
(with-open [rdr (java.io.BufferedReader.
(java.io.FileReader. log ))]
(filter #(not (= -1 (.indexOf % term))
(line-seq rdr))))
@halgari
halgari / gist:3181861
Created July 26, 2012 12:54
example image downloader with global scope
public static class ImageDownloader
{
public static string Server = "http://foo.bar.com/images";
public static GetImage(string id)
{
return DownloadURL(url + "/" + id);
}
}
@halgari
halgari / gist:3213067
Created July 31, 2012 02:49
Async example in clojure-py
(ns async-test
(:require urllib))
(defmacro await-async [& body]
`(py.bytecode/YIELD_VALUE ~@body))
(defn download-google
[]
(future
(with-open [x (urllib/urlopen "http://www.google.com")]
(defproject clj-lobapp "0.1.0-SNAPSHOT"
:description "A Line of Business App in Clojure"
:url "http://github.com/halgari/clj-lobapp"
:dependencies [[org.clojure/clojure "1.4.0"]
[com.datomic/datomic-free "0.8.3343"]
[midje "1.4.0"]])
tim@tim-desktop:~/clj-lobapp$ ~/bin/lein midje
>>> Output from clojure.test tests:
FAIL in (a-test) (core_test.clj:7)
FIXME, I fail.
expected: (= 0 1)
actual: (not (= 0 1))
>>> clojure.test summary:
Ran 1 tests containing 1 assertions.
@halgari
halgari / gist:3237283
Created August 2, 2012 13:56
varargs defrecord
(defrecord Foo [exprs])
(defn foo [& exprs]
(Foo. exprs))
@halgari
halgari / gist:3341474
Created August 13, 2012 14:54
jitlog
[13235a60055c] {jit-log-opt-loop
# Loop 0 (0 LOAD_ARG) : loop with 196 ops
[p0, p1, p2]
+84: label(p0, p1, p2, descr=TargetToken(140251232256032))
debug_merge_point(0, 0, '0 LOAD_ARG')
+135: p4 = getfield_gc(ConstPtr(ptr3), descr=<FieldP system.interpreter.Interpreter.inst__call_stack 16>)
+148: p6 = call(ConstClass(W_InternalList.getFirst), p4, descr=<Callr 8 r EF=0>)
+167: i7 = getfield_gc(ConstPtr(ptr3), descr=<FieldS system.interpreter.Interpreter.inst__ip 24>)
+180: i9 = call(ConstClass(get_bcode_at), p6, i7, descr=<Calli 8 ri EF=0>)
+209: i11 = int_add(i7, 1)
@halgari
halgari / 101212
Created October 12, 2012 13:50
Rube Goldberg Dates
user=> (let [r (range 10 13 2)]
(apply str (interpose "/"
(take 3 (concat r (reverse r))))))
"10/12/12"
user=>
@halgari
halgari / gist:4136116
Created November 23, 2012 15:26 — forked from AlexBaranosky/gist:4134522
doseq-indexed
;; Example:
;; (doseq-indexed idx [name names]
;; (println (str idx ". " name)
(defmacro doseq-indexed-functional [index-sym [item-sym coll] & body]
`(doseq [[~item-sym ~index-sym]
(map vector ~coll (range))]
~@body))
@halgari
halgari / gist:4195378
Created December 3, 2012 14:35
Atom refactoring
(defn update-my-atom [a name]
(let [new-state (assoc-in @a [:foo :bar] merge {:name name})]
(swap! a (fn [old] new-state))))
;; refactored into:
(defn get-new-state [state name]
(assoc-in state [:foo :bar] merge {:name name}))