This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[clojure.core.async :as async] | |
'[clj-http.client :as client] | |
'[clojure.data.json :as json]) | |
(def concurrency 5) | |
(let [in (async/chan) | |
out (async/chan) | |
request-handler (fn [url out*] | |
(async/go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# For loop | |
# suggested: replace with each | |
for i in x | |
end | |
# each_with_object mutates array | |
# suggested: replace with map | |
each_with_object([]) |list, i| | |
list << i | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cljs.user=> (defn foo [] (loop [i 1] (if (>i 10) i (recur (inc i))))) | |
#'cljs.user/foo | |
cljs.user=> foo | |
#object[cljs$user$foo "function cljs$user$foo(){ | |
var i = 1; | |
while(true){ | |
if(cljs.core.truth_(cljs.user._GT_i.call(null,10))){ | |
return i; | |
} else { | |
var G__23 = (i + 1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
repo = Bugwatch::Repo.discover('bugwatch', Dir.pwd) | |
# => #<Bugwatch::Repo:0x00000101c98ae0 @name="bugwatch", @url="/Users/jacobr/code/bugwatch"> | |
commit = repo.commits.first | |
# => #<Bugwatch::Commit:0x00000101ca0100 @attributes={:sha=>"d7ecd8b082906df27be6d973a196f32621d5ad1f", :message=>"add the rest of the headers", :diffs=>#<Enumerator: #<Enumerator::Generator:0x00000101ca0218>:each>, :stats=>#<Enumerator: #<Enumerator::Generator:0x00000101ca0290>:each>, :tree=>#<Bugwatch::Tree:0x00000101ca01c8 @grit=#<Grit::Tree "658dc44b7abec270b90121798e111202ac040dd9">>, :author_name=>"Jacob Richardson", :author_email=>"jacobr@groupon.com", :authored_date=>2013-07-23 12:22:49 -0600, :committer_name=>"Jacob Richardson", :committer_email=>"jacobr@groupon.com", :committed_date=>2013-07-23 12:22:49 -0600}> | |
commit.author_name | |
# => "Jacob Richardson" | |
commit.authored_date | |
# => 2013-07-23 12:22:49 -0600 | |
commit.files | |
# => ["Gemfile", "Rakefile", "bugwatch.gemspec", "features/exceptions/commit_finder.feature", "features/step_defin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example of chunking an operation that would use too much memory | |
# Grit is a git repository library | |
all_commits = -> grit_repo { | |
Enumerator.new do |y| | |
total_commit_count = grit_repo.commit_count | |
# we can safely handle 500 commits in memory | |
amount = 500 | |
offset = 0 | |
((total_commit_count / amount) + 1).times do |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
urls = ['http://www.google.com', 'http://www.github.com', 'http://www.twitter.com'] | |
concurrent_url_grabber = Enumerator.new do |y| | |
urls.map do |url| | |
Thread.new do | |
result = http_request(url) | |
# Need a mutex here for multithreaded VMs | |
y << result | |
end | |
end.each(&:join) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CommitScore | |
ScoreValue = Struct.new(:email, :score) | |
def initialize(commit_values) | |
@commit_values = commit_values | |
end | |
def score | |
grouped_commits_by_email.map {|(email, commits) score_value(email, commits) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fib(n) | |
if (n < 2) | |
n | |
else | |
fib(n-1) + fib(n-2) | |
end | |
end | |
JRuby 1.7.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lazy_expensive_sequence = Enumerator.new do |y| | |
offset = 0 | |
amount_to_retrieve = 50 | |
loop do | |
## This may not be memory safe if we try to retrieve all at once, so we safely retrieving an amount that we know will fit in memory | |
retrieve_memory_hogs(offset, amount_to_retrieve).each do |i| | |
y << i | |
end | |
offset += amount_to_retrieve | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SetupTestCase < Test::Unit::TestCase | |
def self.test(test_name, *setups, &block) | |
test_with_setups = lambda do | |
setup_procs = setups.map {|setup_func_name| respond_to?(setup_func_name) ? method(setup_func_name) : nil }.compact | |
setup_procs.each(&:call) | |
block.bind(self).call | |
end | |
super(test_name, &test_with_setups) | |
end |
NewerOlder