Primzahlen

Posted on July 25, 2006

The magnanimous Eric Holbrook unwaveringly opined that it is possible to generate the prime numbers using g++ error messages.

template <int i> struct D { D(void*); operator int(); };

template <int p, int i> struct is_prime {
 enum { prim = (p==2) || (p%i) && is_prime<(i>2?p:0), i-1> :: prim };
};

template <int i> struct Prime_print {
 Prime_print<i-1> a;
 enum { prim = is_prime<i, i-1>::prim };
 void f() { D<i> d = prim ? 1 : 0; a.f();}
};

template<> struct is_prime<0,0> { enum {prim=1}; };
template<> struct is_prime<0,1> { enum {prim=1}; };

template<> struct Prime_print<1> {
 enum {prim=0};
 void f() { D<1> d = prim ? 1 : 0; };
};

#ifndef LAST
#define LAST 18
#endif

main() {
 Prime_print<LAST> a;
 a.f();
}

Save this off as dedekind.C and then abuse gcc with the nefarious recipe.

% g++ -DLAST=30 dedekind.C 2>&1 | grep "error:  "
error:   initializing argument 1 of `D<i>::D(void*) [with int i = 29]'
error:   initializing argument 1 of `D<i>::D(void*) [with int i = 23]'
error:   initializing argument 1 of `D<i>::D(void*) [with int i = 19]'
error:   initializing argument 1 of `D<i>::D(void*) [with int i = 17]'
error:   initializing argument 1 of `D<i>::D(void*) [with int i = 13]'
error:   initializing argument 1 of `D<i>::D(void*) [with int i = 11]'
error:   initializing argument 1 of `D<i>::D(void*) [with int i = 7]'
error:   initializing argument 1 of `D<i>::D(void*) [with int i = 5]'
error:   initializing argument 1 of `D<i>::D(void*) [with int i = 3]'
error:   initializing argument 1 of `D<i>::D(void*) [with int i = 2]'

Bumper has proved this inadvertent programming paradigm to be Turing-complete. As a result, I’m diligently coding a new interpreted web frame work called “Gronky!”. The output is generated exclusively with gcc error messages which are then consumed by Zeus just as a munted man consumes libations. Expect a feature-complete alpha upon the return of Hale-Bopp.

RSS Parser in 10 lines of Ruby or in 153 lines of C++

Posted on March 20, 2006

I know there’s like 10 libraries to do this already, but they all seemed a bit heavy to me. I just wanted to grab the items from an RSS feed. Plain and simple. What’s amazing to me is that it could be done in 10 lines of Ruby. This parser reads an RSS feed, and stores the descriptions, items, and links into a hash of arrays.


#!/usr/bin/env ruby
require 'http-access2'

h = HTTPAccess2::Client.new()
element = Hash.new {|h,k| h[k] = []}

while urlstr = ARGV.shift
  response = h.get(urlstr) { |data|
    data.gsub(/<(description|title|link)>(.*?)<\/(description|title|link)>/m) {
      element[$1].push($2.gsub(/\s\s+/m,' '))
    }
  }
end

site_description = element['description'].shift
site_title = element['title'].shift

Stupidly, I wrote the same code in C++ first because I thought it would run faster. After starting down that path, I found myself wanting to use ActiveRecord. So, I ported over to Ruby.

The C++ version is here if you really want to see it. It’s 121 lines of C++ with a 32 line h-file. Truth be told, it actually took me less time to code the C++ because I made a couple of mistakes in my Ruby version at first. All said and done, the Ruby version is a helluva lot easier to maintain than the C++ which I think is why I spent the time to do it in the first place.