Skip to content

Instantly share code, notes, and snippets.

@devn
Created October 13, 2010 00:43
Show Gist options
  • Select an option

  • Save devn/623200 to your computer and use it in GitHub Desktop.

Select an option

Save devn/623200 to your computer and use it in GitHub Desktop.
;; "Clojure transactions should be easy to understand if you've ever used database
;; transactions - they ensure that all actions on Refs are atomic, consistent, and
;; isolated." (http://clojure.org/refs)
(def large-future-data-structure (ref {}))
(def big-text (read-lines (File. "/path/to/my/file")))
;; #"" is how you define a regex
;; dosync is required for transactions with a ref
(defn add-to-structure [line]
(let [match (re-match #"foo" line)]
(dosync
(if match (conj match large-future-data-structure)))))
;; (fn [x]) can be replaced with the syntactic sugar: #(%)
;; as in: (map #(inc %) [1 2 3 4]) => [2 3 4 5]
;; or: (map #(+ % %) [1 2 3 4]) => [2 4 6 8]
(.start (Thread. (fn [] (map add-to-structure big-text))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment