The idea is to gradually, over a few steps, move from a Java
implementation to a pure Clojure implementation. A this step the
Clojure implementation is called by the Java JUnit-test (see complete
test below). In the last step, the Java-references will be removed
from the Clojure implementation and the JUnit test replaced by Clojure
tests.

The complete code, including Java, Groovy and current Clojure
implementation and a micro-benchmark, can be found here:
http://code.google.com/p/pnehm-java-to-cool-language-x/source/browse

The complete JUnit test:
public class OrdererTestUsingJava extends TestCase {

    private void doTestOrderByFreq(Orderer orderer) {
        List<String> unordered = Arrays.asList("b", "d", "b", "b",
"a", "c", "a");
        List<String> ordered = orderer.orderByFreq(unordered);
        assertEquals(Arrays.asList("b", "a", "c", "d"), ordered);

        unordered = Arrays.asList("b", "d", "d", "e", "b", "a", "c", "a");
        ordered = orderer.orderByFreq(unordered);
        assertEquals(Arrays.asList("a", "b", "d", "c", "e"), ordered);

        unordered = new ArrayList<String>();
        ordered = orderer.orderByFreq(unordered);
        assertTrue(unordered.isEmpty());
        assertTrue(ordered.isEmpty());
    }

    public void testJavaImpl() {
        doTestOrderByFreq(new JavaOrderer());
    }

    public void testClojureImpl() {
        doTestOrderByFreq(new step3.pnehm.ClojureOrderer());
    }

    public void testGroovyImpl() {
        doTestOrderByFreq(new GroovyOrderer());
    }

}

/Patrik

On Thu, Jul 9, 2009 at 3:23 PM, Sean Devlin<francoisdev...@gmail.com> wrote:
>
> One question on design intent before feedback.  Is your intent to have
> this Clojure code called by Java code later?
>
>
> On Jul 9, 7:31 am, Patrik Fredriksson <patri...@gmail.com> wrote:
>> Hi!
>>
>> I started to look closer at Clojure after Rich Hickey's presentation
>> at QCon London in March, this is my first post. I spend my days
>> programming Java, but my journey as developer did actually start with
>> an ML programming course many years ago. It has been great fun to
>> re-discover the functional style of programming again! This list is a
>> great source of knowledge.
>>
>> A colleague of mine wrote an article about how to convert a piece of
>> Java code to Groovy a few months ago. As an exercise I thought I
>> should do the same, but to Clojure of course. I'd really appreciate
>> some feedback on the solution. I hope you aren't too sick of these
>> kind of posts just yet :-)
>>
>> The code orders a list of characters, first by frequency and then
>> alphabetically if two characters happen to have the same frequency.
>>
>> A Java unit test:
>>
>> private void doTestOrderByFreq(Orderer orderer) {
>>         List<String> unordered = Arrays.asList("b", "d", "b", "b",
>> "a", "c", "a");
>>         List<String> ordered = orderer.orderByFreq(unordered);
>>         assertEquals(Arrays.asList("b", "a", "c", "d"), ordered);
>>
>> }
>>
>> A Java implementation:
>>
>> public class JavaOrderer implements Orderer {
>>
>>     public List<String> orderByFreq(List<String> in) {
>>         final Bag bag = new HashBag(in);
>>         List<String> out = new ArrayList<String>(bag.uniqueSet());
>>
>>         Collections.sort(out, new Comparator<String>() {
>>             public int compare(String a, String b) {
>>                 int freq = Integer.valueOf(
>>                     bag.getCount(b)).compareTo(bag.getCount(a)
>>                 );
>>                 return freq != 0 ? freq : a.compareTo(b);
>>             }
>>         });
>>
>>         return out;
>>     }
>>
>> }
>>
>> My shot at a Clojure implementation, with inspiration from a previous
>> post in this group on a similar problem:
>>
>> (ns step3.pnehm.clojure-orderer
>>   (:gen-class
>>     :name step3.pnehm.ClojureOrderer
>>     :implements [pnehm.Orderer]
>>     ))
>>
>> (defn count-words [coll]
>>   (reduce #(merge-with + %1 {%2 1}) {} coll))
>>
>> (defn cmpr [[val1 freq1] [val2 freq2]]
>>   (let [freq (compare freq2 freq1)]
>>     (if (not= freq 0) freq (compare val1 val2))))
>>
>> (defn -orderByFreq [_ coll]
>>   (if (empty? coll) () (keys (sort cmpr (count-words coll)))))
>>
>> Orderer is a simple Java interface making it easy to call different
>> implementations from the JUnit test:
>>
>> List<String> orderByFreq(List<String> in);
>>
>> All kinds of feedback are welcome, both on style and the chosen solution.
>>
>> Many thanks!
>>
>> /Patrik
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to