Dividing a file into chunks along line endings in Erlang

I’ve been dabbling in Erlang recently. I’ve wanted to learn a functional programming language for a while now and Erlang’s concurrency make it rather attractive.

For my “hello world” app, I decided to write a simple log parser which processes chunks of a file in parallel. Here is a part of that app which produces a list of tuples which describes the chunks adjusted to the nearest newline (Unix newlines, \n, in this case).

[]

A Java String permutations utility

On a recent project I need to find all the possible permutations of a given URL. Stripping off subdomains, paths, and query parameters. Here is the first part of the solution. A method which takes a string and strips it down based on a given divider in a given direction at a given interval.

Here’s the code:

private static String[] getPermutations(String whole, String divider, int lim, int dir) {
	   String[] chunks = whole.split((divider.matches("\\.") ? "\\" : "")+divider);
	   System.out.println("chunks.length: "+chunks.length);

	   if(chunks.length <= lim) {
		   System.out.println("return whole: "+whole);
		   return new String[]{whole};
	   }

	   String[] permutations = new String[chunks.length-lim];

	   if(dir == 1) {
		   	permutations[0] = whole;
   		    System.out.println("permutations[0]: "+permutations[0]);
			for(int i = 1; i < chunks.length-lim; i++) {
			   String permutation = "";
			   for(int o = i; o < chunks.length; o++) {
				   permutation += (o == i ? "" : divider) + chunks[o];
			   }
			   permutations[i] = permutation;
			   System.out.println("permutations["+i+"]: "+permutations[i]);
			}
	   } else if(dir == -1) {
		   for(int i = 0; i < chunks.length-lim; i++) {
			   String permutation = "";
	 		   for(int o = 0; o < chunks.length-i; o++) {
				   permutation += (o == 0 ? "" : divider) + chunks[o];
			   }
			   permutations[i] = permutation;
			   System.out.println("permutations["+i+"]: "+permutations[i]);
			}
	   }

	   return permutations;
   }

Here is an example of it being used.

[]

Javascript color gradient calculator

Here is a color calculator I adapted from a friend’s PHP implementation:

Color = function() {
};

Color.hexdec = function(hex_string) {
    hex_string = (hex_string + '').replace(/[^a-f0-9]/gi, '');
    return parseInt(hex_string, 16);
}

Color.dechex = function(number) {
    if (number < 0) {
        number = 0xFFFFFFFF + number + 1;
    }
    return parseInt(number, 10).toString(16);
}

Color.pad = function(number, length) {
    var str = '' + number;
    while (str.length < length) {
        str = '0' + str;
    }
    return str;
}

