I did a talk at the Copenhagen Ruby Brigade (Ruby Meetup Group based in Copenhagen) about my way to have fun whilst programming.
The topic at hand was “WTF Brainfuck? A golfing experience”.
These are my notes from that talk.
Imagine an endless stream of buckets.
We can add, subtract, read, and write to each bucket.
The entire spec for the Brainfuck language is as follows:
"Hello world!\n" program is thus written:
If we wanted, we could easily write a Brainfuck interpreter in Ruby .
We start, by having it load the Brainfuck program from a file:
Now we can run it with
With this it’s easy to go on, implementing each of the instructions from the spec above.
The only problem is
], where we need a depth counter, to count nested loops, and then move over these in a correct manner.
We end up with this 40 lines (650 bytes) program.
Now lets go golfing!
Code golf is a type of recreational computer programming competition in which participants strive to achieve the shortest possible source code that implements a certain algorithm.
DISCLAIMER: Don’t do this to your production code. This is for fun and learning only!
We can mess alot around with different stuff, learning a lot doing so.
Did you know that
$<.read will read the file contents of a file given as argument?
Did you also know that
$><<"hello" will print
t[?+] will return
nil otherwise? (Thus enabling us to short circuit our statements)
When we are done goofing around, our Brainfuck interpreter will look something like this (only 234 bytes):
This is indeed a Brainfuck!
And it messes up the syntax highlighter… Brilliant!