Translate

Map function in Java

August 31st, 2010

I read on some email signature something along the lines of:
“If I had a dollar for every for(int i=0; i < size; i++) { ... } I've written I'd be rich"

After coding on Android and learning about some of the tips for performance, like
"With an ArrayList, a hand-written counted loop is about 3x faster"

If you do use ArrayLists a lot you then have that tip in the back of your head and you end up with a lot of code like the following:

List<T> myList = … ;
int size = myList.size();
for (int i=0; i < size; i++) {
  T elem = myList.get(i);
  //do something with elem
}

Eventually there was a moment when I said, I need a freaking map function, I'm sick of this little pattern, surprisingly I couldn't find a map() function anywhere on the java collection framework. So I went and did this.

//what I think would be the equivalent of a lambda or closure in Python...
public interface MapFunction<T> {
   public void map(T t);
}

And then on one of my utils classes I added a static method map that looks like this:

public static <T> void map(List<T> list, MapFunction<T> mapFunction) {
int size=list.size();
   for (int i=0; i < size; i++) {
      mapFunction.map(list.get(i));
   }
}

So now, everytime I need to iterate over a whole list and do something to each element it's a lot cleaner

//let's serialize all the messages...
List<Message> messages = ...;
Utils.map(messages, new MapFunction<Message> {
   public void map(Message message) {
      Engine.INSTANCE.SERIALIZER.save(message);
   }
});

done deal.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Building The Taj Mahal with LEGOs

August 3rd, 2010

It had been ages since I didn’t play LEGO, after building a smaller project to see if we could get along as a team we decided to tackle a larger project.

Some life lessons you learn while playing with LEGOs:

1. Organization is key.
The more you can organize your blocks before doing any actual building, the better. The thinking is that you’ll have to find and use each and every piece on that box, you might as well do the indexing first and then spend no time looking for the pieces you need for your building.
Eventually we ended up separating every single kind of block and we put them in zip-lock bags or tupperware containers.

2. Attention to detail.

3. Focus.

4. Divide and conquer
But do so wisely, if you have to build the same module several times, that’s a perfect tasks to divide and do in parallel. If there’s a unique and very complex module to build, it’s better that you do it by yourself while your team builds other unique pieces. If they want to participate you could take turns, and spot each other to avoid mistakes. (sounds a lot like pair coding)

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Reference: Passing functions as parameters in C

July 18th, 2010
#include <stdio.h>
//Example to show how to pass
//functions as parameters in C.                                               

//simple function that returns the sum of two ints
int sum(int i, int j) {
  return i+j;
}

//simple function that prints a char*
void printSomething(char* something) {
  printf("%s\n",something);
}

//function that takes
// 2 ints
// 1 char*
// one function that returns an int and takes two ints
// one function that takes a char* returns nothing
void functionThatTakesOtherFunctions(int a,
                int b,
                char* name,
                int (functionA) (int,int),
                void (functionB) (char*)) {
  printf("Function A: %d\n",functionA(a,b));
  functionB(name);
}

int main(void) {
  //we pass the first two functions as parameters
  functionThatTakesOtherFunctions(3,4,
                                  "John Doe",
                                  sum,
                                  printSomething);
  return 0;
}

This worked fine on my gcc compiler (MacOSX 10.5), no need to even use * or & operators when defining the parameters or when passing the functions as parameters.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

On “Inception” and Programming

July 18th, 2010

After watching Inception I think I have an easy way to explain what complex object oriented programming can be like.

Next time someone asks “What is Programming like?” I’ll say something along these lines:

“Did you watch that movie “Inception”? Programming is similar, you build your own worlds, with your own rules (and worlds that can only exist in the abstract, sometimes very hard to explain in words), but now imagine that instead of dreaming in 4 levels (dreams inside dreams) you could be dreaming in about 12 levels down, but not only that, you could be having several dreams at once (each with several levels down), and most of the times some of these dreams have to share things with one another in order to make sense or they break.

So Programming is like Inception, but Deeper, In Parallel and Synchronized.”

Photo by Smif

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Reviewing the Nokia N900, User and Developer opinions.

July 7th, 2010


