Harsh J

Memoirs of a QWERTY Keyboard

Archive for the ‘Computing Issues’ Category

Changing the Google Chrome’s User Agent String

one comment

A simple google for changing the Google Chrome web-browser’s UA (user-agent) string would lead you to a horribly old, yet popular result that would ask you to basically hex-hack the chrome binary [Winks at labnol.org]. Ugh, ugh, ugly for 2010!

For those who don’t know yet, you can start a fresh chrome window with a custom UA using the –user-agent command-line parameter while launching it:

# Using chrome, or chromium -- as you like it.
chrome --user-agent="My User Agent String. Bow to it, statisticians. Kidding."

You can then visit http://whatsmyuseragent.com/ to verify the results of the user-agent property change in Chrome/Chromium.

The command-line parameter, –user-agent, is (not very) surprisingly not documented under the supplied manual page.

Written by Harsh

December 22nd, 2010 at 1:18 am

Swype Beta APK [Zeitgeist]

one comment

[This is a December 2010 Post -- mind it]

Here’s a quick way to download the now-available beta swype.apk if you don’t really want to download right onto your phone via their given link:

wget https://beta.swype.com/android/get/?download_NOW=androidBeta -U "Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1" -O Swype.apk

There, nice and easy?

Sorry Swype, to make you count a N1 download instead of an SGS.

Written by Harsh

December 22nd, 2010 at 12:56 am

Posted in Fun,Software

Modals and the spinning void

leave a comment

If you like modal dialogs, you love being annoyed and probably don’t care if you murder someone out there due to the rage that you didn’t know popped up cause of some piece of software pushing a neat looking, variable sized box with aligned text and a helper image onto your face.

Never has a modal dialog made you read itself to understand. It has always been about pressing the right button, to get back to whatever it was you wanted to do. As far as intentions of the developers, and to some extent the libraries go, they only want you to do whatever it is you wanted to do in the software’s right way. Press the right button, and maybe the software will do good to you. Press the wrong button and you’re probably doomed. Press the only button and you know you’re back to square one. If there are no buttons, then thank god for the display manager. If there is no display manager, you’re free of all annoyances.

There have been enough criticisms of modal dialogs that the Wikipedia page for the same has a section discussing it. But one hardly finds an application that does not throw a modal dialog on your face for whatever it is you did wrong or it did wrong (oh the horror).

Then there came the web, claiming to do away with most modal stuff as the web GUI programming model is hardly similar to the desktop model. Designers had a great chance to rid all the flaws of ancient desktop models and implement the best usability ways there could ever be, with support of some very powerful languages and associated libraries. What got popular instead, are things similar to Lightbox.

Such elegant are the looks of a lightbox implementation (an example of modal-ness), that it makes you feel all warm inside when you see it working on your own website, the feeling you get about never having to, or having your visitors to, leave the page they are on in order to see content associated to it; and to certain extent, browse horizontally across all other associated content than just one. It began okay first, click a media and it begins a modal environment that looks like Apple’s keynote events with a spinner. People could also open the link in a new tab to view the image in their browser’s image viewer (or other). Then, people couldn’t. Someone exclaimed with great delight that their JavaScript Kung-Fu was better than others.

Its one thing to pop a modal window and darken out all other content to let it have focus. It is another thing to add a spinning animation with absolutely no information, percentage wise or other, to indicate progress. Nobody cared if the spinner were enough to let the user know content’s actually loading or not because hey, they developed it in environments where the network was blazing fast and you hardly ever saw it. Progress. They just don’t get its meaning right. Space and time are measurable quantities and with content, progress is finite and must be calculated to be called as a progress animation. Fancy circular spinning darkness is anything but fancy. Even the Flash developers of the past, present and the future understood this mostly. But not the web developers, never ever.

Has no one ever noticed how a browser or image viewers worth their salt render images? Has nobody seen the progressive rendering of image data on the screen as it gets downloaded? It might make your network feel slow, but it never lets it feel non-functional.

Plague like the Lightbox (and associated beautiful modals) must be scrubbed out of the WWW. Let my clicks pass me to another page, I don’t mind. Nobody ever minded it until someone suspected they did.

Written by Harsh

September 25th, 2010 at 12:08 am

Justice enough

leave a comment

About the immutability of variables once bounded in Erlang:

Single assignment is like algebra.

When I went to school, my math teacher said, “If there’s an X in several different parts in the same equation, then all the Xs mean the same thing.” That’s how we can solve equations: if we know that X+Y=10 and X-Y=2, then X will be 6 and Y will be 4 in both equations.

But when I learned my first programming language, we were shown stuff like this:
X = X + 1

Everyone protested, saying “you can’t do that!”. But the teacher said we were wrong, and we had to unlearn what we learned in math class. X isn’t a math variable: it’s like a pigeon hole/little box…

In Erlang, variables are just like they are in math. When you associate a value with a variable, you’re making an assertion – a statement of fact. This variable has that value. And that’s that.

Joe Armstrong, in his book Programming Erlang.

Written by Harsh

August 7th, 2010 at 11:08 pm

Posted in Computing Issues,Fun

Tagged with ,

Sieve of Euler in Erlang

9 comments

Learning Erlang is a fun task. Solving PE problems with it even more. You can never really beat PE problems unless you have your own fast implementations of primality tests, prime generation sieves and so on.

A quick way to generate primes, as many would tell you, would be to implement and use the Sieve of Eratosthenes. Its a simple sieve that works by striking off all multiples of primes thus observed in sequence. Wikipedia has an illustration on its working technique.

