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.

Access Gmail from Ruby using Net::POP3 and Stunnel

Posted on July 20, 2006

I Googled around for this at one point, however, all I could find were ill-fated attempts by a couple of tortured souls on ruby-talk. Google offers POP3S access to Gmail, but, unfortunately, Ruby’s Net::POP3S module is still under development. POP3S is the much reviled and revered Post Office Protocol tunneled over a thick blanket of SSL obfuscation. Fortunately, the good Stunnel folks have anticipated this kind of thing and they’re giving out some code that can encode and decode SSL to a standard socket.

This was done on OS X, but Stunnel seems to work on most platforms. However, I wasn’t finding it in ports or fink. So, I had to roll my own stunnel binary.


wget http://www.stunnel.org/download/stunnel/src/stunnel-3.22.tar.gz
tar zxf stunnel-3.22.tar.gz
cd stunnel-3.22
./configure
make
sudo make install

Whew. So, after you’ve installed stunnel, you need to invoke it to redirect a local port to Google’s secure POP3 port located at pop.gmail.com on port 995. You can redirect any local port to Google’s pop server. I chose to use port 42 because that’s as good as anything else. Here’s my stunnel invocation.


sudo /usr/local/sbin/stunnel -c -d 42 -r pop.gmail.com:995

So, now you can use Ruby’s Net::POP3 to access your gmail from port 42 on your local machine. Stunnel will handle the details of SSL.


require 'net/pop'

pop = Net::POP3.new('localhost', 42)
pop.start('mrbeastly', 'sUp3Rs3kRiT!')
  if pop.mails.empty?
    puts 'No mail.'
  else
    i = 0
    pop.each_mail do |m|
    exit if i > 20
    puts m.pop
    i=i+1
  end
end

Running this code will display the first 20 emails in Gmail to your terminal. This is really a stopgap solution until the fabled POP3S patch gets accepted into Ruby’s Net library. This is rumored to be soon. So, I’ll keep my hopes up.

Reflection

Posted on July 19, 2006

Jim once believed that the following Ruby code constitutes an example of reflection.

def create(klass, value)
    klass.new(value)
end

g = create(Greeting, "Hello")
g.show

Neither introspection nor metaprogramming occur in this code. This is not reflection. However, Jim argues that since getConstructor exists in Sun’s Java reflect class and because the Ruby code accomplishes the same task as his Java example that this is reflection. This logic is tenuous. However, I can defeat it because his Java code does not exemplify reflection.

public static Object create(Class c, String value)
  throws Exception
{
  Constructor ctor = c.getConstructor(
    new Class[] { String.class } );
  return ctor.newInstance(
    new Object[] { "Hello" } );
}

public static void main (String args[])
  throws Exception
{
  Greeting g =
    (Greeting) create(Greeting.class, "Hello");
  g.show();
}

Using Java’s reflect class does not preclude that reflection has occured. If these examples are reflection, I could argue that the following C code is an example of reflection.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void* create(void* klass, char* value);

void* create(void* klass, char* value) {
  size_t value_length = strlen(value);
  klass = malloc(value_length+1);
  strncpy(klass,value, value_length);
  return klass;
}

int main() {
  char* Greeting;
  Greeting = create(Greeting, "Hello");
  printf("%s", Greeting);
  free(Greeting);
  return 0;
}

Oh dear. Why did I switch to Ruby anyway if I can reflect in C?

Maltreatment.

Posted on July 18, 2006

“You’re maleficent!”
“Mellifluous?”
“No, MALEFICENT!”
“Magnificent?”
“MA-LIF-EH-CENT!”
“Mellophone?”
“Why do I even talk to you?”

Metaprogramming with Darkie The Fridge

Posted on July 12, 2006


m = "mr_beastly_"
(1..50).collect { |darkie_the_fridge|
  thefun = m + darkie_the_fridge.to_s + "!"
  eval %{
    def #{thefun}(cinnamocha)
      puts cinnamocha * #{darkie_the_fridge}
    end
  }
}
mr_beastly_5! 'boogiebot '

The result of this is boogiebot boogiebot boogiebot boogiebot boogiebot.

Garmin Forerunner 201 Track Overlays Using Google Earth Plus and Photoshop CS2.

Posted on July 02, 2006


The GPS track overlay images coming out of Google Earth Pro aren’t doing it for me. One can barely see the immutable one pixel wide track on the map. What’s a hardcore geek to do? Well, break out some mad h4x0r Photoshop skills, that’s what. I invented a new process whereby I make a non-antialiased (aliased?) 4-pixel wide trace over the wonky-ass default one, and then apply a stroke and bevel layer style. So, if you have a Garmin Forerunner 201 GPS Watch, a copy of Photoshop CS2, a Google Earth Plus subscription, and run about on somedays, you should do this and put it on your Flickr Pro account.