Say hi to Mikael Boman's new blog The wheel of destiny!
First entry is about The top six technical practices every Product Owner must know about. Enjoy!
Kurser i Domain-Driven Design - Våren 2012
Tuesday, November 17, 2009
Tuesday, November 03, 2009
From Java to Clojure, followup followup
Martin Lübcke came up with this solution to the frequency sorting problem presented in the PNEHM article From Java to Clojure:
When it comes to performance, this version is about as fast as the original version in the article, but it sure is compact, and surprisingly readable. Many thanks to Martin!
Replace frest with fnext for Clojure 1.0.
(defn order-by-freq [coll]
(keys (sort-by #(vector (- (frest %)) (first %)) (frequencies coll))))
When it comes to performance, this version is about as fast as the original version in the article, but it sure is compact, and surprisingly readable. Many thanks to Martin!
Sunday, November 01, 2009
Tuesday, October 27, 2009
New Citerus Blogger
Follow Mattias Holmqvist's new blog "Learning more about software development".
The first two posts focus on, yes, you guessed it, Clojure, as Mattias works through some examples from Structure and Interpretation of Computer Programs.
The first two posts focus on, yes, you guessed it, Clojure, as Mattias works through some examples from Structure and Interpretation of Computer Programs.
Labels:
citerus,
clojure,
development,
reading
From Java to Clojure Followup
Interest in the From Java to Clojure article has been pretty big, rarely before have we seen a PNEHM!-article getting so much attention!
I have also gotten some great feedback, in particular Stuart Halloway suggests that we use Clojure-contrib's seq-utils/frequencies to improve the code. Since we use Apache Commons Collections in the Java version of the code, its only fair for us to dive into the goodness of Clojure-contrib to see what can be of use there.
Let us start by a quick review of what our final version of the Clojure code looked like:
As it turns out, clojure.contrib.seq-utils/frequencies does exactly what our function count-words does, as such we can use it as a drop-in replacement. A version which uses contrib now looks like this:
We have to make sure we
Now, for extra credits, let's look inside the frequencies function in Clojure-contrib, to see what i looks like:
The implementation is quite different from our own, it feels a bit more straight-forward and intuitive. Initially, an empty map is created. As the collection is reduced a copy of the map is created for each processed item and the item is added with an incremented count (if the item already is in the map) or added to the new map with a count of one (if it is the first time the item is processed). get, gets a value from the map given a key, if there is no match the default, '0', is returned. inc increments the value, and assoc associates the value to a key in the map.
Not only is this version simpler than our own (which is good), it's also much faster (also good). Using seq-utils/frequencies a sample run with our micro-benchmark now looks like this (sorting 100 characters with 10000 samples):
Java: 120 ms
Groovy: 538 ms
Time Clojure: 563 ms
Excellent!
So, Joshua Bloch's item 47 in Effective Java (2 ed) applies as always: Know and use the libraries. If you get the feeling that someone must have done what you are about to do before you, someone most probably have.
Many thanks to Stuart Halloway (who will be at Øredev next week, don't miss it!).
The sources at http://code.google.com/p/pnehm-java-to-cool-language-x/ have been updated with the alternative version.
I have also gotten some great feedback, in particular Stuart Halloway suggests that we use Clojure-contrib's seq-utils/frequencies to improve the code. Since we use Apache Commons Collections in the Java version of the code, its only fair for us to dive into the goodness of Clojure-contrib to see what can be of use there.
Let us start by a quick review of what our final version of the Clojure code looked like:
(ns step4.pnehm.clojure-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 (zero? freq) freq (compare val1 val2))))
(defn order-by-freq [coll]
(keys (sort cmpr (count-words coll))))
As it turns out, clojure.contrib.seq-utils/frequencies does exactly what our function count-words does, as such we can use it as a drop-in replacement. A version which uses contrib now looks like this:
(ns withcontrib.pnehm.clojure-orderer
(:use clojure.contrib.seq-utils))
(defn cmpr [[val1 freq1] [val2 freq2]]
(let [freq (compare freq2 freq1)]
(if-not (zero? freq) freq (compare val1 val2))))
(defn order-by-freq [coll]
(keys (sort cmpr (frequencies coll))))
We have to make sure we
:use clojure.contrib.seq-utils, and then we can replace our call to count-words, with a call to frequencies.Now, for extra credits, let's look inside the frequencies function in Clojure-contrib, to see what i looks like:
(defn frequencies
"Returns a map from distinct items in coll to the number of times
they appear."
[coll]
(reduce (fn [counts x]
(assoc counts x (inc (get counts x 0))))
{} coll))
The implementation is quite different from our own, it feels a bit more straight-forward and intuitive. Initially, an empty map is created. As the collection is reduced a copy of the map is created for each processed item and the item is added with an incremented count (if the item already is in the map) or added to the new map with a count of one (if it is the first time the item is processed). get, gets a value from the map given a key, if there is no match the default, '0', is returned. inc increments the value, and assoc associates the value to a key in the map.
Not only is this version simpler than our own (which is good), it's also much faster (also good). Using seq-utils/frequencies a sample run with our micro-benchmark now looks like this (sorting 100 characters with 10000 samples):
Java: 120 ms
Groovy: 538 ms
Time Clojure: 563 ms
Excellent!
So, Joshua Bloch's item 47 in Effective Java (2 ed) applies as always: Know and use the libraries. If you get the feeling that someone must have done what you are about to do before you, someone most probably have.
Many thanks to Stuart Halloway (who will be at Øredev next week, don't miss it!).
The sources at http://code.google.com/p/pnehm-java-to-cool-language-x/ have been updated with the alternative version.
Monday, October 19, 2009
From Java to Clojure
In this article, published in Citerus’ newsletter PNEHM!, I introduce Clojure to Java developers by converting a piece of Java code to Clojure. The article examines how to call Java code from Clojure and Clojure code from Java, complete with source code.
Citerus’ consultant Peter Backlund took the same journey some months ago with his article Från Java till Groovy (in Swedish).
Read From Java to Clojure.
Citerus’ consultant Peter Backlund took the same journey some months ago with his article Från Java till Groovy (in Swedish).
Read From Java to Clojure.
Thursday, October 08, 2009
DDDSample Presentation (free event)
On Thursday evening, October 22, Sweden Spring User Group is hosting an even in Stockholm where Peter Backlund (mostly) and I (less) will present the current state of the DDDSample application.
Sedan applikationen först introducerades för drygt ett år sedan har den kontinuerligt utvecklats, mycket tack vara den feedback som kommit från olika communities. Det är snart dags för en ny release och under denna träff kommer vi kika närmare på applikationens olika delar och se hur dessa mappar mot koncept inom Domain-Driven Design.
Vi kommer att visa hur Spring (och i viss mån andra ramverk) hjälper oss att programmera domändrivet genom att dels låta oss bygga en ren och rik modell i Java, och dels genom att ta hand om infrastrukturell kod och låta oss fokusera på affärsnytta (focus on the core domain).
The presentation will be in Swedish and the event is free to attend.
Sign up at http://www.eventbrite.com/event/445416252
Welcome!
Agenda
DDDSample är en Spring-applikation som utvecklats för att påvisa hur idéer och koncept från Domain-Driven Design kan implementeras i en modern utvecklingsstack. Applikationen utvecklas i samarbete med Eric Evans, författare till boken Domain-Driven Design, och syftar till att visa hur mönster från DDD konkret kan användas för att skapa en systemarkitektur för att lösa problem i en komplex domän, utan att skapa onödig teknisk komplexitet.Sedan applikationen först introducerades för drygt ett år sedan har den kontinuerligt utvecklats, mycket tack vara den feedback som kommit från olika communities. Det är snart dags för en ny release och under denna träff kommer vi kika närmare på applikationens olika delar och se hur dessa mappar mot koncept inom Domain-Driven Design.
Vi kommer att visa hur Spring (och i viss mån andra ramverk) hjälper oss att programmera domändrivet genom att dels låta oss bygga en ren och rik modell i Java, och dels genom att ta hand om infrastrukturell kod och låta oss fokusera på affärsnytta (focus on the core domain).
The presentation will be in Swedish and the event is free to attend.
Sign up at http://www.eventbrite.com/event/445416252
Welcome!
Labels:
citerus,
DDD,
development,
java,
spam
Subscribe to:
Posts (Atom)