There is another simple sieve derived from the same idea, known as the Sieve of Euler, which uses a filtering technique based on products of an encountered prime across the remaining elements. This sort of a sieve, again explained in a good manner at Wikipedia, is shorter and easier to implement in Erlang than the traditional Eratosthenes (and I mean proper Eratosthenes, mind you).

The following is the solution I came up with, a couple of days into learning Erlang for great good:

-module(primes).
-export([generatePrimes/1]).

% Sieve of Euler %

generatePrimes (N) when is_integer(N) and (N > 1) ->
    Numbers = lists:seq(2,N),
    BeginIndex = 1,
    generatePrimes(Numbers, [], BeginIndex).

generatePrimes (Numbers, Numbers, _Index) ->
    Numbers;

generatePrimes (Numbers, _PreviousNumbers, Index) ->
    FilteredNumbers = ordsets:subtract(Numbers, multiples(lists:nth(Index, Numbers), Numbers)),
    generatePrimes(FilteredNumbers, Numbers, Index + 1).

multiples (Prime, Numbers) ->
    lists:map(fun(Num) -> Prime * Num end, Numbers).

% Compile as c(primes). %


The function primes:generatePrimes(N) will generate all primes until N. On my 2.93 GHz machine it takes about 2.1s to generate all primes under a million (78498 primes).

As a bonus, you can go ahead and compute single-line inelegant solutions #3 and #10 of Project Euler as:

% Solution to problem 3 %
lists:nth(1, lists:dropwhile(fun(X) -> not(600851475143 rem X == 0) end, lists:reverse(primes:generatePrimes(trunc(math:sqrt(600851475143)))))).

% Solution to problem 10 %
lists:sum(primes:generatePrimes(2000000)).

Notes: There is some redundant multiple calculation involved but I noticed that if I perform a sublist operation to remove that it slows the generation down, so I let it be. The sieving operation also terminates when it notices that no number was filtered in one of its call, as this marks the proper end of it. For instance, to generate all primes under 100, it only sieves through the first five primes.

Update:

Kind commenter Angel (angel at uah dot es) posted a solution (and a multi-process one!) to a proper Eratosthenes’ Sieve (based on the O’Neill’s paper I referenced above), which goes below:

% "Genuine" Erathostenes sieve in erlang
% based on www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf
% 2009 Angel Alvarez
%
% Process  1     2     3     4     5     6     7 ....
%
%         02    03    05    07    11    13    17
% P 02 -> 04
% P 03    04 -> 09
% C 04 == 04    09
% P 05    06    09 -> 25
% C 06 == 06    09    25
% P 07    08    09    25 -> 49
% C 08 == 08    09    25    49
% C 09    10 == 09    25    49
% C 10 == 10    12    25    49
% P 11    12    12    25    49 -> 121
% C 12 == 12    12    25    49    121
% P 13    14    15    25    49    121 -> 169
% C 14 == 14    15    25    49    121    169
% C 15    16 == 15    25    49    121    169
% C 16 == 16    18    25    49    121    169
% P 17    18    18    25    49    121    169 -> 289

-module(multi_erathostenes).
-author("angel at uah dot es").
-compile(export_all).

compare(X,Y) when X > Y -> greater;
compare(X,Y) when X < Y -> smaller;
compare(_,_) -> equal.


worker(Prime,UpperBound)->
	receive
		{test,Number,ParentPID} ->
			ParentPID ! ok,
			case compare(Number,UpperBound) of
				equal ->
%					io:format("[Worker of prime: ~w (~w)] Found composite: ~w~n",[Prime,UpperBound,Number]),
					worker(Prime,UpperBound + Prime);
				smaller ->
					io:format("[Worker of prime: ~w (~w)] Spawn new worker for Prime: ~w~n",[Prime,UpperBound,Number]),
					NextPID =spawn(?MODULE,worker,[Number,Number*Number]),
					worker(Prime,UpperBound,NextPID)
			end;
		stop ->
		    noop
	end.

worker(Prime,UpperBound,NextPID) ->
	receive
		{test,Number,ParentPID} ->
			ParentPID ! ok,
			case compare(Number,UpperBound) of
				equal ->
%					io:format("[Worker of prime: ~w (~w)] Found composite: ~w~n",[Prime,UpperBound,Number]),
					worker(Prime,UpperBound + Prime,NextPID);
				smaller ->
% 					io:format("[Worker of prime: ~w (~w)] passing along: ~w~n",[Prime,UpperBound,Number]),
					NextPID ! {test,Number,self()},
					receive
					    ok -> ok
					end,
					worker(Prime,UpperBound,NextPID);
				greater ->
% 					io:format("[Worker of prime: ~w (~w)] passing along: ~w~n",[Prime,UpperBound,Number]),
					NextPID ! {test,Number,self()},
					receive
					    ok -> ok
					end,
 					worker(Prime,UpperBound+Prime,NextPID)
			end;
		stop ->
		    NextPID ! stop,
		    noop
	end.

start(N) ->
    OnePID=spawn(?MODULE,worker,[2,4]),
    cicle(3,N,OnePID).

cicle(Last,Last,Worker) ->
    Worker ! stop;
cicle(Current,Last,Worker) ->
    Worker ! {test,Current,self()},
    receive
	ok -> ok
    end,
    cicle(Current+1,Last,Worker).

Written by Harsh

June 6th, 2010 at 11:15 pm