Wednesday, August 18, 2010

CouchDB: Using List Functions to sort Map/Reduce-Results by Value

I just found out that it is possible to sort the result of Map/Reduce with a list function.

Let's take the simple example that you want to count all documents grouped by a field called type. The following map function emits the values of the type fields of all documents:

function(doc) {
  emit(doc.type, 1);
}

To sum up the documents with the same value in the type field, we just need this well-known reduce function:

function(key, values) {
  return sum(values)
}
By default CouchDB yields the result ordered by the keys. But if you want to order the result by occurrences of the type of the document you either have to sort it in your app or you use a list function like this:
function(head, req) { 
  var row
  var rows=[]
  while(row = getRow()) { 
    rows.push(row)  
  } 
  rows.sort(function(a,b) { 
    return b.value-a.value
  }) 
  send(JSON.stringify({"rows" : rows}))
}
If you save the list function as sort and the Map/Reduce-functions as count together in a design document, you can fetch your sorted result like this:
curl http://.../design-doc/_list/sort/count?group=true

Of course there are other options to sort a view result. I didn't found much documentation on this topic, but this thread at stackoverflow is very informative.

Back to the couch - Cheers!

Thursday, August 5, 2010

jQuery meets CoffeeScript

Disclaimer: This article is pretty old.


I'm just amazed how brilliant jQuery and CoffeeScript are working together. JQuery promises "write less, do more", but with the clean syntax of CoffeeScript you can be even more concise!

Here is a quick example:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <title>jQuery meets CoffeeScript</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script src="http://jashkenas.github.com/coffee-script/extras/coffee-script.js"></script>
  
    <script type="text/coffeescript">

      show_message = (msg) -> 
        $('#message').hide().text(msg).fadeIn(2222, 
          -> $('#message').append('!') 
        )
              
      $ -> show_message "world"
      $('#message').click -> show_message "you"
      
    </script>

</head>
<body>
  <h1>hello <span id="message"></span></h1>
</body>
</html>

Here you see it running. Just click "world" to fire the click event.

hello

Just have a look to the JavaScript version:

var show_message = function(msg) {
  return $('#message').hide().text(msg).fadeIn(2222, function() {
    $('#message').append('!');
  });
};
$(function() {
  show_message("world");
});
$('#message').click(function() {
  show_message("you");
});
Happy coding!

Thursday, July 15, 2010

Getting started with node.js and CoffeeScript

After a couple of month the voices saying Node.js is awesome getting louder and louder, I decided to give it a try.

There is something different I never played with but I absolutly need to: CoffeeScript. I was quite amazed to find out, that those two new shiny things fit and work perfectly with one another. If you like to get Node and CoffeeScript up and running just follow my little tutorial. Its written for Mac but it should work for Linux too.

To get started first install Node.js. So lets grep the latest version directly from github at http://github.com/ry/node or download the tar-archive from http://nodejs.org/#download. To build and install Node type:

./configure
make
sudo make 

To install CoffeeScript go and clone the Node Package Manager from http://github.com/isaacs/npm.

git clone http://github.com/isaacs/npm.git
switch to the nmp directory and type
sudo make

The latest stable CoffeeScript-version will be installed to you computer. Now you have binary called "coffee", which just starts your CoffeeScripts with Node.

If you're a TextMate fangirl or fanboy you may want to install the CoffeeScript TextMate bundle. It can be found at http://github.com/jashkenas/coffee-script-tmbundle.

Ok, now you ready to play. Here is a hello world server written in CoffeeScript:

server = require('http').createServer (request, response) ->
    response.writeHead 200, {'Content-Type': 'text/plain'}
    response.end 'Hello World'
server.listen 8124
console.log 'Server running at http://0.0.0.0:8124/'
Save it as hello_world.coffee and run it with:
coffee hello_world.coffee

Open http://0.0.0.0:8124/ to see "Hello World" in your Browser.

Just take a look to the JavaScript version:
var server = require('http').createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end('Hello World');
})
server.listen(8124);
console.log('Server running at http://0.0.0.0:8124/');

Although it's a short and dead simple script, for me the Coffee version is much cleaner and readable.

Cheers!

Thursday, December 10, 2009

Prime Factors Kata in Scala

After practicing the Prime Factors Kata in Ruby a couple of days, I tried the same exercise in Scala.

Except the fact that practicing Katas is a wonderful tool to learn your IDE and optimize your workflow, I think Katas are also great to learn programming languages. You just don't have to think about your programming-problem, you can just think about your language.
Its been a while since I played around with Scala, so i started my daily Kata in Scala to refresh my skills.

The fact that Scala is not just an object oriented language but also a functional language, led me to a complete different solution of the prime factors kata as in Java or Ruby. I took the immutable List of Scala instead of a mutable Array, so I had to build the algorithm in a recursive manner.
Ok, enough talk. Here is my first solution:

// PrimeFactorsTest.scala
import org.junit.Test;
import org.junit.Assert._;
import org.hamcrest.CoreMatchers._;


class PrimeFactorsTest {
 @Test
 def shouldFactorNumbers = {
  Map(
   1  -> Nil,
   2  -> List(2),
   3  -> List(3),
   4  -> List(2,2),
   5  -> List(5),
   6  -> List(2,3),
   7  -> List(7),
   8  -> List(2,2,2),
   9  -> List(3,3),
   10 -> List(2,5),
   (2*2*5*7*13) -> List(2,2,5,7,13)
   
  ).foreach{ case (n, factors) => 
   assertThat(PrimeFactors.of(n), is(equalTo(factors)))
  }
 }
}
// PrimeFactors.scala
object PrimeFactors {

