Wednesday, March 31, 2010

Setting Up Querly

In my last post, I introduced Querly (a simple CouchDB query engine).  In this post, I provide the steps for setting up Querly.

1. Start by installing CouchDB.  A windows installer is available on the couchdb wiki.
2. Next, install Erlang.  I'm using version R13B04.
3. Now, install RabbitMQ, available at http://www.rabbitmq.com/download.html.  Note: Querly has built in support for subscription to a RabbitMQ queue.  This allows updates to be received without having to reload an entire database from CouchDB.  This is not required for utilization of the basic functionality of Querly; however, it is required for auto update support and for all of the tests to run successfully.
4. Launch CouchDB and RabbitMQ.
5. Retrieve Querly from http://github.com/dmohl/Querly.
6. Edit make.bat in the Querly directory and change the Erlang directories appropriately.
7. Launch Querly by executing the batch file in the main directory.  This will run all of the tests.

In a future post, I'll talk about how to get started with Querly.

 

Thursday, March 25, 2010

Querly - A Query Engine for CouchDB

I've been playing with Erlang and CouchDB for a little while now and have been completely amazed by both.  One of the things that would be very helpful for my team to be able to use CouchDB in more production scenarios would be to have enhanced query functionality.  Querly is the first attempt at this enhancement.  Querly is a solution written in Erlang that allows ETS and/or limited SQL style queries against CouchDB.  While there are a number of limitations with this first attempt, it provides a starting point.  The code is available at http://github.com/dmohl/Querly.

I plan to provide additional information including setup instructions, features, limitations, etc. over my next few blog posts.  

Tuesday, March 23, 2010

Project Euler Problem 2

In my last post I talked about how I've been using the Project Euler problems as a sort of Code Kata.  Thanks to @rickasaurus and @calvinb for providing great feedback/comments on that solution!

Problem 2 is below:

Problem 2: "Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...  Find the sum of all the even-valued terms in the sequence which do not exceed four million." (Project Euler Problem #2)

Tests:

module Problem2Tests

open NUnit.Framework   
  
[<TestFixture>]    
type Problem2__When_finding_sum_of_even_valued_terms_in_the_fibonacci_sequence_up_to_89 () =   
    [<Test>]  
    member this.should_return_sum_of_44 () =  
        let result = Problem2.FindSum 89  
        Assert.AreEqual(44, result)        

[<TestFixture>]    
type Problem2__When_finding_sum_of_even_valued_terms_in_the_fibonacci_sequence_below_4_million () =   
    [<Test>]  
    member this.should_return_sum_of_4613732 () =  
        let result = Problem2.FindSum 4000000  
        Assert.AreEqual(4613732, result)
Code:

module Problem2

let FindSum max =
    let rec getFibonacciSumForEvens lastValue currentValue accumulator =
        match lastValue + currentValue with
        | sum when sum >= max -> accumulator
        | sum when sum % 2 = 0 -> 
            getFibonacciSumForEvens currentValue sum (accumulator + sum)
        | sum -> getFibonacciSumForEvens currentValue sum accumulator
    getFibonacciSumForEvens 0 1 0 


Saturday, March 20, 2010

Code Kata and Project Euler

Recently, I've been thinking about the concept of Code Kata and the personal improvement that this type of practice can bring.  I started thinking of types of exercises that I could do to utilize this concept.  After looking around on the web, I landed on projecteuler.net and found that the problems listed seemed to fit my agenda perfectly. 

For those of you who don't know, "Project Euler is a series of challenging mathematical/computer programming problems..." (ProjectEuler.net).  The mathematical nature of these problems make them a great fit for the functional paradigm.

Here's the first problem:

"Add all the natural numbers below one thousand that are multiples of 3 or 5."  Additional details include the following simple example: "If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23."

With the help of the simple example, I created a test that looked like this:

 
module Problem1Tests

open NUnit.Framework 

[<TestFixture>]  
type FindPrimes2__When_Getting_Sum_Of_Multiples_Of_3_And_5_to_a_max_number_of_10 () = 
    [<Test>]
    member this.should_return_sum_of_23 () =
        let result = Problem1.GetSumOfMultiplesOf3And5(10)
        Assert.AreEqual(23, result)

After a few refactoring sessions, my final code looked like this:

 
module Problem1

let GetSumOfMultiplesOf3And5 max =
    seq{3..max-1} |> Seq.fold(fun acc number -> 
                        (if (number % 3 = 0 || number % 5 = 0) then 
                            acc + number else acc)) 0 

Have you found good Code Kata exercises?  If so, I'd love to see them.  Also, let me know if you have a better solution for the problem above.