Executing Process in the Background by Default in Bash

I was trying to solve this problem in this blogpost, but only now I found the solution I wanted.

To execute a program in the background (that is, without having to close it to continue using the shell) and to also hide the shell output of open and close notices, the & must be inside the subshell and error output redirected to /dev/null:

  ("C:\Program Files\Sublime Text 2\sublime_text.exe" $* &) 2> /dev/null;

True Mod Implementation in CoffeeScript

Reading this proposal I got curious as to which “true” or mathematically correct modulo implementation is the most efficient.

ECMA languages (Javascript, with CoffeeScript, and Actionscript – but also others like C++) have surprising modulo implementation which returns, most noticeably, an unexpected negative result. Here is a comparison of the differing values:

modulo   -3  3    js %   -3  3
         -----           -----
    -5 | -3 -2      -5 | -3  3       -3 % 5 = -3     
     3 |  0  0       3 |  0  0
     5 |  2  3       5 | -3  3

The surprising one is -3 % 5 = -3 and once doing anything related to mathematics, this can come up quite easily.

It seemed to me that the proposed implementation of the true modulo couldn’t be the most efficient, since it is performing Javascript modulo twice.

(a % b + b) % b

Another proposed definition was closer to my idea, using % only once, and it’s author claimed it covers some additional edge cases (I can’t seem to find them though). It uses bit-wise XOR and as it turned out from my benchmarking, XOR is pretty slow in Javascript (which might possibly have something to do with Number’s internal representation). I improved it by using != operator and compared it with my version. Here are the four functions I tested:

fastMod = (n, base) -> 
  if (_result = n % base) < 0 && base > 0 || _result > 0 && base < 0
    _result + base
  else _result
originalMod = (n, base) ->
  (n % base + base) % base

saferMod = (n, base) ->
  unless (jsmod = n % base) and (n > 0 ^ base > 0) then jsmod
  else jsmod + base
fasterSaferMod = (n, base) ->
  unless (jsmod = n % base) and ((n > 0) != (base > 0)) then jsmod
  else jsmod + base

And here are some of the results, ran on varying ranges of operands:

mine:         | 0.12 | 0.128 | 0.14
original:     | 0.23 | 0.219 | 0.234
safer:        | 0.24 | 0.185 | 0.203
safer faster: | 0.17 | 0.15  | 0.168

I used Firefox on a machine with Core i5, IE results were quite similar (just a bit slower).

My version turned out to be almost twice faster than the original proposed implementation and only slightly slower than pure % (well, the overhead of a function call is probably overshadowing the difference). The code expansion is much longer though and the real question is how often this is the bottleneck of a given application. But with CoffeeScript being increasingly used as computational tool, getting a fast implementation might be important.

To try out my test suite, open the Hot&Cold Editor with this link provided, copy the functions at the start of the code and invoke the test with compare() (use :help for help with the editor).

Using jQuery in Windows 8 Apps

This post should provide a summary of the steps I took in order to fully use jQuery in Windows 8 app development.


Because of MS policy on innerHTML (docs), I had to replace all its uses in jQuery before being able to use it. I took the non-minified version of the library and used Sublime to execute RegExp replace:

( +)(.+)\.innerHTML = (.*);
$1WinJS.Utilities.setInnerHTMLUnsafe($2, $3);

(Not the simplest and not a bullet-proof expression, but works fine for current version and should do most of the time.)

VMPlayer Disk Encoding Error

My computer died while having a virtual machine on (Mac OS X Lion hosted by Windows 7). After starting the computer again, VMPlayer reported wrong encoding in configuration file and after fixing it (commenting the encoding line out or changing it to UTF-8) it couldn’t read the main virtual hard disk, reporting

Disk Encoding Error

What ultimately helped was restarting the VMPlayer, possibly meanwhile trying to open the vm in VMWare Workstation (which also failed).

Arithmetic Operators in Parentheses-Less Languages

In languages where a function call doesn’t require parentheses, the binary arithmetic operators pose an interesting problem. Most obviously, the issue can be seen when considering negation

a - 2

Does it mean a minus 2 or a(-2)? Surprisingly, CoffeeScript allows a plus to precede a number, and handles both plus and minus the same way:

a + 2 # denotes a plus 2
a +2 # surprise: a(+2), so: a(2), or: a 2 in CoffeeScript
a+2 # a plus 2 again
a+ 2 # obviously, a plus 2

Spaces do make a difference!

Java Overriding Abstract Methods in Different Packages

Java might not be as safe as you might think. Consider following:

abstract class A {
  abstract void display();

abstract class B extends A {
  void display(){

Everything is fine as long as the two classes reside in the same package. Once you move them to different packages, all hell breaks loose. The compiler will only report that class B doesn’t implement display(). What it doesn’t tell you, is that you have problems with privileges. When you change both declarations to public, everything works fine.

Why did this occur? I forgot to specify the access level modifiers – never using protected in Java is the way to stay safe!

Sublime Text 2 – Make Input Fields’ Background Dark

To change the default white background and black text in search and replace and other input fields (to match the rest of default UI theme), use these values from Soda UI theme in Packages/ThemeĀ  – Default/Widgets.stTheme for settings: