Recently I needed to parse a semi-structured long text document and convert it into a data structure. As a lazy programmer I did not want to copy and paste the text a thousand times by hand. My solution was quite simple: read the document line-by-line, keep track of each line that I have not successfully parsed yet in an array, and try to parse the array at the end of each iteration, and empty the array every time parsing succeeds. And repeat till EOF.

This is how parsers work generally. My little hack was easy to do only because I contained my logic inside reduce function. …


Recently I spent a good part of two working days decoding service workers to make an app work offline.

I hope this post clarifies some tricks and techniques for dealing with Cache Storage and Service Worker APIs and saves you some time.

function fetch (req : Request) : Promise<Response>

Fetch API provides a rich interface for creating request / responses in browser-side JavaScript:

var req = new Request('https://medium.com/');
fetch(req).then(res => res.text()).then(console.log);

We can add custom headers to this request, but note that the browser might override our headers. For example Chrome always overrides User-Agent and Referer. …


Promises are a well known solution to the Callback hell problem that arises in asynchronous JavaScript programs.

Borrowing some ideas from Functional languages, I am exploring a different approach to address callback hell in this post. This solution will be more general than Promises, in fact we will take advantage of these ideas to make Promises even more composable.

I use a notation similar to Haskell’s. But in many ways I will divert from rigid Haskell notation everywhere that I think it helps.

You only need to be familiar with Callbacks, Promises, and ES6 anonymous function (lambda) syntax to follow this post. …


In this post I’ll present a reusable function for timing async operations in JavaScript.

calc is an async function that we like to profile. By convention its last argument is a callback. We use calc like this:

calc(arg, (err, res) => console.log(err || res))

Perhaps the easiest way of profiling a function like calc is adding the timing logic wherever we need it:

const t0 = Date.now()
calc(arg, (err, res) => {
const t1 = Date.now()
console.log(`Log: time: ${t1 = t0}`)
console.log(err || res)
})

But this is not a reusable solution. Every time that we want to time a function we have to introduce a t⁰ at the outer scope and change the callback to measure and log the time. …


We usually breakdown the traffic of a campaign into its sources: the publishers (apps or websites) which display our ads. Often the quality of traffic from different sources varies. Some sources never convert while some have a few times higher conversion rate than the campaign’s average.

We like to detect high, low or never converting sources early in a campaign.

In a Cost Per Click (CPC) campaign we expect users to reach our website after clicking on a banner, then they will have a chance to convert. In reality a ratio of these users (hopefully a small ratio) get lost and never reach our website. …

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store