Hi, I'm Akaash.

My apologies in advance.

Buddhism & Programming

What happens when a guy’s vocational interest in Programming meets his recreational fascination with Buddhism?

A thought-provoking (albeit, unproductive) evening spent trying to understand how programs should view time.

Every time I’ve humoured my interest in Buddhist philosophy, I’ve always been struck by how its principles run orthogonal (if not downright antithetical) to those of Programming. Where Buddhism relies on the concepts of anicca (impermanence of being), dukkha (imperfection) & anatta (not self) to characterise sentience, programs depend on hard-edged entities, precision & unambiguously defined rules & identities. Sunyata (a state of emptiness) is about the last thing a programmer experiences when he’s going about writing his code. And while the Noble Truths of Buddhism propound an outlook that should be bereft of expectations, Programming is all about defining deterministic systems that conform to the expectations of those that build and use them.

So, ostensibly, it would appear that there isn’t much for Programming to learn from Buddhism.

Recently, though, I was watching Rich Hickey’s talk, Are we there yet?, on the current state of programming. In it he talks about how the widely observed design practices of Object Oriented Programming (OOP) have come to conflate the actually distinct concepts of entities (putative things) & states (values assumed by entities). This is a consequence of how our brains have been trained to perceive reality.

A great analogy Hickey uses here is that of a river. He leads with this fascinating quote:

No man ever steps in the same river twice.

~Heraclitus

As humans, we use abstractions to make sense of the world around us. The ‘river’ is but an abstraction for a large stream of water (which in itself is also an abstraction; but let’s not overcomplicate this for now) flowing from one point to another. But when broken down, it’s nothing but some water at a point in space and then later other water at that same point. ‘River’ is just the identity we’ve superimposed on the water and the continuum of its states at different points in time. In doing so, we’ve succumbed to one of the pitfalls of OOP, wherein we fail to distinguish between entities and their many states over time.

This is where Immutability (especially as implemented in languages like Clojure) shines by preserving all the states of entities over time and treating them as what they are — unchangeable snapshots of the entity’s existence at specific points in time. So, when viewed, the entity is representative of its state only at that point in time. This makes the entity immutable. The entity never changes, it just associates itself with different, causally-related states.

Poetically beautiful, is how I’d describe the resonance this has with the Buddhist view of time — which is that there is no time. Only an illusion of it; that stems from the series of “epochal transitions” we observe entities going through. Nowhere is this view more eloquently articulated than in these lines from one of my favourite books, Siddhartha. In them, the protagonist, Siddhartha, arrives at an epiphany after spending time by a — wait for it — river!

“Did you,” so he asked him at one time, “did you too learn that secret from the river: that there is no time?”

[…]

“Yes, Siddhartha,” he spoke. “It is this what you mean, isn’t it: that the river is everywhere at once, at the source and at the mouth, at the waterfall, at the ferry, at the rapids, in the sea, in the mountains, everywhere at once, and that there is only the present time for it, not the shadow of the past, not the shadow of the future?”

If programs were to also give up this illusion of time and embrace the concept more as a progression of instants, a succession of indelible ’now’-s, we’d actually be moving to a model much better equipped to handle the challenges of concurrency & parallelism, that arise with modern computing architecture.

It’s funny how two seemingly divergent outlooks — one based on immutability and another entrenched in impermanence can find common ground on something as fundamental as time. But it’s nice to find that Programming can in fact learn from Buddhism, even if it involves a significant amount of un-learning on the part of programmers in viewing not just their code, but also the world.

Words! What’re They Good For?

Edgar Rice Burroughs once famously called all humans “creatures of habit”. And while I cannot, in all honesty, extricate myself from that generalisation, I’m definitely not one who unqualifiedly subscribes to the power of ritual.

Friday evening hijinks being the exception, of course.

Because if there were ever a ritual that stood for all that was good and noble; one which separated us from the savages – it would have to be the exercise of seeking merriment after a long, arduous work-week. In my personal experience, numerous factors have contributed to the long-standing charm of Friday evenings – alcohol, company, but most importantly… conversation.

Yep! That ancient, endangered artform that we’ve happily traded in for uninterrupted access to Whatsapp messages, work e-mails, and Facebook updates. I mean, I like “social” networking as much as the next guy, but nothing will ever compare to the satisfaction I derive from some good ol’ solid, non-electronic conversation.

An excellent illustration of this being last evening, when a friend and I were discussing the kind of lyrical content we tend to appreciate in our music. It started with him expressing his admiration for Nas’ N.Y. State of Mind and some of the lines in them. In particular, these:

It drops deep as it does in my breath

I never sleep, cause sleep is the cousin of death

Beyond the walls of intelligence, life is defined

I think of crime when I’m in a New York state of mind

Rap Genius has annotated explanations (some by Nas himself) for the entire song, so I’m not gonna go into that. But what was particularly interesting about this was that while I found the song and those lyrics undeniably powerful, I didn’t quite share the same level of appreciation as my friend for that brand of song-writing. Let the record show that I think Nas is as good a rapper as any, and this is in no way a criticism of him. The guy is a bona fide genius. My point is about the larger theme of abstruse lyricism. Turns out what I appreciate slightly less about Nas (and other lyricists such as him) is exactly what everyone loves him for – his intentionally open-ended rhymes.

