Skip to content

Instantly share code, notes, and snippets.

@O-I
O-I / object-relational-impedance-mismatch.md
Last active November 6, 2017 23:14
Object-Relational Impedance Mismatch

The fundamental difficulty

Straight from the [wiki]:

Fundamentally, objects (instances) reference one another and therefore form a graph in the mathematical sense (a network including loops and cycles). Relational schemas are, in contrast, tabular and based on the relational algebra, which defines linked heterogeneous tuples (groupings of data fields into a "row" with different types for each field).

Converting linked tabular rows to graph structures is hard, and even described as the [Vietnam of Computer Science][vietnam-of-cs].

Further reading:

@O-I
O-I / four_elements_simple_design.md
Last active February 26, 2024 11:44
The Four Elements of Simple Design

The Four Elements of Simple Design

  • Introduced by [Kent Beck][beck] in the 1990s.
  • Part of his software development methodology [Extreme Programming][extreme-programming].
  • His exact wording appears in the [White Book][white-book].

The rules can be stated as followed:

  1. Passes all tests
  2. Maximizes clarity
@O-I
O-I / blockchain-links.md
Last active August 14, 2023 18:46
Blockchain links
@O-I
O-I / damm.rb
Last active August 22, 2016 01:27
Damm Algorithm
module Damm
# See http://en.wikipedia.org/wiki/Damm_algorithm
TABLE=["0317598642","7092154863","4206871359","1750983426","6123045978",
"3674209581","5869720134","8945362017","9438617205","2581436790"]
def lookup number
number.to_s.each_char.inject(0) do |m,v|
TABLE[m][v.to_i].to_i
end
end
@O-I
O-I / counter_cache_has_many_through.md
Last active August 29, 2015 14:27
Counter caching on a has_many :through association in Rails 3.2

I'm trying to figure out if using counter_cache on a has_many :through association is still considered kosher in Rails 3.2. I've read several posts that distill the steps for setting this up (e.g., see 1 and 2). On the other hand, it seems like counter_cache on has_many :through was an unintended side effect that was never a supported feature of Rails and was ultimately removed.

Here's my specific scenario:

# Event model
has_many :event_attendees, dependent: :destroy
has_many :attendees, through: :event_attendees, source: :user

# EventAttendee model
@O-I
O-I / weighted_random_sampling.md
Last active February 21, 2024 19:02
[TIx 8] Weighted Random Sampling in Ruby

One of the many reasons I love working with Ruby is it has a rich vocabulary that allows you to accomplish your goals with a minimal amount of code. If there isn't a method that does exactly what you want, it's usually possible to build an elegant solution yourself.

Let's take the example of simulating the rolling of a die.

We can represent a die as an array of its faces.

die = [*?⚀..?⚅]
# => ["⚀", "⚁", "⚂", "⚃", "⚄", "⚅"]
@O-I
O-I / profiles.clj
Last active January 8, 2017 01:11
My Clojure profile settings
{:user {:dependencies [[org.clojure/clojure "1.8.0"]
[org.clojure/tools.namespace "0.2.10"]
[spyscope "0.1.5"]
[criterium "0.4.3"]]
:injections [(require '(clojure.tools.namespace repl find))
; try/catch to workaround an issue where `lein repl`
; outside a project dir will not load reader literal
; definitions correctly:
(try (require 'spyscope.core)
(catch RuntimeException e))]
@O-I
O-I / scrape_mobile_site_with_ruby.md
Created March 4, 2015 19:20
[TIx 7] Scraping the mobile version of a site with Ruby

I found myself in a situation where I wanted to examine the layout of the mobile version of a particular website. I tend to use Ruby's OpenURI module and the Nokogiri gem for my webscraping needs, and it turns out it's really easy to get a mobile version of the site with a bit more effort:

require 'open-uri'
require 'nokogiri'

# let's look at my GitHub profile as an example
url = 'https://github.com/O-I'

# this opens the URL and parses it as XML 
@O-I
O-I / view_source_in_clojure_repl.md
Last active August 29, 2015 14:15
[TIx 6] View source code in a Clojure REPL

This is one of those things that was right in front of my face, but I figure it's worth a mention. I was trying to figure out the best way to view the source code for Clojure's frequencies function — ideally while in a REPL. I found this older post mentioning a source function in the clojure.contrib.repl-utils namespace. I launched a REPL to see if I could use it and, well, here's what I saw:

$ lein repl
nREPL server started on port 58700 on host 127.0.0.1 - nrepl://127.0.0.1:58700
REPL-y 0.3.5, nREPL 0.2.6
Clojure 1.6.0
Java HotSpot(TM) 64-Bit Server VM 1.7.0_45-b18
    Docs: (doc function-name-here)
 (find-doc "part-of-name-here")