Search code examples
javacommon-lispcross-product

Cross-product calculator in Java


I am working my way through Norvig's book on AIP. There is an exercise in it on writing a cross-product function -

(defun cross-product (fn list-1 list-2)
  (mappend #'(lambda (y)
               (mapcar #'(lambda (x)
                           (funcall fn y x))
                       list-2))
           list-1))

(defun mappend (fn the-list)
  (if (null the-list)
      nil
      (append (funcall fn (first the-list))
              (mappend fn (rest the-list)))))

I am trying to write an implementation in Java -

interface Function<T1, T2, T3> {
    public T3 function(T1 t1, T2 t2);
}

public class CrossProduct<T1, T2> {
    private List<T1> list1;
    private List<T2> list2;

    public CrossProduct(List<T1> t1, List<T2> t2) {
         this.list1 = t1;
         this.list2 = t2;
    }

    public <T3> List<T3> calculate(Function<T1, T2, T3> fn) {
    List product = new ArrayList();
    for (int i = 0; i < list1.size(); i++)
        for (int j = 0; j < list2.size(); j++)
            product.add(fn.function(list1.get(i), list2.get(j)));
    return product;
}

}

Usage -

@Test
public void testWithStrings() {
    List<String> list1 = new ArrayList<String>();
    list1.add("6");
    list1.add("8");

    List<String> list2 = new ArrayList<String>();
    list2.add("2");
    list2.add("3");

    List<String> product = new CrossProduct<String, String>(list1, list2)
            .<String> calculate(new Function<String, String, String>() {
                public String function(String x, String y) {
                    return (String) x + (String) y;
                }

            });

    Assert.assertEquals("62", product.get(0));
    Assert.assertEquals("63", product.get(1));
    Assert.assertEquals("82", product.get(2));
    Assert.assertEquals("83", product.get(3));
}

Is there a better way of doing this?


Solution

  • It seems a little arbitrary to define your CrossProduct class that way: why are the list args member variables, whereas the fn is a method parameter? In fact, why is CrossProduct a class at all? A cross product is a list, but it's not a subtype of list, since a given list could both

    1. be expressed as a cross product in many different ways, and
    2. not have been constructed using the crossproduct function.

    It's not natural to think of "cross product" as a type, IMO.

    I would probably do something like

    public class ListFunctions {
        public static <T1, T2, T3> List<T3> crossProduct(List<T1> list1, List<T2> list2, Function<T1, T2, T3> fn) {
            List<T3> product = new ArrayList<T3>();
            for (int i = 0; i < list1.size(); i++)
              for (int j = 0; j < list2.size(); j++)
                product.add(fn.function(list1.get(i), list2.get(j)));
            return product;
        }
    }
    

    If you did want to define a class CrossProduct for some reason (e.g. to implement lazy evaluation as salman suggested), I would say it's more OO to have all three args as member variables, and have the class implement List, e.g.

    public class CrossProduct<T1, T2, T3> implements List<T3> {
        public CrossProduct(T1 list1, T2 list2, Function<T1, T2, T3> fn) {
            // remember args...
        }
        // etc...
    }