Saturday, January 16, 2010

Weekend Programming Excursion - 1


So I have joined a new company on 30th December 2009. I am now working for Thomson-Reuters. As always the blog that I write here will not represent in any manner what so ever my new employer’s views, these are just my technological rants that I always write. I do not even know whether these are read or not, but just in case you are among those people who surf around and read this blog (either occasionally or regularly) be assured that I will eventually write something in here.


So, gosh what new year it was – leaving one company and joining another, I didn’t even got a chance to work on anything (other than all the pending items that I had on my plate with my previous employer, writing a good-bye mail, meeting people who wanted to wish me luck for my new venture and of course the team send off dinner) significant to write. It was all work and no play with my laptop.


As some of you may have noticed in this blogs that Lisp is not the programming language that I am working now. I am no longer in those fortunate few who get paid for learning Lisp, working with Lisp and writing Lisp codes. But that said nobody stopped me from learning a new programming language – but as you can guess from my worthless rants that I have not even scratched the surface of it.


On a boring weekend you usually start the day with your home stuff like laundry. However, if you are a programmer who cannot live without coding something or the other in a day, you also code. That kind of coding I call a “weekend excursion into programming”. Most of the times one tends to lose all the code that you have built over the time. This is especially true for me. So before I forget what I did I would like to put it in here.


So what was that I did?


A class and an interface in Java! Yes you heard it correct just a class and an interface - an interface with one method signature and a class with one method (as of now). That’s it!


You must be wondering what the heck! 1 interface, 1 class and 1 method for each of them!


You may tell me, “Dude! You are not working hard.”


Point taken! No more blah, blah….


So here it is what I did.




The One Interface



I bad at naming but I knew that the word “lambda” has to be there for this interface name. So in the kingdom of noun (i.e. Java) I have to use adjective for interfaces so the name of the interface is LambdaService.


So what is there in this interface? Here is the code for you:




import java.util.List;
public interface LambdaService {

T lambda(List params );
}


Pretty simple, huh! It takes a list of arguments of type T and returns an object of type T. If you are from some functional programming background, you already know this stuff. But for people who do not know how I am going to use it – hold on, you are in for a ride.




The One Class




Let me give you the class definition code also.




import java.util.ArrayList;
import java.util.List;

/**
* @author samikc
*
*/
public class Functional {

public static List map(LambdaService lambda,
List... list) {
List retList = new ArrayList();
List params = new ArrayList();
int iterCount = getShortestListSize(list);
for (int i = 0;i < iterCount;i++) {
int paramCounter = 0;
for (List l : list) {
params.add(paramCounter, l.get(i));
paramCounter++;
}
retList.add((T) lambda.lambda(params));
}
return retList;
}

private static int
getShortestListSize(List... list) {
List retList = list[0];
for (List l : list) {
if (l.size() < retList.size()) {
retList = l;
}
}
return retList.size();
}

}



So what this method map is doing here. Let me try to explain.


In map you provide a lambda service (which is a method at the end of the day) and a list. The lambda service is nothing but a strategy pattern, I am allowing others to provide an option to provide an algorithm which will be applied to each member of the list provided and result will be collated in a different list.
Notice that I have made “different list” bold. Why? As I am applying the algorithm provided I am not changing the anything inside the actual list passed on. So it does not have a side-effect.


The main is given below without any comments; I feel it’s very simple and intuitive.




The *main* Story





import java.util.ArrayList;
import java.util.List;

public class Main {
public static void main(String[] args) {
ArrayList il1 = new ArrayList();
il1.add(10);
il1.add(20);
il1.add(30);
ArrayList il2 = new ArrayList();
il2.add(10);
il2.add(20);
il2.add(30);
il2.add(40);
List li = Functional.map(new LambdaService() {
@Override
public Integer lambda(List params) {
Integer i1 = params.get(0);
Integer i2 = params.get(1);
Integer ret = i1 + i2;
return ret;
}
}, il1, il2);

for (Object o : li) {
System.out.println(o);
}
}
}

Sample output:
20
40
60

No comments: