echo $my_thoughts > /dev/null

Code, philosophy and scribblings

Implement Decorators in Javascript ES5

There’s an amazing pattern in Python, called function decorators. It’s a set of declarations at the top of a function definition, that slightly enhances how the function behaves without actually polluting the function definition with related logic. I find it a really neat way to define a function and to re-use these enhancements/additional logic across functions.

If you are like me, in love with decorators and now in the wildness of Javascript, missing them – here’s a quick way to implement decorators in JS. My solution here, uses underscore’s compose method. But it’s easy to whip up your own version of compose, if you don’t really want to use underscorejs.

Here’s the short version:

Say, you have a couple of decorators.

One, which logs the return value of the function it decorates.

Two, which makes sure the function is called exactly and only once, no matter how many times it gets invoked.

/* Decorator 1 */
var shouldLog = function(original_fn) {
  var decorated_fn = function() {
      var returnVal = original_fn(arguments);
    return decorated_fn;

/* Decorator 2 */
var runOnlyOnce = function(original_fn) {
  var isAlreadyRun = false;
  var decorated_fn = function() {
      if (!isAlreadyRun) {
          isAlreadyRun = true;
    return decorated_fn;

var myLittleFunction = function() {
  // here goes your function definition

// now decorate it.
myLittleFunction = _.compose(shouldLog, runOnlyOnce)(myLittleFunction);

Or you can go for another version, if you aren’t really a fan of two liners.

var myLittleFunction = U.compose(shouldLog, runOnlyOnce)(function() {
  // here goes your function definition

All done! Now every time you call your function, it passes through the decorators around it.

Now, for the longer version:

A decorator function is nothing fancy. It’s a function that takes a function as input and returns a function that invokes the original input function, with extra logic executed before or/and after the invocation. It might even decide to not invoke the function at all, like our runOnlyOnce decorator above.

If you like mathematics, you might better understand this:

Say, f(x) is your function Your decorator is g(f(x)) where g(x) = { invoke x with some additional code padding }

There might be more than one decorator that decorates a function. In fact, the real power of decorators lies in the mixing and matching of these decorators – so there’s likely going to be more than one decorator decorating a function.

With that, it’s more like h(g(f(x))) and it might grow infinitely.

We want to define the function f such that, when we actually invoke f(x), we actually do h(g(f(x))), where g, h are the decorator functions for f.

If you are into functional programming, you can easily tell that this is a function composition.

Underscore has a useful util to compose a set of functions.

_.compose(g, f) = g(f())

Remember we haven’t really executed the function. It’s just a composed higher order function. To execute it, you might want to do something like this:

var composed = _.compose(g, f)
composed(x) = g(f(x))

If you are very anxious about implementing decorators, you might have come up with your brilliant idea of

var decorated_fn = _.compose(decorator_1, decorator_2, actual_fn)

If yes, you actually are very close, but still incorrect.

Because that would technically mean,

decorated_fn(x) = execute actual_fn(x) and pass the return value to decorator_2 as parameter. When that finishes, again pass the return value to decorator_1 as parameter.

That’s not what we want. We want to execute actual_fn, only after passing through decorator_1 and decorator_2.

To get there, there’s only this tiny little change to our “brilliant idea”

We’ll first compose the decorators together. Then assign our actual_fn as the input for this composition to work on. This is like throwing a function at this composition, expecting it to spit out a function, that wraps this function with multiple layers of padding.

var decorations = _.compose(decoration_1, decoration_2)

var decorated_fn = decorations(actual_fn)

That’s it. Now when you call decorated_fn(), it’s going to funnel through the decorators!

Relax, if you can’t comprehend it fully. I suck at explaining things, sometimes. It will all get easy, if you quickly try it in a javascript console.

HexoPress Now Supports Plain Markdown Files

HexoPress, works primarily on Google Docs. But that’s not enough. There might be certain markdown elements, that you might want to use in your posts, that isn’t there yet in Google Docs – like, codeblocks?

I’ve got news.

HexoPress now understands markdown files the same way it understands Google Doc files. As usual, drop your .md files in your hexopress folder and you are good to go!

This Is Gold

This. Should frame and hang this somewhere in my new place.

A Systematic Approach to Understanding Complex Systems

There is a systematic approach for understanding complicated systems. All it takes is a bit of predicate logic skills and a fair amount of short term memory. Everything else is drawing patterns, connecting dots, thinking it the other way and similar things that our brain already is quite capable of.

To put it in simpler terms, we can just say that to understand a complex system: we’ll need to get a lot of facts about the system and enough space in our head to fit these facts into, so that drawing out meaningful inference out of them becomes an easy thing to do.

So how do you do this effectively? One way is to gain as much facts / data about the system as possible. One simple thumb rule is that if our motive is to change/innovate/rethink the system, we need to know *everything* about the existing system before trying to work on it. So before going into the rethinking mode, gain as much understanding as possible about how the system works currently.

Secondly, when your brain works out logical results out of these dots you’ve accumulated, question them ruthlessly. Drop a *Why* wherever possible. What you are looking for are those scarce little why-s that might lead you to an alternative route, maybe a better route with better outcomes or maybe not.

One effective way to do this is to have a mind-map, or written notes of the decision tree, of all possible questions and answers that comes out of a certain *why*.

The last step is something that you’ll intuitively know–picking a tree from this map of yours.

I’d like to take you through a little tour of how to understand a complex system with example, but I’ll need a lot more time to do that effectively. That can wait. Will do it sometime later.

Hello World!

This is my first blog post @ JFYI: I made

Whatever platforms there are now, are great. Except, there is a lock-down of your writings. The platform that you choose, essentially take your writings, packages them into something that the platform can understand for itself and hides them under its pillows, merely extending you with a corner to export them – all the while hoping, you will never need to.

What if the control is inverse?

What if all your posts are somewhere where you decide it to be? What if all these blogging sites are just a conversion layer that treats your files/writings as the primary database and generate a blog out of them?

Octopress is the only one which comes very close to this vision. There’s a tiny little problem though. It’s for hackers. It assumes we will have downright access to a text editor and git, every time we have a great idea that we need to write down – which is actually seldom right.

As for me, most of my “I bloody want to start writing now” moments happen when I am somewhere far far away from my desktop.

But wait, this is an already solved problem.

All these cloud people(Google & Zoho) have been empowering me to write down things, anywhere, on any device – which is exactly what I want now. It’s just I want these blogging softwares to understands what I put in these cloud filesystems.

That’s essentially what HexoPress is. I’m excited about what it will turn out to be.

Go ahead and start writing!

Interesting HN Links

Ask HN: What is the most exciting development in your field right now? - Startup ideas - Chainpad - a collaborative markdown editor, based on blockchains - ML algorithms written in Python - Pointers to resources for learning low-level programming. - 500 DS & Algo Interview Questions & Answers - Write your own simple operating system - ARM Assembly Intro - A detailed narrative on how to create a scripting language. - Programmer Book Collection - REST was never about CRUD - Product posting forffff ums - Best Textbooks for different fields