Pattern matching or sort of in python

I must admit that pattern matching is not really new to me. The first time I looked at haskell I was impressed by the idea a lot. It wasn’t until late that I found about new languages on the JVM that support the same idiom.

For this to work properly – the language should know how to “destruct” an already constructed element. A simple example in haskell is if you have a list of elements and you want to capture the first element in a variable and the remaining part of the list in another variable. In that case you would write something like this:

 
f (x:xs) =  -- do something with "x" as the head of the list and "xs" as the tail

This is a definition of f as a function on one argument – if you then call the function with a list like [1, 2, 3] you will have a x = 1 and xs = [2, 3] in the body of the function.

So given this knowledge today I started coding a python script at work. And I found myself trying this pattern in the python interactive shell – the result was that python supports this for lists at least:

>>> p = [1, 2]
>>> [a, b] = p
>>> a 
1
>>> b
2
>>>

But continuing further I was given an error message:

>>> p = [1, 2, 3]
>>> [a, b] = p
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
>>>

So while this works for full pattern on lists it won’t work if you provide less elements.

It is strange how exotic languages like haskell can open your mind for idioms that you can use in more “accepted” languages like python.

Reloading jars at Runtime

I haven’t blogged in a while but now i have some time :) . This week I had to dynamically reload some jars from a directory and i searched the net about the problem. I will show the implementation that I finally came up with. The problem with the dynamically loading external jars is that they should not be in the CLASSPATH because the system classloader is making some optimizations(cache) of the currently loaded classes and doesn’t search again in the CLASSPATH if it has already instantiated the class. So to make the jars dynamically loadable we have to make our own ClassLoader or we can use the URLClassLoader which is part of the JavaSE. The idea is that every time we load the class we should use a new object of the URLClassLoader so we don’t use the cache. And also we should have an interface so we can cast our instantiated object or the alternative is to use reflection and the Method class. But enough words here is the implementation:
1. The Interface :

package org.nikolavp;
 
public interface Reloadable{
    public void method();
}

2. The Class which will reload our class by a given name:

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
 
import org.nikolavp.Reloadable;
 
public class ClassReloader{ 
    public static Reloadable reload(String className) {
        Reloadable obj = null;
        URL jar = null;
        try {
            jar = new File("jars/somejar.jar").toURI().toURL();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        URLClassLoader loader = new URLClassLoader(new URL[] { jar },
                        ClassLoader.getSystemClassLoader());
        try {
            Class clazz = loader.loadClass(className);
            obj = (Reloadable) clazz.newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }  return obj;
    }
}

3. The Main class to test our work:

import org.nikolavp.Reloadable;
 
public class Main{
    public static void main(String[] args) {
        while (true) {
            Reloadable object = ClassReloader.reload("MyClassFromSomeJar");
            object.method();
            try {
                Thread.sleep(10 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

Assume that MyClassFromSomeJar exist in the jars/somejar.jar
you can now make changes to the
code in the external jar and see the results :)

Conclusion: This is one of the areas where java really shines. Try doing that with static languages like C/C++ :)