Playing with it from the User Perspective
- Virtual keyboard is deactivated by default, not good if you have one hand busy, however I can see why it’s this way (the virtual keyboard sucks). When it comes out it covers the entire screen, and the experience with it is not there yet in comparison to the virtual keyboards of the iPhone or Android.

- Physical Keyboard feels great.

- Not sold on the stylus, I think I might loose it. However it comes in handy when clicking on tiny links.

- Web browsing experience is clumsy if you try to use your fingers and you’re used to Android or iPhone where dragging occurs on a light touch. With the N900 you have to Tap harder to begin to drag, probably like that intentionally. I must admit I find myself making mistakes on the Droid maybe because it’s too sensitive.

- Using forms on the browser is inconvenient. There’s no way to move from form field to form field except using the stylus. Keyboard arrows won’t work to switch focus between form fields, and the lack of virtual keyboard (when you’re using the real one) implies that you won’t have a “Next” button to move through the form (or at least I couldn’t figure out a field hopping mechanism).

- Zooming in and out on a web page will be extremely unintuitive. Thanks to patents on multitouch gestures you’ll have to either double tap, or do a ridiculous tap+hold and twirl clockwise zoom in (in a controlled way), or tap+hold and twirl counter-clockwise to zoom out… wtf.
Nokia has to put up a fight on this, natural multi-touch gestures are a must on today’s smartphones.

- I still don’t know how to select, copy and paste text from a website. Very hard to quote text and twitter it.

- Default timeout for screen auto lock is way too short.

- It has a stand on the back, which as a developer I appreciate. It’s a pain to debug apps on the nexus one, you always kind of need to pick it up or create some sort of desktop apparatus to have the phone facing you while you test hands free.

Multimedia
- Video and audio playback were perfect. Droid and Nexus One have nothing to envy, so it’s not a selling point. Watch the 3 phones playing the same video.

- Navigation is convenient with top left corner control. It took me a few seconds to figure it out being used to Android’s back and Home Button and of the one physical button on the iPhone.

- The Media player comes with pre-installed internet radio streaming app. On Android I had to download one from the Market.

- Didn’t have to install Flickr photo uploading app, already comes with OS.

- OVI Store experience frustrated me. Should be an app, not a web page. I got asked for an HTTPS certificate while trying to install a Twitter plugin, it was basically the same exception you get on a web browser. The regular user would get lost adding the certificate and the security exception. Then after that I got asked to signup for the OVI store, and I gave up. Installing apps should be a breeze. Setting up your OVI Store account should be done as part of the phone setup process on an App.

The more I use the phone the more I think, Nokia’s hardware is hot, but they’re wasting their time with competing on software. Nokia running Android would kill everybody else, let’s hope it will happen sooner than I think, they just need to test with one cool device and feel the market.

Unboxing Pictures

Playing with it as a Software Developer
- No SDK for Mac environment, or at least not a very accessible or official version of it. The only solution seemed to be installing the SDK on a Virtualized Linux (e.g. VirtualBox).

- Maemo SDK not compatible with Ubuntu 10.4 64bit, it seems it’s only 32bit.

The project page confirms this:
“The development environment for Maemo running on the desktop is called Maemo SDK. You can only install Maemo SDK on a Linux operating system. Maemo SDK currently supports the following Linux distributions: Debian, Ubuntu”

Tried installing Scratchbox on my Mac with no luck.

- It was a little surprising to see the Nokia documentation for developers talking about GTK+ in all their examples and not Qt, given the acquisition of Trolltech by Nokia in 2008. However you go to the Qt page and it says that it’s the defacto SDK for Maemo devices.

In the end I decided to install it on a 32bit Ubuntu VirtualBox image on my Mac and deal with the performance hit, I couldn’t just return the phone without compiling something, otherwise I’d be speaking out of my ass right?

Hello World in GTK

Hello World in Qt4, exactly the same code I would’ve used for a desktop Qt4 app.

compilation is the same thing as if developing a regular Qt4 app on linux

/scratchbox/login
fakeroot apt-get install libqt4-dev

create your helloworld.cpp… then make your project

qmake -project
qmake
make
run-standalone.sh ./qt_helloworld

If you’re used to Linux development with GTK or Qt4 you will feel right at home, and you probably know right off what’s the most comfortable set of tools to code. If you’re not, it seems like Nokia has left the whole thing way too much on the hands of the Maemo community.

Documentation seems a little all over the place, unfinished Wikis, not very welcoming for non-linux developers, I’m talking about companies that come from actually making money on iPhone OS or Android, looking to port their apps to Maemo. The first impression won’t be the best and you’ll think that things aren’t as well prepared as for iPhone with X-Code or Android with the Eclipse extension which let’s you deploy a signed installer in just a few clicks.

It seems that Nokia is relying way too much on the Linux community for all of this, I’m not sure if this is going to get very far with mobile shops, specially after seeing how many hurdles a user has to go through sometimes to get an app from the OVI market (in comparison to iPhone or Android’s markets simple purchasing processes).

SDK experience Screenshots

Conclusions

First of all thanks to Nokia for sending the N900. My final conclusions which are mostly thinking from the consumer perspective might not be one you’ll like much (you=Nokia), but you probably need to hear this (I’m guessing this is why you sent the device in the first place).

In my case after being a faithful Nokia consumer I felt dissapointed with the software side when I chose the N95 over the iPhone, newer Nokia phones came, but the same shitty platform was running on them, I thought Maemo would change that but now I know it’s nowhere near what smartphone users today expect. Then Android came along to truly compete with the iPhone.

Now as an Android user when the time comes for me to upgrade my phone I’ll be looking for another Android device.

Let’s imagine what’s going to happen when my Droid is too old, by that time there will be even more Android phones to choose from also a richer Application ecosystem, if I were the kind of person that installs paid apps I’d probably like to keep my “investment” for my next phone, however even if there’s a myriad of Android phones out there, the only brands I’ll consider are Motorola, HTC, LG and SonyEricsson, I’m not too fond of any of them so I’ll have to research what each of the phones they’ll be offering will suit my needs.

An Android based Nokia smartphone would certainly make my decision a lot easier. As of now you’re entirely out of the picture, there’s no way I’m switching to Maemo, and Moblin is way too new to tell if it’s going anywhere.

Nokia, Stop playing outside the garden, don’t get cocky with your current numbers and admit you’re slowly decaying, if you don’t do something about it once Android is on every cheap phone you’re gonna die. Android could use some Nokia.

Follow the geek.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

startKeychain – bash utility to start ssh-agent

June 3rd, 2010

For my (and your) future reference, here’s a function to put on your .bashrc or .bash_profile, you can invoke it later at any time to start/re-start your ssh-agent.

function startKeychain {
    killall ssh-agent
    rm ~/.keychain/*
    keychain id_rsa
    HOSTNAME=`hostname`
    source ~/.keychain/${HOSTNAME}-sh
}

Then at any time, the “command” startKeychain will be available on your command line.

Output should look something like this:


gubatron@gubatron-desktop:~$ startKeychain 

KeyChain 2.6.8; http://www.gentoo.org/proj/en/keychain/
Copyright 2002-2004 Gentoo Foundation; Distributed under the GPL

 * Initializing /home/gubatron/.keychain/gubatron-desktop-sh file...
 * Initializing /home/gubatron/.keychain/gubatron-desktop-csh file...
 * Initializing /home/gubatron/.keychain/gubatron-desktop-fish file...
 * Starting ssh-agent
 * Initializing /home/gubatron/.keychain/gubatron-desktop-sh-gpg file...
 * Initializing /home/gubatron/.keychain/gubatron-desktop-csh-gpg file...
 * Initializing /home/gubatron/.keychain/gubatron-desktop-fish-gpg file...
 * Starting gpg-agent
 * Adding 1 ssh key(s)...
Identity added: /home/gubatron/.ssh/id_rsa (/home/gubatron/.ssh/id_rsa)

Comments are welcome to improve it, I’m not an ssh-agent expert, but this seems to do the work.

[SOLVED] Issue with KDE 4.4.2 and Dolphin always asking my ssh passwords whenever I browsed folder I checked out from a remote subversion repository.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)