Skip to content

Instantly share code, notes, and snippets.

(let [a (atom 1)]
(defn generate-unique-id
"Returns the next value of a thread-safe incrementing variable."
[]
(swap!
a
#(inc %))))
;; if we use (def) instead of (defn), we can move the atom variable inside the
;; (def) form. We just have to make sure that the atom is only created once, at
(->
(get "/foos/new")
(should-see "Create your foo")
(submit-form-with {:name "Tommy"})
(follow-redirect)
(should-see "Foo created!")
(should-see "Tommy"))
(defn probability-of-taking-this-transition
[tid incoming-variations transitions payment-rule]
(let [this-trans
(first
(filter #(= tid (:id %)) transitions)),
this-variations (this-trans :variations),
this-feasibility (this-trans payment-rule),
change-points
(distinct
(for [feas (map payment-rule transitions),
(for [ob j]
(for [property (:properties ob)]
(merge property (dissoc ob :properties))))
;; this should let you do something like
;;
;; (defn foobar
;; [x y z]
;; (with-defaults
;; [x (even? x) 1881
;; y (odd? y) 823]
;; (+ x y z)))
;;
@gfredericks
gfredericks / money.clj
Created July 10, 2011 16:18
mdeboard
(reduce
(fn [[amount-left coins] denom]
(let [{:keys [mod dividend]} (calc-coins amount-left denom)]
(if (pos? dividend)
[mod (assoc coins denom dividend)]
[amount-left coins])))
[amount {}]
denoms)
; user=> (flat [[[[[4 55] 6] [6 6 [5 4]]]]])
; [4 55 6 6 6 5 4]
(defn flat
[coll]
(loop [res [], coll coll, stack ()]
(if (empty? coll)
(if (empty? stack)
res
(recur res (first stack) (rest stack)))
@gfredericks
gfredericks / nesting.coffee
Created July 19, 2011 01:19
Five levels of nested functions
describe 'VisitCalendarView', ->
tr_for = (service_name) ->
# This commented version is more direct but jquery seems to bug out on it.
# Boo jquery!
# $("tr.line-item-view:has(td:first-child:contains(#{service_name}))")
$("tr.line-item-view td:first-child:contains(#{service_name})").parent()
beforeEach ->
ServerMocker.mock_model(ServiceRequest, 'abc123', Fixtures.service_request)
(defn static-field-map
[class]
(->>
class
(.getFields)
(seq)
(map (juxt #(-> % .getName .toLowerCase (.replaceAll "_" "-") keyword)
#(.get % class)))
(into {})))
@gfredericks
gfredericks / foo.rb
Created August 8, 2011 21:11
kimian quine
foo.rb:1: syntax error, unexpected tINTEGER, expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END
foo.rb:1: syntax error, unexpected tIN...