It’s a way of thinking

danvk tried to solve the Josephus Problem in Perl, Python, and Ruby, comparing each one. He had some pretty ignorant things to say about each.

There’s no reason to use object orientation here. It doesn’t help the problem or organize the data any better. But, it’s cargo cult to make everything a class. Even then, why all the bells and whistles? To you really need string overloading here?

Peter Scott has a natural approach using just the stuff from Learning Perl:

use strict;
use warnings;

my ($n, $k) = (40, 3);
my @soldiers = 1 .. $n;
my $pos = 0;

while ( @soldiers > 1 ) {
    $pos = ($pos + $k-1) % @soldiers;  # -1 for the one we just removed
    splice @soldiers, $pos, 1;

print "Winner: Person #@soldiers, alive\n";

danvk complains that he doesn’t like Perl’s object-oriented framework, but doesn’t realize Larry stole it from Python, the language he prefers. The joke is on him.

Object orientation is a way of thinking about things, not a particular technology. It’s not about translating a program from another language line-by-line using the same techniques.

Leave a comment


  1. Just wow. I read the original, and my first reaction:

    WTF? He’s manually implementing a linked list in (a language|languages) with mutable arrays that don’t even require type homogeneity.

    Maybe this makes more sense in Python, … or Java, or C++, where munging arrays is a nightmare of code.

    But in Perl? or even Ruby? Even in the language he’s more familiar with JavaScript, I’d hardly recommend that approach.

Leave a Reply

You must be logged in to post a comment.