SBT+Scala+Jetty = Elegance

Posted by – December 1, 2011

Scala + SBT. Starting to look at “server stuff”. We can do servlets in Scala like this:

https://github.com/schmmd/helloworld-scala-servlet/

Also, can easily integrate web sockets in the following way

https://github.com/yuroyoro/scala-websocket/blob/master/chat/src/main/scala/com/yuroyoro/websocket/ChatServlet.scala.

You can use Scala+SBT combo with any text editor. There are bundles for TextMate and Sublime Text 2. However an SBT plugin offers IntelliJ IDEA integration, which when paired up with the Monokai color-scheme offers the best in developer experience.

Also, check out oneJar sbt plugin that enables you to package your work from SBT as a runnable jar.

A superb undocumented UIKit call for printing view hierarchies

Posted by – November 4, 2011

Break somewhere inside a UIView and type

(gdb) ...
(gdb) po [self recursiveDescription]

For a rather shallow hierarchy I get something like this

<CustomView: 0x6b292b0; frame = (0 20; 768 1004); autoresize = W+H; layer = <CALayer: 0x6b293f0>>
   | <CustomView: 0x6b2a230; frame = (0 0; 0 0); layer = <CALayer: 0x6b2a270>>
   | <CustomView: 0x6b2a2a0; frame = (0 0; 0 0); layer = <CALayer: 0x6b2a2e0>>

`C`…sometimes…

Posted by – November 4, 2011

Someday when I have the time to draw XKCD-like cartoons I’ll post a bunch on programming languages. For now, however, please enjoy the tips on improving readability in `C`. One can add a line like

#include <iso646.h>

and enjoy branching syntax like keywords like and, or, not in place of the cryptic &&, ||, !

if ((x<55 and y>=bounds) or dontDoThat()) { ... }

Yet in the land of the greener pastures overloaded functions finally made it into C in clang. This makes a difference for the eternal iOS octopi… just consider a substitute for autoboxing:

NSNumber* object(float x) __attribute__((overloadable));
NSNumber* object(double x) __attribute__((overloadable));

NSNumber* __attribute__((overloadable)) object(float x)
{
      return [NSNumber numberWithFloat:x];
}

NSNumber* __attribute__((overloadable)) object(double x)
{
      return [NSNumber numberWithDouble:x];
}

`NSPathControl` useful for more than file paths

Posted by – June 17, 2011

Try configuring NSPathControl object like this

NSPathControl* pathControl = ...
[pathControl setStringValue:@"AAA/BBB/CCC"];

We get picture like the following that can be useful in a number of scenarios.


Typically we need to visualize paths to various data structures and portions of object graphs…

C++ namespaces in gdb are hell no more!

Posted by – April 12, 2011

Say we want to inspect a value of a C++ constant foo that is declared inside a namespace bar in gdb. First, in order to be visible to gdb it has to be a real “object”. Thus, it must be declared as extern in the header with the matching declaration in an implementation module.

// In a header
namespace bar
{
extern int const foo;
}
// In a module
int const bar::foo = 458;

Second, the natural attempt at printing the thing via

(gdb)p foo::bar

fails. Thanks to the bloggers of the world cloud

(gdb)p 'foo::bar'

works.

The shenanigans of switching to XCode4

Posted by – April 11, 2011

For a Cocoa-coding monkey like myself switching to the 4 from the 3 can be a touch disorienting. Boys (and girls) at Apple decided to catch up with the rest of the industry in the IDE space and finally gave the rest of us what we deserve. Well, as the many proverbs of the many peoples’ go: “Careful of what you ask for; You may get it”. And we did! But as the withdrawal symptoms of the 3-narcotic are setting in and while the 4-antidote does take its time to percolate through the cardiovascular systems of the discombobulated we struggle to adjust.

Hmm…. I should’ve been a writer but now I am programmer :P .

Its not just the remapped keystrokes, a new color scheme format, and the missing features we so did cherish, but rather a new workflow paradigm and an onslaught of the new features that don’t necessarily fully replace the missing ones. So, how do I help my fellow men? A few hints if nothing else…

[⌘0] toggles the left pane in and out
[⌘⌥.] lets you cycle through the areas of the IDE such as the editors, the inspector, etc.
[TAB] lets you move around inside an area once you are on it
[^⌘↑] takes the current editor to the counterpart of the currently edited file, while [^⌥⌘↑] brings up the counterpart in the neighboring editor.
[^6] brings up a list of symbols in an editor (what [^2] in XCode3 used to do).

