-
Reversing Groovy switch statement
Recently I’ve been working on a Groovy code that had many methods with long multibranch conditionals like this:
Although this code is working, it is hard to see which branch is called under which condition. It would be much better if we could replace this code with something like Lisp
cond
macro. The best candidate for such a task in Groovy would be aswitch
statement. If we could only refactor the code above to something like following, it would significantly improve readability:Unfortunately, this code doesn’t work out of the box in Groovy, but it works if we do some metaprogramming.
The way
switch
statement works in Groovy is a bit different than in Java. Instead of equals() it uses isCase() method to match case-value and switch-value. The default implementation of isCase() method falls back to equals() method, but some classes, including Collection, override this behaviour. That’s why in Groovy you can do things like this:For our purposes we need some sort of reverse
switch
, where collection is used as a switch-value, and String and Integer are used as a case-value. To do this we need to override default implementation of isCase() method on String and Integer classes. It’s not possible in Java, but is very easy in Groovy. You can change method implementation globally by replacing it in corresponding meta class, or locally with the help of categories. Let’s create a category that swaps object and subject of isCase() method:Now we can use this category to achieve the goal we stated at the beginning of this post:
If you are comfortable with global method replacement, you can amend String and Integer meta classes. In this case you don’t need to wrap
switch
statement withuse
keyword. -
Lazy lists in Groovy
I like lazy evaluation, and it’s one of the reasons I like Haskell and Clojure. Although from engineering perspective lazy evaluation is probably not the most needed feature, it’s definitely very useful for solving some mathematical problems.
Most languages don’t have lazy evaluation out of the box, but you can implement it using some other language features. This is an interesting task, and I use it as a code kata which I practice every time I learn a new strict language.
So, how to implement lazy lists in a strict language? Very simple, if the language is functional. Namely, you build lazy list recursively by wrapping strict list within a function. Here is, for example, the strict empty list in Groovy:
If we wrap it with a closure, it becomes lazy empty list:
If we need a list with one element, we prepend (or speaking Lisp terminology cons) an element to lazy empty list, and make the result lazy again:
To add more elements we continue the same process until all elements are lazily consed. Here is, for example, a lazy list with three elements a, b and c:
Now, when you have an idea how to build lazy lists, let’s build them Groovy way. We start by creating a class:
The variable
list
encapsulates the closure wrapper of the list. We need to expose some methods that allow constructing lists using procedure described above:Now we can construct lists by consing elements to empty list:
To access elements of the list we implement two standard functions,
car
andcdr
, that return head and tail of the list respectively.Here is how you use these functions to get first and second elements of the list constructed above
In Lisp there are built-in functions for various
car
andcdr
compositions. For example, the previous assertion would be equivalent to functioncadr
. Instead of implementing all possible permutations, let’s use Groovy metaprogramming to achieve the same goal.It might look complicated, but in reality it’s pretty simple if you are familiar with Groovy regex and functional programming. It’s easier to explain by example. If we pass “caddr” as a value of
name
parameter, the method will create a chain on method calls.cdr().cdr().car()
which will be applied to delegate of the operation which is our LazyList object.With this method in place we can call car/cdr functions with arbitrary depth.
If you create nested lazy lists, you can access any element of any nested list with this dynamic method.
With so many cons methods it’s hard to see the structure of the list. Let’s implement
lazy
method on ArrayList class that converts strict list to lazy. Again, we will use metaprogramming and functional techniques.Now we can rewrite the previous example as follows
What have we accomplished so far? We learned how to build lazy lists from scratch and from strict lists. We know how to add elements to lazy lists, and how to access them. The next step is to implement
fold
function.fold
is the fundamental operation in functional languages, so our lazy lists must provide it.The only difference between this
fold
function and the standard one is the additional parameter n. We will need it later when we implement infinite lists.foldAll
function to lazy lists is the same as standardfold
to strict lists.First example calculates the sum of all elements of the list, second calculates the product of first three elements.
If you have
fold
functions you can easily implementtake
functionstake
is an inverse operation tolazy
Our next goal is
map
function on lazy lists. Ideally I want the implementation look like thisFor some reason it doesn’t work lazy way in Groovy — it’s still strictly evaluated. Therefore I have to implement it directly with closure syntax
Unlike
fold
, lazymap
is identical to strictmap
The following example shows one of the benefits of laziness
map
didn’t evaluate the entire list, hence there was no exception. If you evaluate the expression for all the elements, the exception will be thrownFor strict lists this is a default behaviour of
map
function.The last function I want to implement is
filter
In the following example we find first two elements greater than 2
With the help of
car
/cdr
,fold
,map
andfilter
you can implement any other function on lazy lists yourself. Here is, for example, the implementation ofzipWith
functionNow, after we implemented all lazy functions we need, let’s define infinite lists
Infinite lists, from my point of view, is the most useful application of lazy lists
At this point you have all basic functionality implemented, and you should be able to extend this model to whatever you need in regards to lazy (infinite) lists. Happy lazy programming!
Resources and links
- Source code for this blog
- Lazy list implementation in Erlang
- Lazy list implementation in Lisp
-
Counting modifications in Git repository
Michael Feathers wrote a blog about Open-Closed Principle, where he described simple technique that measures the closure of the code. I created a Groovy script which implements this algorithm for Git repositories. If you run it from the root of your Git project, it produces a CSV file with the statistics of how many times the files have been modified.
As an example, here is the top 10 files from rabbitmq-server repository
845 src/rabbit_amqqueue_process.erl 711 src/rabbit_channel.erl 650 src/rabbit_tests.erl 588 src/rabbit_variable_queue.erl 457 src/rabbit_amqqueue.erl 448 src/rabbit_mnesia.erl 405 src/rabbit.erl 395 src/rabbit_reader.erl 360 src/rabbit_msg_store.erl 356 src/rabbit_exchange.erl
-
Maven and Git
More and more Maven projects are switching from Subversion to Git, and the majority of those projects make the same mistake: They configure scm section of POM to point to remote repository, the same way they did it in Subversion
By doing this they lose the main benefit of Git. They become dependent on the remote machine. And when their release depends on the remote machine, that’s what happens: They need to release the project but the remote machine is down
The right way of configuring Git in Maven is following
This configuration is universal, and it separates two orthogonal concerns: releases and remote copies.
Resources
- Screencast that shows how to work with Git in Maven projects.
-
Joe Armstrong on optimization
A quote from Erlang and OTP in Action book
Make it work, then make it beautiful, then if you really, really have to, make it fast. 90 percent of the time, if you make it beautiful, it will already be fast. So really, just make it beautiful!
A quote from InfoQ interview
I think that people first of all write a problem, they solve the problem and then they sort of optimize this code, work on it and the code becomes very efficient but unreadable. What I think they should doing is specifying it with a domain specific language or some higher thing and then writing a compiler and then changing the compiler because it’s not efficient. Because then they would have the benefits of a clear specification and a fast implementation. What they do is they don’t keep these things separated and the language doesn’t support them separating it like that.