Categories
Uncategorized

Seinfeld/Gates Microsoft Ad: Who are the Ad Wizards Who Came Up With This One?

At last, the first Microsoft commercial featuring Jerry Seinfeld and Bill Gates! Its humor follows the Seinfeld formula with one difference: Jerry plays the Kramer-esque role while Bill plays the “Seinfeld” part.

The ad runs for 1 minute and 30 seconds, the first minute of which is devoted to Jerry helping Bill shop for discount shoes. Microsoft or what they promise for the future isn’t mentioned until the 1:02 mark, and the big revelation is that they’re going to make computers moist and chewy like cake. The commercial ends with Bill shaking the junk in his trunk, followed by three cards: “The Future”, “Delicious”, and finally the Windows logo.

My guess is that the purpose of this commercial is to set the tone and flavor of the Gates/Seinfeld relationship for the ones to follow, which presumably will have a little more substance.

Categories
Uncategorized

Enumerating Enumerable: A Cute Trick for Explaining inject / reduce / fold

Enumerating Enumerable

The next method I’m going to cover in Enumerating Enumerable — the series of articles in which I try to do a better job of documenting Ruby’s Enumerable module than Ruby-Doc.org does — is inject, a.k.a. reduce. Not only is it one of the trickiest methods to explain, it’s also one of the cornerstones of functional programming. I thought that I’d take a little time to explain what the function does.

inject

The term inject comes from Smalltalk and isn’t terribly descriptive. I remember reading the documentation for it and being all confused until I saw some examples. I then realized that I’d seen this function before, but under two different names.

reduce

The Second-Best Accordion Picture Ever
Burning Man 1999: gratuitous nudity and even more gratuitous accordion!

The second name by which I encountered this function is reduce, and it was at Burning Man 1999. I was to start a new job the week after Burning Man, and I had to learn at least some basic Python by then. So along with my camping gear, accordion and a kilo of candy for barter, I also brought my laptop (a 233Mhz Toshiba Sattelite with a whopping 96MB of RAM) and O’Reilly’s Learning Python and noodled during the downtime (early morning and afternoon) on Python 1.6. When I got to covering the reduce function, I was confused until I saw some examples, after which I realized that I’d seen that function before, but under a different name.

(You may have also heard of reduce through Google’s much-vaunted MapReduce programming model.)

fold

The first name by which I encountered this function is fold, or more specifically, “fold left” or “foldl”, and it was at the “Programming Paradigms” course I took at Crazy Go Nuts University. “Programming Paradigms” was a second-year course and had the reputation of being the most difficult course in the computer science curriculum. The intended purpose of this course was to provide students with an introduction to functional programming (these days, they use Haskell and Prolog, back then, it was Miranda). Its actual effect was to make most of the students swear off functional programming for the rest of their lives.

In spite of the trauma from this course, I ended up remembering a lot from it that I was able to apply, first to Python and now to Ruby. One of these things is a cute little trick for cememnting in your mind what fold does.

What Ruby-doc.org Says

Before I cover that cute little trick, let’s take a look at what Ruby-doc.org’s documentation has to say about Enumerable‘s inject method.

One thing you’ll find at Ruby-doc.org is that as of Ruby 1.8.7 and later, inject gained a synonym: the more familiar term reduce.

As for the description of the inject/reduce method, I don’t find it terribly helpful:

Combines all elements of enum by applying a binary operation, specified by a block or a symbol that names a method or operator.

If you specify a block, then for each element in enum<i> the block is passed an accumulator value (<i>memo) and the element. If you specify a symbol instead, then each element in the collection will be passed to the named method of memo. In either case, the result becomes the new value for memo. At the end of the iteration, the final value of memo is the return value fo the method.

If you do not explicitly specify an initial value for memo, then uses the first element of collection is used as the initial value of memo.

(Yes, those stray <i> tags are part of the text of the description for inject. Hopefully they’ll fix that soon.)

This confusing text becomes a little clearer with some examples. The most typical example of inject/reduce/fold in action is the classic “compute the sum of the numbers in this range or array” problem. There are a number of approaches you can take in Ruby, all of which use inject/reduce:

(1..8).reduce(:+)
=> 36

(1..8).reduce {|sum, number| sum += number}
=> 36

(1..8).reduce(0) {|sum, number| sum += number}
=> 36

The reduce method takes some kind of operation and applies it across the enumerable to yield a single result. In this case, the operation is addition.

Explaining how that operation is applied is a little trickier, but I do just that in the next section.

Demonstrating inject / reduce / fold With a Piece of Paper and Literal Folding

To explain what’s happening in the code above, I’m going to do use a piece of paper. I’ve folded it into 8 even sections and then numbered each section, as shown in the photo below:

Think of the paper as the range (1..8). We’re now going to compute the sum of the numbers in this range, step by step, using a literal fold — that is, by folding the paper. I’m going to start folding from the left side of the paper, and when I do, I’m going to add the numbers that I’m folding into each other.

In the first fold, I’m folding the number 1 onto the number 2. Adding these two numbers yields 3, which I write on the back of the fold:

For the second fold, I fold the first number 3 onto the second number 3. The sum of these two numbers is 6, and I write that on the back of the resulting fold:

I fold again: this time, it’s the number 6 onto the number 4, the sum of which is 10. I write that number down on the resulting fold:

Next, I fold 10 onto 5, yielding the number 15:

I then fold 15 onto 6, which gives me 21:

Next comes 21 folded onto 7, which makes for a sum of 28:

And finally, 28 folded onto 8, which gives us a final total of 36.

And there you have it: a paper-based explanation of inject/reduce/fold, as well as why I often refer to the operation as “folding”.

Categories
Uncategorized

Google Chrome is Now Available for Download!

Downloading Google Chrome

I’m downloading Google Chrome as I write this. More later.

Categories
Uncategorized

Enumerating Enumerable: Enumerable#include?

Enumerating Enumerable

Welcome to the eighteenth installment of Enumerating Enumerable!

In this series of articles, I’m going through the methods in Ruby’s Enumerable in alphabetical order, explaining what each does and providing examples. This is my attempt to make better documentation for Ruby’s Enumerable module than Ruby-Doc.org’s.

In this article, I cover the include? method.

In case you missed any of the previous articles, they’re listed and linked below:

  1. all?
  2. any?
  3. collect / map
  4. count
  5. cycle
  6. detect / find
  7. drop
  8. drop_while
  9. each_cons
  10. each_slice
  11. each_with_index
  12. entries / to_a
  13. find_all / select
  14. find_index
  15. first
  16. grep
  17. group_by

Enumerable#include? Quick Summary

Graphic representation of the "include?" method in Ruby's "Enumerable" module.

In the simplest possible terms Does the collection contain an item equal to this one?
Ruby version 1.8 and 1.9
Expects An argument containing the item to search for in the collection.
Returns
  • true if there is at least one item in the collection that is equal (using the == operator) to the argument.
  • false if no item in the collection is equal (using the == operator) to the argument.
RubyDoc.org’s entry Enumerable#include?

Enumerable#include? and Arrays

When used on an array, include? iterates through it, comparing items in the array with the argument using the == operator. If any of these comparisons has a result of true, include? returns true. If you think of arrays as sets, you can think of include? as a set membership test.

Examples

# Here's a list of words (if they seem unfamiliar, go read
# http://en.wikipedia.org/wiki/Zoom_schwartz_profigliano)
words = ["zoom", "schwartz", "profigliano", "butterman"]
=> ["zoom", "schwartz", "profigliano", "butterman"]

# Is "profigliano" in our list of words?
words.include? "profigliano"
=> true

# How about "kwyjibo"?
words.include? "kwyjibo"
=> false

Enumerable#include? and Hashes

include?, when used with a hash, behaves differently than you might expect. You might think that include? would return true if given a two-element array argument that matched an item in the hash, where the first element matched the key and the second element matched the corresponding value.

However, that is not the case. Instead, include? returns true if there is a key in the hash that is equivalent to the given argument when compared with the == operator. In other words, include?, when used with a hash, answers the question “Is there an item in this hash with this key?”

Examples

values = {"zoom" => 1, "schwartz" => 5, "profigliano" => 10, "butterman" => 25}
=> {"zoom"=>1, "schwartz"=>5, "profigliano"=>10, "butterman"=>25}

values.include? "schwartz"
=> true

values.include? ["schwartz", 5]
=> false

values.include? "kwyjibo"
=> false

Categories
Uncategorized

Enumerating Enumerable: Enumerable#group_by

Enumerating Enumerable

Once again, it’s Enumerating Enumerable time! This is the latest in my series of articles where I set out to make better documentation for Ruby’s Enumerable module than Ruby-Doc.org’s. In this installment — the seventeenth in the series — I cover the group_by method.

In case you missed any of the previous articles, they’re listed and linked below:

  1. all?
  2. any?
  3. collect / map
  4. count
  5. cycle
  6. detect / find
  7. drop
  8. drop_while
  9. each_cons
  10. each_slice
  11. each_with_index
  12. entries / to_a
  13. find_all / select
  14. find_index
  15. first
  16. grep