TODO: Update this as I sink deeper into the abyss….

Compute the Voodoo way via the GCD

Posted by – March 30, 2011

Multiple cores of my Quad-Core Xeon are the violinists that play music to my ears. With the 4 cores one should expect a 4-time speedup at best, right? And that, only for the embarrassingly problems at that. Wrong!!!

Lets start by including a few headers in our guinea pig.

#import <Cocoa/Cocoa.h>
#import <iostream>
#import <libkern/OSAtomic.h>

Ok..ok.. Objective-C++… Guilty. Let us set up a function that does some dummy work but toils to the dawn with it.

using namespace std;

void myLongComputation(double& d)
{
    for (int j=0;j<100000;j++) { d++; }
}

Now we set up a parallel dispatch queue. Notice the  __block qualifier in front of our accumulator variable. This enables our forthcoming blocks to accumulate into the stack variable rather than into a local copy. It will all become clear in a moment.

dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
size_t iterations = 1000*100;
__block int32_t total=0;

Now we enqueue a bunch of blocks (in parallel) that invoke our long computation each incrementing the value of our shared total. The following pass does it in a non-atomic way. We also time our operations in a naive way, however it should be sufficient for our purposes.

CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
dispatch_apply(iterations, q, ^(size_t sz) { total++; double d; myLongComputation(d); } );
CFAbsoluteTime finishTime = CFAbsoluteTimeGetCurrent();
cerr << "total (parallel+naive) = " << total << endl;
cerr << "program took " << finishTime-startTime << " seconds" << endl;

The second pass is a copy of the first but utilizes atomic increments.

total = 0;
startTime = CFAbsoluteTimeGetCurrent();
dispatch_apply(iterations, q, ^(size_t sz) { OSAtomicAdd32(1, &total); double d; myLongComputation(d);  } );
finishTime = CFAbsoluteTimeGetCurrent();
   
cerr << "total (parallel+atomics) = " << total << endl;
cerr << "program took " << finishTime-startTime << " seconds" << endl;

Finally, we simply loop over the computations in the main thread as follows.

total = 0;
startTime = CFAbsoluteTimeGetCurrent();
void (^myBlock)(size_t) = ^(size_t sz) { total++; double d; myLongComputation(d); };
for (int j=0;j<iterations;j++) { myBlock(j); }
finishTime = CFAbsoluteTimeGetCurrent();    
cerr << "total (serial) = " << total << endl;
cerr << "program took " << finishTime-startTime << " seconds" << endl;

Is seems the mercurial hyper threading actually works :) since the tin-headed (aluminum-headed?… no.) under my desk spits out:

total (parallel+naive) = 99991
program took 5.27613 seconds
total (parallel+atomics) = 100000
program took 5.26177 seconds
total (serial) = 100000
program took 32.9869 seconds

with a 6 time speedup… hmm…

In the land of the blind… typechecking man is king

Posted by – March 25, 2011

Pliability that is the main trump of a dynamic language can be its own worst enemy. Messages speeding along the scarcely patrolled highways of dynamic lookup can crash afflicting an unsuspecting psyche of a proverbial hacker. The simple safeguards like the following

@implementation SomeObject
- (id)myUnsupportedSelector
{
    [self doesNotRecognizeSelector:_cmd];
    return nil;
}
@end

can make a all difference throughout SDLCs of our dynamic binary offsprings…

XCode4 and the color themes

Posted by – March 15, 2011

Transitioning to 4 from 3 can be enough of a shock without finding out that your carefully crafted color theme (that took a million years to perfect) no longer “works”. Our newborn XCode has an entirely new model and instead of the familiar  ~/Library/Application Supporrt location for settings it stores them (in a different format) at  ~/Library/Developer/Xcode/UserData/FontAndColorThemes . Mac Indie has a nice article detailing how to convert your “old” XCode3 themes to a new format via a python script. When the elegance muse got sidetracked from a visit with Apple engineers on this one, it apparently wondered in for a chat with the author of that script.

Cappuccino Machine

Posted by – May 11, 2010

I would call this post: “In a realm of all things cool Cappucino starts to rule”. I’ve noticed the framework a while ago but never quite had the time to sit down and start writing the “Hello World”. In my (graphics developer) vernacular that would be an animated ‘dot’ running around in circles. Here it goes… a bit slow, but does the job. I imagine it easy enough to improve the speed the animation by layer-backing the ‘dot’. Enjoy..