 def of(n:Int) : List[Int] = tryFactor(2, n)
 
 private def tryFactor(divider: Int, n: Int) : List[Int] = 
  if (n > 1) 
   takeIfFactorElseTryNext(divider, n) 
  else 
   Nil
 
 private def takeIfFactorElseTryNext(divider: Int, n: Int) : List[Int] = 
  if (n % divider == 0) 
   divider :: takeIfFactorElseTryNext(divider, n/divider) 
  else 
   tryFactor(divider+1, n)
}

EDIT:
A few days later, I came to a slightly better solution. I now use a nested function tryFactor to do the recursion. It's nested because in a different context it makes absolutly no sense.
As in my first solution I used object instead of class to create the singleton object PrimeFactors. Again it is not written in tail-recursive style, but Scala obviously optimizes it.

// PrimeFactors.scala
object PrimeFactors {
 
 def of(n:Int) = {
  def tryFactor(n:Int, divider:Int) : List[Int]  =
   if (n == 1) Nil
   else if (n % divider == 0) divider :: tryFactor(n/divider, divider) 
   else tryFactor(n, divider+1)
  
  tryFactor(n, 2)
 }
}

Friday, September 18, 2009

Creating anonymous classes from Java interfaces in JRuby

Today I tried to create an anonymous class from an inteface in JRuby. After a little bit of experimenting a collegue showed me that I need to alter the singelton class of an instance of the interface in order to implement a method. Here I implement the Callable-Interface:

callable = Java::java.util.concurrent.Callable.new 
class << callable
  def call
    # implements method 'call' of the Callable-Interface
  end
end

So I tried this:

require 'java'
include_class 'java.util.concurrent.Callable'
include_class 'java.util.concurrent.Executors'

msg = "from thread"

callable = Java::java.util.concurrent.Callable.new 
class << callable
  def call
    msg
  end
end

executor_service = Executors.newFixedThreadPool 1
future = executor_service.submit callable
puts "hello #{future.get}"
# -> undefined local variable or method `msg'

For my suprise, a variable defined in the outer context was invisible for the implemented method. I expected that it would behave just like a Ruby block or a Java anonymous class which can "see" the outer defined variables.

We found a workaround for this problem by calling a method on the instance that initializes a instance variable with the needed object:

require 'java'
include_class 'java.util.concurrent.Callable'
include_class 'java.util.concurrent.Executors'

msg = "from thread"

callable = Java::java.util.concurrent.Callable.new 
class << callable
  def init(msg)
    @msg = msg
  end
  def call
    @msg
  end
end
callable.init msg

executor_service = Executors.newFixedThreadPool 1
future = executor_service.submit callable
puts "hello #{future.get}"
# -> hello from thread
Please let me know if there a more elegant way to accomplish this!

Tuesday, September 1, 2009

Pattern to low couple to proprietary frameworks

Most Java programmers that are using proprietary libraries or other immutable frameworks are faced with a problem: Every time you need some functionality to work with a framework class you build procedural or even worse redundant code. This leads to high coupling between the framework and the rest of the application.

Here are some thoughts to solve this problem:

Inheritance
The additional functionality is added to an inherited class. This often leads to many casts and instanceof in the source code.

Simple Wrapper
A class that holds the class of the framework. All additonal functionality is added by the wrapper class. Whenever the framework class is needed it can be retrieved by a getter of the wrapper class.

Decorator
Using the decorator pattern often is not possible. The reason is that the framework class has to implement a shared interface with the decorater class but there is no way to change the framework. If the framework class does not implement such an interface the decorator pattern can not be used.

Monkey Patching
For Java-Programmers this is unusual, but often used in ruby and other dynamic languages. With the rise of JRuby and Groovy you can now patch your Java framework classes easily and follow TDA to get DRY code. In some static languages like Scala this is possible, too (see Pimp my Library).
In my opinion monkey patching is the best way to work with unchangable frameworks.

Friday, August 28, 2009

clojure parallel map

Map is a function that takes two parameters. A list and another function that will be executed for every element in the list:

(defn multiply-by-2[x]
  (* x 2))

(map multiply-by-2 [0 1 2 3 4 5 6 7 8 9])
; => (0 2 4 6 8 10 12 14 16 18)

The map funktion is a wonderful thing, because every computation for a list element can be parallelized.

Even though Clojure has the function pmap that does exactly this parallelization, I built the same functionality on my own to understand multithreading with Clojure and java.util.concurrent.

Here it is:
(import '(java.util.concurrent Executors))
(defn my-pmap [map-fn, l] 
  (let [len (count l)
   pool  (Executors/newFixedThreadPool len)
   tasks (map
           (fn [i](fn [] (map-fn (nth l i))))
           (range len))]

     (let [result (map 
                    (fn [future](.get future)) 
                    (.invokeAll pool tasks))]
       (.shutdown pool)
       result)))

(my-pmap multiply-by-2 [0 1 2 3 4 5 6 7 8 9])
; => (0 2 4 6 8 10 12 14 16 18)

; it yields the same result as
(pmap multiply-by-2 [0 1 2 3 4 5 6 7 8 9])