Enumerable#group_by Quick Summary

Graphic representation of the "group_by" method in Ruby's "Enumerable" module.

In the simplest possible terms Break a collection into groups based on some given criteria.
Ruby version 1.9 only
Expects A block containing the criteria by which the items in the collection will be grouped.
Returns A hash where each key represents a group. Each key’s corresponding value is an array containing the members of that group.
RubyDoc.org’s entry Enumerable#group_by

Enumerable#group_by and Arrays

When used on an array, group_by iterates through the array, passing each element to to the block. The result value of the block is the group into which the element will be placed.

Example 1

For the first example, I’ll use some code similar to the example given in Ruby-doc.org’s writeup of group_by:

(0..15).group_by {|number| number % 3}
=> {0=>[0, 3, 6, 9, 12, 15], 1=>[1, 4, 7, 10, 13], 2=>[2, 5, 8, 11, 14]}

In the code above, the numbers 0 through 15 are passed to the block, which receives each number as the parameter number. The group that each number is placed into is determined by the result value of the block, number % 3, whose result can be one of 0, 1 or 2. This means that:

  • The resulting hash will have three groups, represented by the keys 0, 1 and 2
  • The key 0‘s corresponding value is an array containing the numbers in the range (0..15) that are evenly divisible by 3 (i.e. the numbers for which number % 3 is 0.
  • The key 1‘s corresponding value is an array containing the numbers in the range (0..15) that when divided by 3 leave a remainder of 1 (i.e. the numbers for which number % 3 is 1.
  • The key 2‘s corresponding value is an array containing the numbers in the range (0..15) that when divided by 3 leave a remainder of 2 (i.e. the numbers for which number % 3 is 2.

Example 2

In the first example, the keys in the resulting hash are the same type as the values in the array whose contents we’re grouping. In this example, I’ll show that the keys in the resulting hash don’t have to be the same type as the values in the array.

simpsons = %w(Homer Marge Bart Lisa Abraham Herb)
=> ["Homer", "Marge", "Bart", "Lisa", "Abraham", "Herb"]

simpsons.group_by{|simpson| simpson.length}
=> {5=>["Homer", "Marge"], 4=>["Bart", "Lisa", "Herb"], 7=>["Abraham"]}

In the code above, each Simpson name is passed to the block, which receives it as the parameter simpson. The block’s result is the length of simpson, and this result is the group into which the name will go.

In the resulting hash:

  • Note that the keys are integers while the names in the groups are strings.
  • The key 5‘s array contains those names in Simpsons that are 5
    characters in length.
  • The key 4‘s array contains those names in Simpsons that are 4 characters in length.
  • The key 7‘s array contains those names in Simpsons that are 7 characters in length.

Example 3

In the previous two examples, the keys for the resulting array were calculated from the values in the initial array. In this example, I’ll demonstrate that the keys for the groupings can be determined in a completely arbitrary fashion that has nothing to do with the values:

# Put the Simpsons into randomly determined groups
simpsons.group_by{rand(3) + 1}
=> {3=>["Homer", "Bart", "Abraham", "Herb"], 1=>["Marge", "Lisa"]}

# Let's try that again. The results are very likely to be different:
simpsons.group_by{rand(3) + 1}
=> {1=>["Homer", "Bart"], 2=>["Marge", "Lisa", "Herb"], 3=>["Abraham"]}

# One more time!
simpsons.group_by{rand(3) + 1}
=> {2=>["Homer", "Bart", "Lisa"], 3=>["Marge", "Herb"], 1=>["Abraham"]}

Enumerable#group_by and Hashes

When used on a hash, group_by passes each key/value pair in the hash to the block, which you can “catch” as either:

1. A two-element array, with the key as element 0 and its corresponding value as element 1, or
2. Two separate items, with the key as the first item and its corresponding value as the second item.

Example 1

In this example, we’ll group the cast of Family Guy by the item that they’re bringing to a potluck dinner:

potluck = {"Peter" => "lasagna",
           "Lois"  => "potato salad",
           "Chris" => "lasagna",
           "Meg"   => "brownies",
           "Stewie" => "chateaubriand",
           "Brian" => "potato salad",
           "Evil Monkey" => "potato salad"}
=> {"Peter"=>"lasagna", "Lois"=>"potato salad", "Chris"=>"lasagna", "Meg"=>"brownies",
"Stewie"=>"chateaubriand", "Brian"=>"potato salad", "Evil Monkey"=>"potato salad"}

# Here's one way to do it:
potluck.group_by{|person, bringing| bringing}
=> {"lasagna"=>[["Peter", "lasagna"], ["Chris", "lasagna"]], "potato salad"=>[["Lois", "potato salad"],
["Brian", "potato salad"], ["Evil Monkey", "potato salad"]], "brownies"=>[["Meg", "brownies"]],
"chateaubriand"=>[["Stewie", "chateaubriand"]]}

# Here's another way to do it:
potluck.group_by{|person| person[1]}
=> {"lasagna"=>[["Peter", "lasagna"], ["Chris", "lasagna"]], "potato salad"=>[["Lois", "potato salad"],
["Brian", "potato salad"], ["Evil Monkey", "potato salad"]], "brownies"=>[["Meg", "brownies"]],
"chateaubriand"=>[["Stewie", "chateaubriand"]]}

Example 2

In the previous example, the groupings were based on a calculation performed on the objects in the original hash. In this example, the groupings will be random: a random number generator will determine whose car each potluck attendee will ride to the potluck dinner:

potluck.group_by {[:peters_car, :quagmires_car, :clevelands_car][rand(3)]}
=> {:peters_car=>[["Peter", "lasagna"], ["Chris", "lasagna"], ["Evil Monkey", "potato salad"]],
:quagmires_car=>[["Lois", "potato salad"], ["Meg", "brownies"], ["Stewie", "chateaubriand"]],
:clevelands_car=>[["Brian", "potato salad"]]}

# Let's try another random grouping
potluck.group_by {[:peters_car, :quagmires_car, :clevelands_car][rand(3)]}
=> {:peters_car=>[["Peter", "lasagna"], ["Meg", "brownies"]], :quagmires_car=>[["Lois", "potato salad"],
["Stewie", "chateaubriand"], ["Brian", "potato salad"], ["Evil Monkey", "potato salad"]],
:clevelands_car=>[["Chris", "lasagna"]]}

# One more time!
potluck.group_by {[:peters_car, :quagmires_car, :clevelands_car][rand(3)]}
=> {:peters_car=>[["Peter", "lasagna"], ["Chris", "lasagna"], ["Stewie", "chateaubriand"]],
:quagmires_car=>[["Lois", "potato salad"], ["Evil Monkey", "potato salad"]], :clevelands_car=>[["Meg", "brownies"],
["Brian", "potato salad"]]}

Categories
Uncategorized

Blast from the Past: My Old Notes on the GNUtella Protocol

Found these photos while digging through my archives on the backup hard drive and thought you folks might find them interesting. They’re flipchart pages from either June 2000, back when I was working at Cory Doctorow’s startup, OpenCola. These are notes that I wrote on the GNUtella protocol; Chris Cummer and I worked off these notes to produce an app called COLAvision, which scored Gnutellanet for audio and video files, captured them and cued them up to be served as streams (oh, the assignments we got during the bubble…).

I expect to be doing a fair bit of doodling, since the b5media tech office walls are largely floor-to-ceiling whiteboards and I’ve also been diving into Dan Roam’s book, The Back of the Napkin, a book on solving problems and selling ideas with hand-drawn pictures on whiteboards, flipcharts, notepads, scrap paper or even — as the title implies — the back of a napkin (or serviette to you Brits out there).

Joey deVilla's GNUtella protocol notes, circa summer 2000.
My notes on the GNUtella protocol, part 1.

Joey deVilla's GNUtella protocol notes (part 2), circa summer 2000.
My notes on the GNUtella protocol, part 2.

Categories
Uncategorized

Pair Programming Chairs

The development tool company Cenqua have a cute gag ad for a pair programming chair called the “PairOn” — an Aeron built for two:

Cenqua's "PairOn" chair

They list the PairOn’s “key features” as:

  • Fully unit-tested in our ego-free ergonomics lab
  • Essential office furniture for any eXtreme XP Pair (XXPP)
  • Fully adjustable via individual or pair control
  • Can be levered to standup-meeting height
  • 40-hour-week alarm buzzer built in
  • Available in a range of attractive colours

Here’s a pair programming chair that really exists: it’s a set of drawers that doubles as a stool:

TSOT's "Pair Programming" filing cabinate/chair

These are drawer sets mounted on some very good wheels and topped with well-padded upholstery. If you wanted to pair program with someone, you’d simply pull the drawer unit out from under their desk and sit on it. These things were surprisingly comfortable; I’ve spent upwards of two hours sitting on one of these with nary a complaint.

Alas, these aren’t available in stores. They were custom-built for a startup for whom I briefly worked, whose CEOs was more in love with the idea of running a start-up than actually running a start-up. Their last programmer recently ditched them, and I’m hoping to pick up one of these in their bankruptcy sale.