Archive for the ‘Programming’ Category
Hamcrest: Improving Reducer Implementations
In the beginning of the year I posted about the ways you can use Hamcrest out of test code, together with hamcrest-collections. This combination allows us to write different kinds of matchers to select and reject items from lists, as well as applying map and reduce to them. After a while making use of them on my current project, I wanted to share what I liked a lot, and what I didn’t like a lot. My friend Liz Douglass has also written a post sharing our experience, and I will just complement it a bit…
What I Liked A Lot
There’s no much to write here, as we all know that this combination is quite powerful when you’re looking for writing code that reads more like english language, making it much easier to express the intent of your code. Not to mention that we get rid of for loops everywhere in the codebase.
What I Didn’t Liked A Lot
One aspect I didn’t like since the beginning when implementing Reducers is that they are coupled to one specific type. It reduces a list of one type into a result of the same type. And from the Wikipedia definition of Map and Reduce…
“Map” step: The master node takes the input, chops it up into smaller sub-problems, and distributes those to worker nodes.
“Reduce” step: The master node then takes the answers to all the sub-problems and combines them in a way to get the output - the answer to the problem it was originally trying to solve.
… we can see that it doesn’t mention that the result should be of the same type as the original one, after applying the reducer. And that’s exactly what I wanted to do instead. People said I was trying to combine both Map and Reduce into a single implementation. I kind of disagree with that, because the fact that I am reducing a list into a result of a different type, it doesn’t mean that I am transforming the original input (like multiplying each item in a list of integers by 2, before concatenating them). Confusing?
Let me try to explain it using an example. Given we have a list of integers…
Listlist = Lists.create(1, 2, 3);
and that I want to concatenate these numbers into a string. With the current hamcrest-collections implementation, that would be possible doing something like…
IterablelistOfStrings = FunctionMapper.map(list, new Function () { public String apply(Integer number) { return String.valueOf(number); } }); String result = Reduction.reduce(listOfStrings, new Reducer () { public String apply(String first, String previous) { return first.concat("+").concat(previous); } }); Assert.assertEquals("1+2+3", result);
It’s quite a lot of code just to concatenate a list of numbers! One day while pairing with Tom Czarniecki, we decided to reimplement the Reduction and Reducer classes, so that we could create more flexible and simple Reducer implementations, and of course writing almost half the lines of code.
public interface Reducer{ U apply(T first, U previous); }
public class Reduction {
public static U reduce(List list, U initialValue, Reducer reducer) {
U currentValue = initialValue;
for (T item : list) {
currentValue = reducer.apply(item, currentValue);
}
return currentValue;
}
}
To do the same number concatenation with this new implementation, is just a matter of defining a new Reducer, that concatenates them in a string.
Assert.assertEquals("1+2+3", Reduction.reduce(list, "", new Reducer() {
public String apply(Integer first, String previous) {
return previous.concat("+").concat(String.valueOf(first));
}
}));
Much simpler!
Advantages Of This New Implementation
- Do I have to mention again that it’s much cleaner?
- We use java.util.List instead of Iterable.
- No exception is thrown if the list to reduce is empty. It just uses the initial value provided on the reducer implementation.
- Flexibility to reduce a list into a result of any type.
Hope you enjoy it!
Hamcrest Out Of Test Code!
It’s been a while since I read some interesting posts showing creative uses of Hamcrest library out of test code. Since then I’ve been proscrastinating to implement my own version, trying strongly typed java delegates.
Thankfully this week I came across a nice API called hamcrest-collections. It uses Hamcrest to implement features such as select, reject, map, reduce and zip familiar from languages like Ruby and Python.
Selectors
Selectors can be used to select or reject items that matches a given Matcher, from any iterable object. It reminds me the Specification Pattern from Domain-Driven Design, which is also used for querying objects that satisfies defined specifications.
public static final Person john = new Person("John", 28);
public static final Person nicole = new Person("Nicole", 12);
public static final Person ryan = new Person("Ryan", 23);
public static final Person nathan = new Person("Nathan", 18);
public static final List list() {
return Arrays.asList(john, nicole, ryan, nathan);
}
The code below selects from the list of users defined above, the ones that are under twenty.
@Test
public void should_select_only_people_under_twenty_years_old() {
List users = Person.list();
Iterable underTwentyList = select(users, underAge(20));
assertThat(underTwentyList, hasItems(nicole, nathan));
assertThat(underTwentyList, not(hasItems(john, ryan)));
}
The code below rejects all the users that are under twenty.
@Test
public void should_reject_every_people_under_twenty_years_old() {
List users = Person.list();
Iterable aboveTwentyList = reject(users, underAge(20));
assertThat(aboveTwentyList, hasItems(john, ryan));
assertThat(aboveTwentyList, not(hasItems(nicole, nathan)));
}
Map and Reduce
Map is used to apply a function onto each item in any iterable object, whereas Reduce combines all these elements, applying a Reducer implementation. In our example, we map the timesTwo function, that doubles each element in the list, and then we reduce it by adding up all of them.
@Test
public void should_double_each_number_in_the_list_then_sum_all_of_them() {
List numbers = Arrays.asList(1, 2, 3);
MultiplyBy timesTwo = new MultiplyBy(2);
Iterable result = map(numbers, timesTwo);
assertThat(result, hasItems(2, 4, 6));
Integer sum = reduce(result, new Sum());
assertThat(sum, equalTo(12));
}
public class MultiplyBy implements Function{ private Integer factor; public MultiplyBy(Integer factor) { this.factor = factor; } public Integer apply(Integer number) { return (int)number * factor; } }
public class Sum implements Reducer{ public Integer apply(Integer first, Integer second) { return first + second; } }
Despite the bias created by some developers, that Hamcrest should not be used anywhere else but test code, specially after JUnit has defined it as its new matcher library, just ignore it and add these features to your runtime library, so that you can let your creativity drive you when developing. Get rid of “for” loops from your life! ![]()
Clojure: Integrating With Java
Currently I am learning Clojure. It is a functional programming language, but not a pure one, since you can both write code that share state (mutable) and also ones that doesn’t.
Why Clojure?
The main reason why I chose Clojure is its easy interoperability with Java, still one of the most used languages, bringing to it the power of Lisp. It’s fast, since the code is compiled, and it supplements some of Java’s weakness, such as the Collections framework and concurrent programming. It is pretty straightforward to write concurrent programs, everything is automatic, no manual lock management!
Integrating With Java
Importing classes
A single class:
(import java.util.List)
Multiple classes from the same package:
(import '(java.util List Set))
Creating instances
Using Java’s new keyword:
(new java.util.ArrayList) (new ArrayList) ; after importing
Assigning a new List to a Clojure variable:
(def list (new java.util.List)) -> #'user/list
Syntactic Sugar:
(ArrayList.)
Accessing fields
Static fields:
(. Math PI)
Syntactic Sugar:
Math/PI
Invoking methods
Static Methods
(.currentTimeMillis System)
Syntactic Sugar:
(System/currentTimeMillis)
Non-static Methods
(. list size) (. list get 0) ; returns the object stored at index 0
Syntactic Sugar:
(.size list)
Mixing Them All
Clojure provides a macro called memfn that makes possible execute Java methods as functions. So, for a list of String objects, if I want to make all of them upper-case, all I have to do is:
(map (memfn toUpperCase) ["a" "short" "message"])
The map function applies the function/method toUpperCase to each element in ["a" "short" "message"]
You can also use the bean function to wrap a Java bean in an immutable Clojure map.
(bean (new Person "Alexandre" "Martins"))
-> {:firstName "Alexandre", :lastName "Martins"}
Once converted, you can manipulate the new map using any of Clojure’s map functions, like:
(:firstName (bean (new Person "Alexandre" "Martins"))) -> Alexandre
The code above extracts the firstName key, originally from the Person object.
Ping Pong Pairing: Even More Fun!
The agile software development practice I like the most, and at the same time, the one I find the most difficult is pair programming. Each individual has his/her own way of working, and characteristics such as motivation, engagement, habits, open-mindedness, and coding/design style varies a lot from individuals. Sometimes, to get a balance between these differences is quite hard. I am still not an expert in pair programming coaching, but I’ve been learning a lot on my current assignment.
And from this experience, it seems that clients are definitely more involved and amused when it comes pairing following the ping pong pattern.
Ping Pong Pattern
It happens when the developer 1 from a pair implements a test for a given feature and see it failing, then passes the keyboard to developer 2 who makes the test pass, do some refactoring on the code and implements another test, passing the keyboard back to developer 1 to do the same thing and continue until the feature is done.
Why Do We Like
- Challenge - Each time a developer writes a test for you to make it pass, it sounds like a challenge, then you do it and write another one, challenging him back.
- Dynamics - The worse thing is a developer that just hogs the keyboard, making you feel a useless. Ping pong pairing makes you swap keyboard more frequently.
- Engagement - Developers are much more engaged because they are constantly coding, not only observing.
- Fun - It is so much fun when you have all the above items together!
Spring MVC, almost there!
One of the things I really like about Spring 2.5 is the new set of annotations for defining controllers.
Now you don’t have to extend any superclass to turn your class into a controller, just add the @Controller annotation on the top of the class definition and that’s it, you got one (still have to define it as a bean in the XML file). Another cool feature in this set, is the combination of the @RequestMapping and @RequestParam annotations.
@Controller
public class ProductController {
@Autowired
private MemberRepository repository;
@RequestMapping(value="/product", method=RequestMethod.GET)
public String product(@RequestParam(value="id") String id, ModelMap model) {
model.addAttribute("product", repository.getProduct(id));
return "product/view";
}
}
@RequestMapping gives you flexibility to map methods in the controller to whatever URIs and HTTP methods (default to RequestMethod.GET) you like. They are not dependent on another or on controller’s name. This really turns things easier when mapping your methods to RESTful URIs. The only downside in this implementation is that it doesn’t allow you to pull out parameters from an URI. If you want to grab some data from the URL, you are going to have to do it the old fashioned way, like:
mystore/product?id=123456
But on the other hand the @RequestParam annotation frees you from the dirty job of extracting parameters from a request. Thus, you don’t even need to know about the HttpServletRequest and HttpServletResponse objects. To retrieve a parameter from a request, just add it as a method parameter and define it as a @RequestParam. The value attribute on the annotation should match the URL parameter name.
@RequestMapping("/products")
public String products(@RequestParam(value="letter", required=false) String letter, ModelMap model) {
checkLetter(letter);
model.addAttribute("products", repository.getProducts(letter));
return "product/list";
}
You also have the option of defining a request parameter as required or not (default set to required), so as in the example above, the method would be able to handle requests coming from both mystore/products, to list all existent products, and mystore/products?letter=A, to list all existent products starting with the letter ‘A’. Really neat!
The good news is that Spring 3.0 is coming out next November, and it will include the URI templating functionality. According to Springify blog, you will be able to define a URI like mystore/product/123456, and that will map to the controller method as follows:
@RequestMapping("/product/${id}")
public String product(@RequestParam("id") String id, ModelMap model) {
model.addAttribute("product", repository.getProduct(id));
return "product/view";
}
So let’s wait for it!
Common Lisp, Erlang and Emacs (Leopard)
This useful post from my friend Renato Lucindo is for those interested in start playing with Erlang. It helps you to set up your development environment, for MacOS, showing how to install it using MacPorts and how to set up Emacs.
* It’s in portuguese!
Subversion command reference
This post is more to keep in a single place reference to useful subversion commands, for future development.
Anyone who has a command can comment this post, so I can add it here.
List all your local files that hasn’t been added to the repository yet.
vn st | grep ?
Add all your local files that hasn’t been added to the repository yet.
svn st | grep ? | cut -b7- | xargs svn add
or
svn add . –force
Send HTML formatted email messages for Subversion activity (see SVN-Notify website)
svnnotify -d -H HTML::ColorDiff --smtp myhost.com --repos-path "$1" --revision "$2" -t dev1@myhost.com -t dev2@myhost.com