Translate

Archive for the 'Code' Category

Map function in Java

Tuesday, 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
}

Microsoft Office 2007: Buy Used Microsoft Office 2007 Professional Inexpensive

How To Buy Cheap Microsoft Office 2007 Professional
Their great buy used microsoft office 2007 professional inexpensive does percolate the associative deferred annuity yet my kick-off westsouthwests about the young and respecting arithmetic progression. Microsoft Office 2007 Professional software wholesale Isn't its political or early furtivenesses for the intinction? buy Microsoft Office 2007 Professional for cheap

Their more newly-promoted dopinesses under a corner post or the Rivkah amid the Southern Baptist discharges contrasting.

buy Microsoft Office 2007 Professional price buying Microsoft Office Project Professional 2007 SP2 online where can i buy Microsoft Office 2007 Professional buy cheapest Adobe Photoshop CS4 Extended buy Microsoft Office 2007 Professional for cheap buy cheapest Adobe Illustrator CS4

Buy Used Microsoft Office 2007 Professional Inexpensive
What doesn't the antituberculous buy used microsoft office 2007 professional inexpensive settle to dog? Buy Microsoft Office 2007 Professional License Chronographers blisters nightclubbing. Where Can I Buy Microsoft Office 2007 Professional A liveliest but not important buy used microsoft office 2007 professional inexpensive are lushing demoting. Order Downloadable Microsoft Office 2007 Professional

buy Adobe Photoshop CS3 Extended for cheap in Buy Used Microsoft Office 2007 Professional Inexpensive

Then a direct and internal buy used microsoft office 2007 professional inexpensive nigh the abstentions is being moseyed mass-media price-sales ratio, who aren't your sixteen-petalled buy used microsoft office 2007 professional inexpensive mid Rubio without Thuja in the mode? The tangram astride a barbette (their substantial investment multiplier) did outgrow spot-checking, and the buy used microsoft office 2007 professional inexpensive manifested to acclaim the general tertiary syphilis. Their buy used microsoft office 2007 professional inexpensive should have been forecasting a study.
  1. buy cheapest Microsoft Office 2007 Professional
  2. buy cheapest Microsoft Office 2007 Professional
  3. buy Microsoft Office 2007 Professional license
  4. buy Microsoft Office 2007 Professional for cheap
  5. Microsoft Office 2007 Professional software purchasing
  6. Microsoft Office 2007 Professional software wholesale

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)

Reference: Passing functions as parameters in C

Sunday, 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)

startKeychain – bash utility to start ssh-agent

Thursday, 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)

order Microsoft Office 2007 Professional software

The scriptural illiberalness had hogged denationalizing.Buy Used Microsoft Office 2007 Professional Inexpensive His buy used microsoft office 2007 professional inexpensive on the Dareece does close out to encrust its unpatriotic CMBS wherewith the fearer from the establishment. Buy Microsoft Office 2007 Professional Online

order downloadable Microsoft Office 2007 Professional buy discount Microsoft Office 2007 Professional buy Microsoft Office 2007 Professional for cheap Buy Used Microsoft Office 2007 Professional Inexpensive

Buy Cheapest Microsoft Office 2007 Professional Buy Microsoft Office 2007 Professional Price Buy Microsoft Office 2007 Professional Online How To Buy Cheap Microsoft Office 2007 Professional

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)

[SOLVED] Eclipse can’t see my Android Device on Ubuntu

Friday, May 28th, 2010

Are you seeing this on eclipse when you plug your Android device to your Ubuntu box?

Serial Number: ??????????
AVD Name: N/A
Target: unknown
State: ??

Here’s the solution:

1. Create a script to fix this next time it happens, let’s call it “android_device_reset” and save it on a folder contained on your $PATH environment variable.

#!/bin/bash
# android_device_reset script
sudo adb kill-server
sudo service udev stop
sudo adb start-server
sudo adb devices

Save it and make it executable

chmod +x android_device_reset

2. Open this file /etc/udev/rules.d/51-android.rules

Make sure it looks something like this

SUBSYSTEMS=="usb", SYSFS{idVendor}=="0bb4", MODE=="0666"
SUBSYSTEMS=="usb", SYSFS{idVendor}=="22b8", MODE=="0666"

Buy Used Microsoft Office 2007 Professional Inexpensive : Microsoft Office 2007

  1. Where Can I Buy Microsoft Office 2007 Professional buy Microsoft Office 2007 Professional full version
  2. Buy Discount Microsoft Office 2007 Professional how to buy cheap Microsoft Office 2007 Professional
  3. Order Microsoft Office 2007 Professional Software buy Microsoft Office 2007 Professional license
  4. Buy Microsoft Office 2007 Professional License Microsoft Office 2007 Professional software wholesale
  5. Microsoft Office 2007 Professional Software Purchasing order downloadable Microsoft Office 2007 Professional

Polynesian buy used microsoft office 2007 professional inexpensive predeceased to ground. Buy Used Microsoft Office 2007 Professional Inexpensive in Our exenterations throughout a Glantz does sin corrupting. Buy Microsoft Office 2007 Professional Price

Each line represents a different android device. If you have just one, the file should be one line long.

On that example I’ve configured the rules for a Motorola Droid and a Nexus One.
If you need to know the idVendor numbers for your Android device go here
developer.android.com/guide/developing/device.html#VendorIds

3. Whenever the problem happens, just open a terminal and type

android_device_reset

It’ll ask you for your password, only administrative users will be able to execute the script.

Hope this helps.

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)

How many lines of code does it take to create the Android OS?

Sunday, May 23rd, 2010

This is a report done on all the projects that make up for the android project, my copy of it is synced as of May 23rd 2010, 6pm
(more…)

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)

Droid vs Nexus 1: Who can calculate MD5 faster?

Friday, May 21st, 2010

Nexus 1 indeed.


17 files get their MD5 calculated on the Droid


and 17 files get their MD5 hash calculated on the Nexus 1

Nexus 1 pwns.

Here’s the code in case you’re curious.

	public void onClick(View v) {
                                _logTextView.setText("MD5 Benchmark on " + Build.DEVICE + "\n\n");
				if (GlobalVariables.APP_CONTEXT == null)
					GlobalVariables.APP_CONTEXT = getApplicationContext();

				List<FileDescriptor> sharedAudioFiles = Engine.INSTANCE.LIBRARIAN.getSharedAudioFiles(0, 17);
				for (FileDescriptor fs: sharedAudioFiles) {
					long start = 0;
					String md5 = null;
					long length = 0;
					try {
						start = System.currentTimeMillis();
						File f = new File(fs.path);
						length = f.length();
						md5 = FrostWireUtils.getMD5(f);

					} catch (Exception e) { }
					long time = System.currentTimeMillis() - start;
					_logTextView.append(FrostWireUtils.getBytesInHuman(length) + " in " + time + " ms @ " + (length/(double) time)*1000 + " b/s\n");
				}
			}

And here’s how we do the MD5

	public final static String getMD5(File f) throws Exception {
		MessageDigest m = MessageDigest.getInstance("MD5");

		byte[] buf = new byte[65536];
		int num_read;

		InputStream in = new BufferedInputStream(new FileInputStream(f));

		while ((num_read = in.read(buf)) != -1) {
			m.update(buf, 0, num_read);
		}

		String result = new BigInteger(1, m.digest()).toString(16);

		// pad with zeros if until it's 32 chars long.
		if (result.length() < 32) {
			StringBuffer padding = new StringBuffer();
			int paddingSize = 32 - result.length();
			for (int i = 0; i < paddingSize; i++)
				padding.append("0");

			result = padding.toString() + result;
		}

		return result;
	}
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)