The Faulty Equipment Blog

Experimenting with JS

As much as I love Golang and Haskell, Coffeescript and Javascript remain probably my favorite languages, it’s a language with many uses (client/server), easy to hack in and fun to experiment with.

In the last weeks I’ve been casually opening a JS Bin instance every now and then and just experimenting things.

For those who know me, they’ll already know I’ve been using JS Bin for doing Speedcoding some months ago, or the software renderer project I started but never finished because my knowledge on the field of rasterization is not as much as it is required to attempt such a thing.

After seeing an article from the sicilian code god antirez about Tcl (Tcl the misunderstood) and its flexibility, I tried to implement some functional paradigms into JS using only the builtin language constructs, no source modification/recompilation or pseudomacros or anything. DISCLAIMER If you think you know JS just because it looks like Java, these next snippets might give you an headache.

What I’ve made are those (check the JSbin links for usage examples and trying them out):

Partial functions

jsbin.com/gipog/2 (Code + examples)

// Partial function builder
var p = function() {
    var args = Array.prototype.slice.call(arguments);
    var func = args[0];
    var params = args.slice(1);
    return function () {
        var args = Array.prototype.slice.call(arguments);
        var endp = params.concat(args);
        return func.apply(this,endp);
    };
};

Partials were the first thing I’ve implemented, and it already involved doing some arguments magic (see the Array.prototype.slice calls) The compromise for not using recompilation is.. not that bad! Instead of having builtin partials like Haskell where you do let part = func 3 you have to do var part = p(func,3);

Pattern matching

jsbin.com/cozal/1 (Code + examples)

var Matcher = function() {
    var types   = [];
    var matches = [];
    this.match = function(argtypes, func) {
        types.push(argtypes.join(','));
        matches.push(func);
    };
    this.func = function() {
      args = Array.prototype.slice.call(arguments, 0);
      argtypes = args.map(function(a){ return typeof a; }).join(',');
      index = types.indexOf(argtypes);
      if (index < 0) return undefined;
      return matches[index].apply(this, arguments);
    };
};

Due to Javascript not having static typing (or static annotations) this resulted in the final code being uglier than what it could have been, but it works surprisingly fine.

Function composition

jsbin.com/fuped/2 (Code + Examples)

var c = function() {
    args = Array.prototype.slice.call(arguments, 0);
    return function() {
        var result = Array.prototype.slice.call(arguments, 0);
        for (var i = args.length - 1; i >= 0; i--) {
            result = [args[i].apply(this, result)];
        }
        return result[0];
    };
};

Function composition is simple in its concept and thankfully that also translated in a simple implementation.

Minor things I’ve done

jsbin.com/wawuj/3
Limiting the execution of a function of 1 every X milliseconds

jsbin.com/guzag/1
Lazy execution.. but doesn’t work properly

jsbin.com/fadul/2
Implementation of maps in Javascript as JS objects only have string as keys.. though Maps are actually in ES6 and I didn’t know it.. boo me.

So that’s the experimentation I’ve done so far. If you’d like to see more try visiting padrepio.in where I post links to some of my other experiments, like my incomplete Lisp interpreter made for fun or my HTML encryption tool.