Skip to content

Instantly share code, notes, and snippets.

@krishnabhargav
Created July 19, 2014 12:51
Show Gist options
  • Save krishnabhargav/1445de8cc1ccb2c6b49a to your computer and use it in GitHub Desktop.
Save krishnabhargav/1445de8cc1ccb2c6b49a to your computer and use it in GitHub Desktop.
Some Clojure Tidbits
;; CLOJURE STM USING REFS
;; this is just an immutable declaration
(def some-numbers #{1 2 3})
;;if you want to make the above mutable, you can use "refs"
(def some-numbers (ref #{1 2 3 }))
;; @ is used to dereference data
(def current-some-numbers @some-numbers)
;; the set is already dereferenced, so 23 is added to that set
;; the some-numbers remains unchanged
(@some-numbers conj 23)
;; dosync is required when modifying the references
;; alter ref func args => applies the func on the ref "in-place"
(dosync (alter @some-numbers conj 23))
;;how to setting some-numbers to a vector?
;;meaning changing it completely
(dosync (ref-set @some-numbers [1 2 3 4 5]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment