Skip to content

Instantly share code, notes, and snippets.

View ciniglio's full-sized avatar

Alejandro Ciniglio ciniglio

View GitHub Profile
@ciniglio
ciniglio / gist:aa5544b967d920e1fce8
Last active August 29, 2015 14:13
Doall and parallelism
;; fake-io is a function that returns a promise and sets a future to deliver a random int after 5 seconds
user> (time (doall (take 3 (map deref (map (fn [_] (fake-io)) (vec (range 1000)))))))
"Elapsed time: 5006.289 msecs"
(183 475 390)
user> (time (doall (take 32 (map deref (map (fn [_] (fake-io)) (vec (range 1000)))))))
"Elapsed time: 5003.565 msecs"
(12 45 407 497 446 329 398 80 134 286 351 136 307 170 324 288 197 357 337 481 462 480 324 468 304 141 328 247 445 118 211 0)
user> (time (doall (take 33 (map deref (map (fn [_] (fake-io)) (vec (range 1000)))))))
"Elapsed time: 10008.503 msecs"
@ciniglio
ciniglio / gist:bf80f6ceb664f75032ae
Created January 17, 2015 19:52
Clojure race condition
;; Original from
;; https://github.com/aphyr/prism/blob/d59aa8cc3b59b44f5241e53d452340bfc3936343/src/com/aphyr/prism.clj#L63
(defn slur
"Takes a unary function f, and a time in ms. Returns a function g such that
(g x) causes (f x) to be invoked unless (g x) had been invoked less than dt
ms ago."
[dt f]
; Maintain a map of args to invocation times.
(let [ts (atom {})]
@ciniglio
ciniglio / gist:4b0636aed59087323d3f
Created November 28, 2014 00:38
Unordered pipeline
(def from-chan)
(def to-chan)
(dotimes [_ num-threads]
(let [rf (fn [_ x] (>!! to-chan))
f (xform rf)]
(thread
(loop []
(when-some [x (<!! from-chan)]
(f x))))))
;; Taken from https://github.com/Prismatic/eng-practices/blob/master/clojure/20130821-testing-principles.md
(defmacro test-double
"Creates a test double of a function given a fully qualified function name.
Behaves like a function, and then stores arguments values in a map,
with key as the argument name"
([] `(gensym))
([fqfn] ; fully qualified function for argument naming
(let [args (first (:arglists (meta (resolve fqfn))))
lein deps :tree
[clojure-complete "0.2.3" :exclusions [[org.clojure/clojure]]]
[org.clojure/clojure "1.6.0"]
[org.clojure/tools.nrepl "0.2.3" :exclusions [[org.clojure/clojure]]]
desc "tail production log files"
task :tail_logs, :roles => :app do
trap("INT") { puts 'Interupted'; exit 0; }
run "tail -f #{shared_path}/log/production.log" do |channel, stream, data|
puts # for an extra line break before the host name
puts "#{channel[:host]}: #{data}"
break if stream == :err
end
end
@ciniglio
ciniglio / gist:5346484
Last active December 16, 2015 00:19
Installing graphite on EC2
@ciniglio
ciniglio / staging_urls.rb
Created February 19, 2013 17:38
Staging URL helper
def self.staging_url(url)
if Rails.env == "staging"
url.sub('.', '-staging.')
else
url
end
end
@ciniglio
ciniglio / gist:4986817
Created February 19, 2013 15:22
Exporting db from heroku and using it in development
heroku pgbackups --app APP_NAME 
heroku pgbackups:capture --expire --app APP_NAME # Force a new backup
curl `heroku pgbackups:url --app APP_NAME` -o ~/Desktop/dump #download last backup
pg_restore -c -U username -d database_name_env ~/Desktop/dump # load dump into db (cleanly)