Skip to content

Instantly share code, notes, and snippets.

View davidbalbert's full-sized avatar

David Albert davidbalbert

View GitHub Profile

This is a partial implementation of Piumarta and Warth's Open, extensible object models paper in Ruby. It is the simpler implementation where bind returns a method rather than a closure that wraps the method and some arbitrary piece of data. It also doesn't implement any method caching. I'm not super happy with the the split between in the "low level" Ruby object system, and the "higher level" system that we're implementing, but I don't have time to do a redesign before publishing today's PotW :).

Running the program will drop you into a shell where you can play with the object model. It's nicer if you have pry installed because then you'll have access to local variables like symbol and s_delegated.

module Enumerable
def reductions(sym = nil, &block)
raise LocalJumpError, "no block given" unless sym || block
if sym
block = sym.to_proc
end
arr = to_a
Aardvark
Abyssinian
Accelerator
Accordion
Account
Accountant
Acknowledgment
Acoustic
Acrylic
Act
@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.
@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
slashes = ["|", "/", "-", "\\"]
slashes.cycle do |s|
system "clear"
puts ":#{s}"
sleep 0.3
end
@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

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 / 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
@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; };