Color.calcgrad = function(val, color1, color2) {

    if(!color1.match(/^#[0-9a-f]{6}/) || !color2.match(/^#[0-9a-f]{6}/)) return 'match err!';

    if (val > 1) {
        val = 1;
    }
    if (val < 0) {
        val = 0;
    }
    val = parseFloat(val);

    c1 = [Color.hexdec(color1.substr(1,2)), Color.hexdec(color1.substr(3,2)), Color.hexdec(color1.substr(5,2))]; //b
    c2 = [Color.hexdec(color2.substr(1,2)), Color.hexdec(color2.substr(3,2)), Color.hexdec(color2.substr(5,2))]; //r

    if (val < .5) {
        delta = [(c2[0] - c1[0]), (c2[1] - c1[1]), (c1[2] - c2[2])];
        arrColor = [c1[0] + ((delta[0] * val) * 2), c1[1] + ((delta[1] * val) * 2), c1[2] - ((delta[2] * val) * 2)];
    } else {
        delta = [(c1[0] - c2[0]), (c1[1] - c2[1]), (c1[2] - c2[2])];
        arrColor = [c1[0] - *(delta[0] * (val - .5)* * 2), c1[1] - *(delta[1] * (val - .5)* * 2), c1[2] - *(delta[2] * (val - .5)* * 2)];
    }
    return '#'+Color.pad(Color.dechex(arrColor[0]),2)+Color.pad(Color.dechex(arrColor[1]),2)+Color.pad(Color.dechex(arrColor[2]),2);
}
[]

Ext.ux.JSONP for ExtJS 4

Here is a solution for making simple JSONP requests in ExtJS 4

Ext.define('Ext.ux.JSONP', {
    extend: 'Ext.data.ScriptTagProxy',
    alias: 'ux.jsonp',

    createRequestCallback: function(request, operation, callback, scope) {
        var me = this;

        return function(response) {
            callback.apply(scope, [response]);

            operation.setCompleted();
            operation.setSuccessful();
            me.afterRequest(request, true);
        };
    }
});
[]

Simple message bus for extjs 4

Here’s a simple message bus/broker I wrote for Ext.js 4

Ext.define('Ext.ux.MessageBroker', {
    extend: 'Ext.util.Observable',

    statics: {
        instance: null,
        setInstance: function(i) {
            this.instance = i;
        },
        sendMessage: function(msg, data) {
            this.fireEvent('message',{"msg":msg,"data":data});
        }
    },

    constructor: function(config){
        this.addEvents({
            "message" : true
        });

        if(config && config.listeners) this.listeners = config.listeners;

        Ext.ux.MessageBroker.superclass.constructor.call(this, config)
    }

}, function() {
    Ext.ux.MessageBroker.setInstance(new Ext.ux.MessageBroker());
});

// Sending a message:
Ext.ux.MessageBroker.sendMessage("load_stock", {"symbol":"INTC"});

// Recieving a message:
Ext.util.Observable.observe(Ext.ux.MessageBroker);
Ext.ux.MessageBroker.on('message', function(msg) {
    console.log('message fired: ',msg);
});
[]

Introducing jpoxy, the simple json-rpc implementation for Java

werx-jsonrpc has been renamed to jpoxy (hosted on Google Code). But the project name isn’t the only thing that’s changed. Based on some excellent feedback I’ve made the following changes:

  • The project is now thread-safe. I’ve removed request and response as global objects and, instead, made sure they get passed along the request chain.
  • This means the events for request and response have been taken out (which, I’ve been informed, I didn’t make a big enough deal about last time). The only event that you will get now is the init complete event which will provide your application a copy of the servlet configuration.
  • You can still access the request object (and, subsequently, the session object) by accepting a single HashMap parameter in your method.
  • A new annotation has been added, @Jpoxy(enabled = false), which allows you to tell the framework not to expose a method if it is public. This means you can mark all your internal event listener code so they aren’t exposed to the public.
  • You can now expose methods which accept a complex Java object. This means you can expose a method which accepts, say, a User object and the framework will use Jackson to marshal the parameters you provide into that object.

We also now have a Google group! So if you have any questions or comments we would love to hear from you.

[]

Calculate a color gradient in Javascript

Here is a simple Javascript class to calculate the percent of a gradient between two colors given the percent (as a float 0-1) and two colors. Uses functions from php.js.

Color = function() {
};

Color.hexdec = function(hex_string) {
    hex_string = (hex_string + '').replace(/[^a-f0-9]/gi, '');
    return parseInt(hex_string, 16);
}

Color.dechex = function(number) {
    if (number < 0) {
        number = 0xFFFFFFFF + number + 1;
    }
    return parseInt(number, 10).toString(16);
}

Color.pad = function(number, length) {
    var str = '' + number;
    while (str.length < length) {
        str = '0' + str;
    }
    return str;
}

Color.calcgrad = function(val, color1, color2) {

    if(!color1.match(/^#[0-9a-f]{6}/) || !color2.match(/^#[0-9a-f]{6}/)) return 'match err!';

    if (val > 1) {
        val = 1;
    }
    if (val < 0) {
        val = 0;
    }
    val = parseFloat(val);

    c1 = [Color.hexdec(color1.substr(1,2)), Color.hexdec(color1.substr(3,2)), Color.hexdec(color1.substr(5,2))];
    c2 = [Color.hexdec(color2.substr(1,2)), Color.hexdec(color2.substr(3,2)), Color.hexdec(color2.substr(5,2))];

    if (val < .5) {
        delta = [(c2[0] - c1[0]), (c2[1] - c1[1]), (c1[2] - c2[2])];
        arrColor = [c1[0] + ((delta[0] * val) * 2), c1[1] + ((delta[1] * val) * 2), c1[2] - ((delta[2] * val) * 2)];
    } else {
        delta = [(c1[0] - c2[0]), (c1[1] - c2[1]), (c1[2] - c2[2])];
        arrColor = [c1[0] - *(delta[0] * (val - .5)* * 2), c1[1] - *(delta[1] * (val - .5)* * 2), c1[2] - *(delta[2] * (val - .5)* * 2)];
    }
    return '#'+Color.pad(Color.dechex(arrColor[0]),2)+Color.pad(Color.dechex(arrColor[1]),2)+Color.pad(Color.dechex(arrColor[2]),2);
}
[]

Fun with heatmaps

Recently I’ve been playing with a few new technologies. Some are new to me while most are simply new. My base project to use these technologies is a heatmap visualization of churches in Georgia. While heatmaps in themselves aren’t exactly exciting, having the ability to map more than 10,000 data points at a time in real-time is.

So here are the various technologies I used in the creation of my app.

[]

Risk Reduction Strategies on Facebook

[HT Bruce Schneier]

Super logoff

Mikalah uses Facebook but when she goes to log out, she deactivates her Facebook account. She knows that this doesn’t delete the account – that’s the point. She knows that when she logs back in, she’ll be able to reactivate the account and have all of her friend connections back. But when she’s not logged in, no one can post messages on her wall or send her messages privately or browse her content. But when she’s logged in, they can do all of that. And she can delete anything that she doesn’t like. Michael Ducker calls this practice “super-logoff” when he noticed a group of gay male adults doing the exact same thing.

[]

Debate: U.S. Airports Should Use Racial And Religious Profiling

The setup from Intelligence Squared:

On Christmas Day, 2009, twenty-three-year-old Umar Farouk Abdulmutallab attempted to blow up Northwest Airlines Flight 253 using explosives hidden in his underwear. A string of missed opportunities and errors by government security agencies culminated in what President Obama would declare a “systemic failure.” Is scanning everyone with expensive, high-tech equipment the best use of limited resources? Or should we use the information that we have—the knowledge that, while all Muslims are not terrorists, most terrorists are Muslim.

[]