Jonathan. Frech’s WebBlog

Decoupled fizzbuzz (#262)

Jonathan Frech,

Cogniscent of the baggage associated with this timeless kids-game-turned-into-interview-ques­tion, sparing its hiring efficacy and undeniable tedium when im­ple­ment­ed again and again in yet another C imitation, I believe that the lack of its demise is in parts due to it being just shy of trivial with regards to all three pillars of imperative com­put­ing (control flow, i/o and data).
Contrasting a functional pearl — which is a dazzling S-Ex­pres­sion found out there in con­cep­tu­al cosmos —, I want to describe fizzbuzz as an imperative nut: ef­fort­less­ly consumable when bought already cracked and put into a bag on a store shelf, yet unexpectedly hard to crack into two clean halves by oneself. I feel its im­ple­men­ta­tion often beckoning me, enticing me with forthcoming elegance, on­ly to turn around and show its ugly face of cumbersomely construed i/o calls and disconcertingly intertwined ex­e­cu­tion paths.

Following an over-engineered ap­proach in Haskell utilizing overlapping instances to not discriminate against index or periodicity (decoupled-fizzbuzz_overlapping-instances.hs), I chose to try the oth­er extreme of think­ing about both streams of dif­fer­ent origins and on­ly splicing them appropriately (decoupled-fizzbuzz_decoupled.hs). To further decrease apparent coupling, I fi­nal­ly hid the branching away inside a sort (decoupled-fizzbuzz_decoupled-ifless.hs):

main = mapM_ putStrLn . take 100
    $ zipWith3 (\n s z -> head . reverse . sort $ [show n, s ++ z])
               [1..] (cycle ["","","fizz"]) (cycle ["","","","","buzz"])

Fascinatingly, the often seen ap­proach of leeching periodicity of off the indices’ arithmetic prop­er­ties has vanished completely to the point of having two mutually oblivious data streams being merged based on their intrinsic willingness to provide non-empty data.

Thus born was the basis for a vim im­ple­men­ta­tion of fizzbuzz. Not a vimscript im­ple­men­ta­tion — which would presumably not bring any­thing new to the table — but in non-branching, linearily typed vim keystrokes (decoupled-fizzbuzz_decoupled.vim):

i1<Esc> qiyyp<C-A>q98@i
qfkAfizz<Esc>kkq32@f qb4jAbuzz<Esc>jq19@b
:%s/^\d*\ze[fb]<Enter>

It is not yet clear to me how to transform arbitrary branching decisions into decoupled blind text manipulation tasks, where every branch has somehow be­come an ef­fect of a tex­tu­al arbiter in­tro­duced for a niche action. How­ev­er, I cur­rent­ly entertain hopes of coaxing vim-y edits into a scripting lan­guage more attuned to ed­it­ing tasks and with less translational friction than traditional tools including regexp+ and fully-fledged Turing com­plete programs can offer. The Kolmogorov prob­lem of fizzbuzz is in my view a convincing demonstration of the presence of un­tapped potential in this domain.