Skip to content

Instantly share code, notes, and snippets.

View davidbalbert's full-sized avatar

David Albert davidbalbert

View GitHub Profile
@davidbalbert
davidbalbert / created_at.rb
Last active December 31, 2015 20:49
A bad idea
module CreatedAt
module ClassMixin
def new(*)
obj = super
obj.instance_variable_set(:@_created_at, Time.now)
obj
end
end
require 'singleton'
class NothingClass
include Singleton
def bind
self
end
def map
@davidbalbert
davidbalbert / set.js
Last active January 1, 2016 17:18
Implementations of immutable object oriented sets from William Cook's essay "On Understanding Data Abstraction, Revisited" (http://www.cs.utexas.edu/~wcook/Drafts/2009/essay.pdf). Set.rb is idiomatic Ruby, set.js is sort of idiomatic JavaScript, and set2.js is less idiomatic, but more faithful to the paper.
/*
* Idiomatic-ish JavaScript. Constructor functions use the `new`
* keyword, but we don't use prototype objects.
*/
function Empty () {
this.isEmpty = true;
this.contains = function (i) { return false; };
this.insert = function (i) { return new Insert(this, i); };
this.union = function (s) { return s; };
@davidbalbert
davidbalbert / myhash.rb
Last active April 17, 2019 13:57
A reimplementation of Ruby's Hash in Ruby. It hash all the methods I felt like implementing. It does not preserve insertion order. It is untested.
class Array
def to_my_h
MyHash[self]
end
end
class Hash
def to_my_h
to_a.to_my_h
end

Keybase proof

I hereby claim:

  • I am davidbalbert on github.
  • I am davidbalbert (https://keybase.io/davidbalbert) on keybase.
  • I have a public key whose fingerprint is C59D 970A 2A49 D78B ACE7 6722 F447 4DD8 FC80 DAB5

To claim this, I am signing this object:

@davidbalbert
davidbalbert / hmm_om.cljs
Created July 7, 2014 01:57
A curious case.
(def hmm-state (atom {:value "Hmm..."}))
(defn hmm [state owner]
(reify
om/IRender
(render [_]
(om.dom/textarea #js {:value (:value state)
:onChange (fn [e] (.log js/console (.. e -target -value)))}))))
(om/root
slashes = ["|", "/", "-", "\\"]
slashes.cycle do |s|
system "clear"
puts ":#{s}"
sleep 0.3
end
@davidbalbert
davidbalbert / community_import.rb
Last active August 29, 2015 14:04
Google Groups -> Community import script
# This will be pasted into the REPL
require 'csv'
# these were extracted from the first line of the CSV
GoogleGroupContact = Struct.new(:email_address, :nickname, :group_status, :email_status, :email_preference, :posting_permissions, :join_year, :join_month, :join_day, :join_hour, :join_minute, :join_second, :time_zone)
class InteractiveContactFinder
def self.find_all(contacts)
@@done = false
@davidbalbert
davidbalbert / remove_dupes.rb
Created September 2, 2014 19:49
Community: Destroy duplicate visited statuses
# Returns the number of user+thread combinations with more than one visited
# status, this should be greater than zero before the fix and zero after the
# fix.
VisitedStatus.group([:user_id, :thread_id]).having("COUNT(*) > 1").count.size
# The number of user+thread combinations with at least one visited status. This
# number should remain the same before and after the fix.
VisitedStatus.group([:user_id, :thread_id]).count.size
# The fix: Group all visited statuses by user+thread, select the visited statuses.
Aardvark
Abyssinian
Accelerator
Accordion
Account
Accountant
Acknowledgment
Acoustic
Acrylic
Act