(Interesting aside: this is exactly what Jay-Z used as ammunition against Nas in his diss track, Blueprint 2)

My appreciation is a lot stronger for lyrics that paint a very vivid picture and leave it to you to extrapolate the interpretation of it to your own life experiences. The counter-example I offered was from a song I was listening to earlier that day.

Well all the Apostles, they’re sitting in swings

Saying, “I’d sell off my Savior for a set of new rings

And some sandals with the style of straps that cling best to the era”

That’s the first verse from Modest Mouse’s Bankrupt on Selling. While the song’s about the pitfalls of capitalism, the lyrics above although very explicit in what they’re talking about are equally applicable to the theme of the song as they could be to greed and/or betrayal. The fluidity with which the song plays to your current mood in the moment is what I appreciate more than the brilliance that you might uncover during a post-mortem dissection of the lyrics.

I hadn’t hitherto realised this about my preferences towards lyrical content, but once I had, it came as no surprise that my admiration has always been particularly strong for artists such as Bruce Springsteen, Morrissey, Ben Gibbard, etc. who find beautifully vivid ways of presenting the simplest of concepts, thereby lending it just enough ambiguity to make it feel different to every ear on every play.

Extraction-Based Text-Summarization: A Naive Approach

I had always found Natural Language Processing (NLP) quite interesting, and a few times had toyed with the idea of exploring it a little more seriously. The buzz around recent YC alum, Wit.AI, finally spurred me into action and I decided to give it a shot.

As with anything, the toughest part was getting started. NLP, despite being a discipline in its nascency, is actually quite expansive in its scope. So, I decided to go for low hanging fruit and start with text-summarization.

I came across this algorithm. It seemed to take a naive extraction-based approach to text-summarisation and looked like something that should’ve worked well. Upon using an implementation of it on random articles from the internet, I was somewhat disappointed with the results. That’s when I found this implementation of essentially the same algorithm, but with a slight tweak that improves results significantly.

To explain it briefly, the algorithm converts the text into a fully-connected, weighted graph, wherein every sentence is a node in the graph and its edges connect it to every other sentence/node in the graph. The weight associated with every edge is an “intersection score” which quantifies how much the two sentences connected by the edge have in common. Finally, the sum of all the interesection-scores/edge-weights of a sentence/node is determined. The sentences with high scores are assumed to be “key sentences” because they have more in common with the other sentences and are thus included in the summary.

I’ve created a small app that uses the modified implementation here. And the code for it can be found here.

Running a Python Script From Node.js

While working on something today, I needed to decrypt an RC4 encrypted string on Node. I was receiving the encrypted string from another server, so the encryption wasn’t really under my control. Given that there are a bunch of pre-written NPM modules out there that do RC4 encryption/decryption this wasn’t supposed to be a particularly difficult task. However, this seemingly trivial job turned out to be quite the timesuck.

None of the crypto modules I tried were able to correctly decrypt the encrypted string, which I’m guessing meant that the other server was probably doing something funny during the encryption process. The organisation whose server I was communicating with had provided a Python code snippet that had an implementation of the RC4 decryption algorithm, and this particular implementation was working for the encrypted strings being returned.

As a quick-fix, I decided to just call this Python script from my Node server, pass it the encrypted string and receive the decrypted string as output. To do this in Node, you need to spawn a child Python process.

1
2
3
4
5
6
7
8
9
10
11
12
13
var terminal = require('child_process').spawn('python', ['rc4.py', secretKey, encryptedString]);;

terminal.stdout.on('data', function (data) {
    console.log('stdout: ' + data);
});

terminal.on('exit', function (code) {
    console.log('child process exited with code ' + code);
});

terminal.stderr.on('data', function (data) {
  console.log('stderr: ' + data);
});

The arguments to the spawn function are the child process command name (in this case ‘python’ since I was trying to run a python program), followed by the array of arguments needed by the child process (in this case the name of the Python script, the encryption key and the encrypted string, respectively).

This worked quite well, but seemed somewhat hack-y in nature. So I might have to revisit this later and see how to do it directly within the Node process itself.

Self-centeredness

Here is just one example of the total wrongness of something I tend to be automatically sure of: everything in my own immediate experience supports my deep belief that I am the absolute center of the universe; the realest, most vivid and important person in existence. We rarely think about this sort of natural, basic self-centeredness because it’s so socially repulsive. But it’s pretty much the same for all of us. It is our default setting, hard-wired into our boards at birth. Think about it: there is no experience you have had that you are not the absolute center of. The world as you experience it is there in front of YOU or behind YOU, to the left or right of YOU, on YOUR TV or YOUR monitor. And so on. Other people’s thoughts and feelings have to be communicated to you somehow, but your own are so immediate, urgent, real.

-David Foster Wallace

Init

Captain’s log, Stardate -308789.30679223756.

Been a while since my last foray into blogging, so I’ve had a while to un-learn all the reasons I shouldn’t be doing this. Here goes!