I'm sitting in Nashville, Tennessee this morning, planning how to make sure OOPSLA ends up another great conference this year.
One of those moments of inane absurdity that I seem to gravitate to came in confluence with the discussion of the LISP 50th Year Birthday Party and Dick Gabriel's penchant for poetry, I hit upon the notion of poetry in code.
What if we mapped code expressions to poetry? Can one come up with an analog to meter (e.g. lvalue rvalue rvalue, lvalue rvalue rvalue) and write code along that lines? Or rhyming. What would an analog be?
What I hit upon though, was to look at Haiku in code. Haiku in poetry sets the rule that your poem is 3 lines, of 5, 7, and 5 syllables each. I chose to interpret this in Smalltalk code as 3 statements, with 5, 7, and 5 program nodes per statement in the body of the method.
Rather than be creative myself, I just chose to mine the existing code base. Here's the expression I used to find methods that are written in Haiku:
results := OrderedCollection new.
CompiledMethod allGeneralInstances
do:
[:each |
source := each getSource.
([RBParser parseMethod: source]
on: Error
do: [nil])
ifNotNil:
[:tree |
tree body statements size = 3
ifTrue:
[wordCounts := tree body statements
collect: [:statement | (NodeCounter new visitNode: statement) count].
wordCounts asArray = #(5 7 5) ifTrue: [results add: each]]]]
NodeCounter is not a standard part of the system, here's a fileout of the code for that, it's simple.
So running this, I get a couple hits in a normal development image. Here's an example of Haiku in Smalltalk:
swap: oneIndex with: anotherIndex
| save |
save := self basicAt: oneIndex.
self basicAt: oneIndex put: (self basicAt: anotherIndex).
self basicAt: anotherIndex put: save
Run this on your code. Do you have any good Smalltalk Haiku methods? Or maybe you'd like to change your perspective and see if you can't write a method that follows these lines.