Oct
4
2014
Multiple inheritance and multiple dispatch in JavaScript

Multiple inheritance and multiple dispatch are core features in CLOS. The standard JavaScript object system supports neither, but they can be implemented. I thought I'd write this article, along with working code, for the JavaScript fellows who don't know the concept. Maybe someone smarter than me will come up with an efficient implementation.

Jun
8
2014
How to implement a programming language

I wrote a tutorial on how to implement a programming language in JavaScript. It got quite involved, and it took embarrassingly long to write. I wanted to write only about parsing, after a discussion with my dad about this problem; but once I had a parser I thought I'd add in a simple interpreter and once that was in place, I thought I'd turn it into continuation-passing style to show how we can workaround the lack of tail call optimization in JavaScript. But this opened the opportunity to talk about continuations. And of course, it was too slow so I thought I'd discuss compiling as well.

Long story short: I describe how to implement a non-trivial programming language. We get to a language that has decent performance, can interface easily with JavaScript and can offer first class continuations. To hell with the callback hell!

Read on and let me know what you think.

Jan
22
2014
Find all expressions that evaluate to some value

Some friends came to visit last weekend, and we got to talk about the following problem, which seems to have made the news headlines (it was given at a math contest for ten year olds and apparently many math teachers couldn't figure out a solution):

We have numbers 1, 3, 4 and 6. Create an expression using any elementary operators (+, -, *, /), containing only these numbers, exactly once each, such that the result is 24.

(if you'd like to try finding a solution, stop reading now because there's a spoiler here)

I felt the problem itself is inappropriate for 10 y.o. kids and irrelevant for a maths contest; but it seemed like a good programming exercise, so I told'em “while you're banging your heads to guess a solution, I'll go and write a program to give me all solutions.”

I implemented it in Scheme, because it requires backtracking and using continuations for this is a joy. But in this blog post I'll work out the solution in JavaScript.

Mar
20
2013
Using UglifyJS for code refactoring

Somebody asked me the following question:

I want to do AST transformations on my javascript files. Specifically, i want to take all `throw "string"` expressions and replace them with `throw new Error("string")` expressions.

The problem is I want to make no other changes to my source. I want to not change anything: no comments, no indentation, no whitespace, etc. I just want match certain AST subtrees (a throw node with a string node as a child) and replace them with something different.

I thought I'd write this blog post to show how.

Feb
27
2013
Livenode — Live-code your NodeJS application

I'm working on a NodeJS application for Kendo UI. If you've been following me for a while, you probably know that I'm not the biggest fan of NodeJS—however, it seems that for this project Node is the best option, so I actually pushed for it.

A small annoyance that I'm trying to “fix” is the requirement to restart the server all the time in order to test changes. After some years with Common Lisp, I know there are better ways to do development than restarting the program — so here's Livenode, a module that allows to “live-code” a NodeJS application.

Jan
11
2013
Enterprise tools

Enterprise tools take an idea which is essentially simple and beautiful, and place a mountain of mud over it: acronyms, nouns, APIs, XML (and XSLT, DTDs, SOAP!, WSDL!) and the list is endless. Such that you don't see the beauty anymore; you don't see it simple anymore. You then live in constant fear that everything has to be enormously complex, ungodly hideous, and you thank your Enterprise Tools Vendor, your new God, for providing you with an IDE that makes life bearable, completely losing sight of the fact that it's the Enterprise Bullshit itself which adds in the complexity. You're locked to your Vendor, and you're doomed to never see beauty again.

Nov
13
2012
Thoughts on CommonJS / RequireJS

This rant is to explain this tweet. The short version is, I think they introduce more problems than they solve. While I do admit there is a need to define packages in JavaScript, it should probably be addressed at language level. In this fabulous class, Gerald Jay Sussman says “I consider being able to return procedural values, and therefore to have first-class procedures in general, as being essential to good modular programming” and continues saying that first-class functions cover all modularity needs, including making package systems. That's certainly true, but no language I know, not even Scheme, relies solely on that for defining a module system. Except JavaScript.

Nov
8
2012
Should you switch to UglifyJS2?

Every now and then somebody asks me if UglifyJS v2 is ready for production use. I'd say it is, but it's hard to back that claim without a lot of users. Version 1 got some huge popularity boost after John Resig's twit about it and after jQuery switched to it. Thanks to being so popular, a lot of issues were identified and fixed. Something similar needs to happen to V2.

Here are some views on the status of V2 compared to V1 and other compressors.

Oct
16
2012
UglifyJS online demo

The UglifyJS2 API documentation pages are more or less complete. I also worked on an online demo which allows you to minify scripts from your browser; the intended use case is just to demonstrate features of UglifyJS like compressor/beautifier options and source maps. You can minify a script and click some token in the minified output and the editor will move to the original position of that token in the initial code. You can easily see all the warnings that UglifyJS dumps, and click them to go to the original location and fix them.

It's only tested with Chrome and Firefox. Should work with Safari too, but I'm pretty sure it won't work in Opera or IE. Try it out! If it doesn't appear to work and you visited this website recently, please clear the cache. If you have any comments or suggestions I'd love to know about it!

Oct
4
2012
UglifyJS — why not switching to SpiderMonkey AST

Yesterday I wanted to publish version 2 on NPM and make it official, but I hit this NPM issue. Seems it's now fixed, but I'm holding it off to finish a new feature that I'm working on (import the SpiderMonkey AST), so you still have to use the Git tree to play with it.

I completed the CLI tool with a bunch of new options and wrote the documentation in the README. It's now possible to keep certain comments in the output. Still no API documentation so far, but it'll come shortly (I'll actually have a dedicated section on this website for UglifyJS).

Next, some points about why I'm not switching to the SpiderMonkey AST internally. Tl;dr: I don't quite like it. Also, I'd kinda have to rewrite the mangler and compressor from scratch. That's Esmangle's job. Any case, UglifyJS will support the SpiderMonkey AST by converting it to our internal AST.

« Before 4 Oct 2012
Fork me on Github